@@ -57,10 +57,12 @@ func TestClient(t *testing.T) {
5757			ClientSecret : "test-client-secret" ,
5858			TokenURL :     "https://localhost:8080/oauth2/token" ,
5959		}
60- 		client , err  :=  NewClient (WithOAuth2 (oauth2Config ))
60+ 		client , err  :=  NewClient (WithHTTPClientConfig (& HTTPClientConfig {
61+ 			OAuth2 : oauth2Config ,
62+ 		}))
6163		require .NoError (t , err )
6264
63- 		require .Equal (t , oauth2Config , client .cfg .ouath2Config )
65+ 		require .Equal (t , oauth2Config , client .cfg .httpClientConfig . OAuth2 )
6466	})
6567
6668	t .Run ("WithOAuth2 invalid TLS" , func (t  * testing.T ) {
@@ -72,7 +74,9 @@ func TestClient(t *testing.T) {
7274				CACertificate : "invalid-ca-cert" ,
7375			},
7476		}
75- 		_ , err  :=  NewClient (WithOAuth2 (oauth2Config ))
77+ 		_ , err  :=  NewClient (WithHTTPClientConfig (& HTTPClientConfig {
78+ 			OAuth2 : oauth2Config ,
79+ 		}))
7680		require .ErrorIs (t , err , ErrOAuth2TLSConfigInvalid )
7781	})
7882}
@@ -245,6 +249,7 @@ func TestSendWebhookOAuth2(t *testing.T) {
245249		expOAuth2RequestValues  url.Values 
246250		expClientError          error 
247251		expOAuthError           error 
252+ 		expProxyRequests        bool 
248253	}{
249254		{
250255			name : "valid simple OAuth2 config" ,
@@ -408,11 +413,33 @@ func TestSendWebhookOAuth2(t *testing.T) {
408413			},
409414			expOAuthError : customDialError ,
410415		},
416+ 		{
417+ 			name : "proxy in OAuth2 config" ,
418+ 			oauth2Config : OAuth2Config {
419+ 				ClientID :     "test-client-id" ,
420+ 				ClientSecret : "test-client-secret" ,
421+ 				ProxyConfig : & ProxyConfig {
422+ 					ProxyURL : "xxxx" , // This will be replaced with the test server URL. 
423+ 				},
424+ 			},
425+ 			oauth2Response : oauth2Response {
426+ 				AccessToken : "12345" ,
427+ 				TokenType :   "Bearer" ,
428+ 			},
429+ 
430+ 			expOAuth2RequestValues : url.Values {
431+ 				"grant_type" : []string {"client_credentials" },
432+ 			},
433+ 			expOAuth2AuthHeaders : http.Header {
434+ 				"Authorization" : []string {GetBasicAuthHeader ("test-client-id" , "test-client-secret" )},
435+ 			},
436+ 			expProxyRequests : true ,
437+ 		},
411438	}
412439	for  _ , tc  :=  range  tcs  {
413440		t .Run (tc .name , func (t  * testing.T ) {
414441			oathRequestCnt  :=  0 
415- 			oauth2Server  :=  httptest . NewServer ( http . HandlerFunc ( func (w  http.ResponseWriter , r  * http.Request ) {
442+ 			oauthHandler  :=  func (w  http.ResponseWriter , r  * http.Request ) {
416443				oathRequestCnt ++ 
417444
418445				for  k  :=  range  tc .expOAuth2AuthHeaders  {
@@ -427,8 +454,22 @@ func TestSendWebhookOAuth2(t *testing.T) {
427454				res , _  :=  json .Marshal (tc .oauth2Response )
428455				w .Header ().Add ("Content-Type" , "application/json" )
429456				_ , _  =  w .Write (res )
430- 			}))
457+ 			}
458+ 
459+ 			oauth2Server  :=  httptest .NewServer (http .HandlerFunc (oauthHandler ))
431460			defer  oauth2Server .Close ()
461+ 			tokenUrl  :=  oauth2Server .URL  +  "/oauth2/token" 
462+ 
463+ 			proxyRequestCnt  :=  0 
464+ 			proxyServer  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
465+ 				proxyRequestCnt ++ 
466+ 				// Verify this is a proxy request. 
467+ 				assert .Equal (t , tokenUrl , r .RequestURI , "expected request to be sent to oauth server" )
468+ 
469+ 				// Simulate forwarding the request to the OAuth2 handler. 
470+ 				oauthHandler (w , r )
471+ 			}))
472+ 			defer  proxyServer .Close ()
432473
433474			webhookRequestCnt  :=  0 
434475			webhookServer  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
@@ -440,8 +481,19 @@ func TestSendWebhookOAuth2(t *testing.T) {
440481			defer  webhookServer .Close ()
441482
442483			oauthConfig  :=  tc .oauth2Config 
443- 			oauthConfig .TokenURL  =  oauth2Server .URL 
444- 			client , err  :=  NewClient (append (tc .otherClientOpts , WithOAuth2 (& oauthConfig ))... )
484+ 			oauthConfig .TokenURL  =  tokenUrl 
485+ 
486+ 			if  oauthConfig .ProxyConfig  !=  nil  &&  oauthConfig .ProxyConfig .ProxyURL  !=  ""  {
487+ 				oauthConfig .ProxyConfig .ProxyURL  =  proxyServer .URL 
488+ 			}
489+ 			expectedProxyRequestCnt  :=  0 
490+ 			if  tc .expProxyRequests  {
491+ 				expectedProxyRequestCnt  =  1 
492+ 			}
493+ 
494+ 			client , err  :=  NewClient (append (tc .otherClientOpts , WithHTTPClientConfig (& HTTPClientConfig {
495+ 				OAuth2 : & oauthConfig ,
496+ 			}))... )
445497			if  tc .expClientError  !=  nil  {
446498				assert .ErrorIs (t , err , tc .expClientError , "expected client creation error to match" )
447499				return 
@@ -454,11 +506,13 @@ func TestSendWebhookOAuth2(t *testing.T) {
454506				HTTPMethod : http .MethodPost ,
455507			})
456508			if  tc .expOAuthError  !=  nil  {
457- 				assert .Equal (t , 0 , oathRequestCnt , "expected %d OAuth2 request to be sent, got: %d" , 1 , oathRequestCnt )
458- 				assert .Equal (t , 0 , webhookRequestCnt , "expected %d webhook request to be sent, got: %d" , 1 , webhookRequestCnt )
509+ 				assert .Equal (t , 0 , proxyRequestCnt , "expected %d Proxy request to be sent, got: %d" , 0 , oathRequestCnt )
510+ 				assert .Equal (t , 0 , oathRequestCnt , "expected %d OAuth2 request to be sent, got: %d" , 0 , oathRequestCnt )
511+ 				assert .Equal (t , 0 , webhookRequestCnt , "expected %d webhook request to be sent, got: %d" , 0 , webhookRequestCnt )
459512				assert .ErrorIs (t , err , tc .expOAuthError , "expected error to match" )
460513				return 
461514			}
515+ 			assert .Equal (t , expectedProxyRequestCnt , proxyRequestCnt , "expected %d proxy request to be sent, got: %d" , expectedProxyRequestCnt , proxyRequestCnt )
462516			assert .Equal (t , 1 , oathRequestCnt , "expected %d OAuth2 request to be sent, got: %d" , 1 , oathRequestCnt )
463517			assert .Equal (t , 1 , webhookRequestCnt , "expected %d webhook request to be sent, got: %d" , 1 , webhookRequestCnt )
464518			assert .NoError (t , err , "expected no error" )
@@ -469,6 +523,7 @@ func TestSendWebhookOAuth2(t *testing.T) {
469523				Body :       "test-body" ,
470524				HTTPMethod : http .MethodPost ,
471525			})
526+ 			assert .Equal (t , expectedProxyRequestCnt , proxyRequestCnt , "expected %d proxy request to be sent, got: %d" , expectedProxyRequestCnt , proxyRequestCnt )
472527			assert .Equal (t , 1 , oathRequestCnt , "expected %d OAuth2 request to be sent, got: %d" , 1 , oathRequestCnt )
473528			assert .Equal (t , 2 , webhookRequestCnt , "expected %d webhook request to be sent, got: %d" , 2 , webhookRequestCnt )
474529		})
0 commit comments