@@ -66,6 +66,7 @@ const (
66
66
ExpectedAuthenticationCredentials = AuthorizationType + " " + BearerToken
67
67
ExpectedUsername = "arthurdent"
68
68
ExpectedPassword = "42"
69
+ ExpectedAccessToken = "12345"
69
70
)
70
71
71
72
var invalidHTTPClientConfigs = []struct {
@@ -363,6 +364,45 @@ func TestNewClientFromConfig(t *testing.T) {
363
364
}
364
365
},
365
366
},
367
+ {
368
+ clientConfig : HTTPClientConfig {
369
+ OAuth2 : & OAuth2 {
370
+ ClientID : "ExpectedUsername" ,
371
+ ClientSecret : "ExpectedPassword" ,
372
+ TLSConfig : TLSConfig {
373
+ CAFile : TLSCAChainPath ,
374
+ CertFile : ClientCertificatePath ,
375
+ KeyFile : ClientKeyNoPassPath ,
376
+ ServerName : "" ,
377
+ InsecureSkipVerify : false },
378
+ },
379
+ TLSConfig : TLSConfig {
380
+ CAFile : TLSCAChainPath ,
381
+ CertFile : ClientCertificatePath ,
382
+ KeyFile : ClientKeyNoPassPath ,
383
+ ServerName : "" ,
384
+ InsecureSkipVerify : false },
385
+ },
386
+ handler : func (w http.ResponseWriter , r * http.Request ) {
387
+ switch r .URL .Path {
388
+ case "/token" :
389
+ res , _ := json .Marshal (oauth2TestServerResponse {
390
+ AccessToken : ExpectedAccessToken ,
391
+ TokenType : "Bearer" ,
392
+ })
393
+ w .Header ().Add ("Content-Type" , "application/json" )
394
+ _ , _ = w .Write (res )
395
+
396
+ default :
397
+ authorization := r .Header .Get ("Authorization" )
398
+ if authorization != "Bearer " + ExpectedAccessToken {
399
+ fmt .Fprintf (w , "Expected Authorization header %q, got %q" , "Bearer " + ExpectedAccessToken , authorization )
400
+ } else {
401
+ fmt .Fprint (w , ExpectedMessage )
402
+ }
403
+ }
404
+ },
405
+ },
366
406
}
367
407
368
408
for _ , validConfig := range newClientValidConfig {
@@ -372,6 +412,12 @@ func TestNewClientFromConfig(t *testing.T) {
372
412
}
373
413
defer testServer .Close ()
374
414
415
+ if validConfig .clientConfig .OAuth2 != nil {
416
+ // We don't have access to the test server's URL when configuring the test cases,
417
+ // so it has to be specified here.
418
+ validConfig .clientConfig .OAuth2 .TokenURL = testServer .URL + "/token"
419
+ }
420
+
375
421
err = validConfig .clientConfig .Validate ()
376
422
if err != nil {
377
423
t .Fatal (err .Error ())
@@ -381,6 +427,7 @@ func TestNewClientFromConfig(t *testing.T) {
381
427
t .Errorf ("Can't create a client from this config: %+v" , validConfig .clientConfig )
382
428
continue
383
429
}
430
+
384
431
response , err := client .Get (testServer .URL )
385
432
if err != nil {
386
433
t .Errorf ("Can't connect to the test server using this config: %+v: %v" , validConfig .clientConfig , err )
@@ -1129,14 +1176,14 @@ func NewRoundTripCheckRequest(checkRequest func(*http.Request), theResponse *htt
1129
1176
theError : theError }}
1130
1177
}
1131
1178
1132
- type testServerResponse struct {
1179
+ type oauth2TestServerResponse struct {
1133
1180
AccessToken string `json:"access_token"`
1134
1181
TokenType string `json:"token_type"`
1135
1182
}
1136
1183
1137
1184
func TestOAuth2 (t * testing.T ) {
1138
1185
ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1139
- res , _ := json .Marshal (testServerResponse {
1186
+ res , _ := json .Marshal (oauth2TestServerResponse {
1140
1187
AccessToken : "12345" ,
1141
1188
TokenType : "Bearer" ,
1142
1189
})
@@ -1169,7 +1216,7 @@ endpoint_params:
1169
1216
t .Fatalf ("Expected no error unmarshalling yaml, got %v" , err )
1170
1217
}
1171
1218
if ! reflect .DeepEqual (unmarshalledConfig , expectedConfig ) {
1172
- t .Fatalf ("Got unmarshalled config %q , expected %q " , unmarshalledConfig , expectedConfig )
1219
+ t .Fatalf ("Got unmarshalled config %v , expected %v " , unmarshalledConfig , expectedConfig )
1173
1220
}
1174
1221
1175
1222
rt := NewOAuth2RoundTripper (& expectedConfig , http .DefaultTransport )
@@ -1197,7 +1244,7 @@ func TestOAuth2WithFile(t *testing.T) {
1197
1244
t .Fatal ("token endpoint called twice" )
1198
1245
}
1199
1246
previousAuth = auth
1200
- res , _ := json .Marshal (testServerResponse {
1247
+ res , _ := json .Marshal (oauth2TestServerResponse {
1201
1248
AccessToken : "12345" ,
1202
1249
TokenType : "Bearer" ,
1203
1250
})
@@ -1244,7 +1291,7 @@ endpoint_params:
1244
1291
t .Fatalf ("Expected no error unmarshalling yaml, got %v" , err )
1245
1292
}
1246
1293
if ! reflect .DeepEqual (unmarshalledConfig , expectedConfig ) {
1247
- t .Fatalf ("Got unmarshalled config %q , expected %q " , unmarshalledConfig , expectedConfig )
1294
+ t .Fatalf ("Got unmarshalled config %v , expected %v " , unmarshalledConfig , expectedConfig )
1248
1295
}
1249
1296
1250
1297
rt := NewOAuth2RoundTripper (& expectedConfig , http .DefaultTransport )
0 commit comments