@@ -421,6 +421,85 @@ func TestEnableBasicAuthWithFunc(t *testing.T) {
421421 assert .Equal (t , http .StatusUnauthorized , resp .StatusCode , "TestEnableBasicAuthWithFunc Failed!" )
422422}
423423
424+ func TestEnableOAuth_HealthCheckEndpoint (t * testing.T ) {
425+ port := testutil .GetFreePort (t )
426+
427+ // Mock server that serves both /.well-known/alive and /.well-known/jwks.json
428+ mockServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
429+ switch r .URL .Path {
430+ case "/.well-known/alive" :
431+ w .WriteHeader (http .StatusOK )
432+ case "/.well-known/jwks.json" :
433+ w .Header ().Set ("Content-Type" , "application/json" )
434+ w .WriteHeader (http .StatusOK )
435+ _ , _ = w .Write ([]byte (`{"keys":[]}` ))
436+ default :
437+ w .WriteHeader (http .StatusNotFound )
438+ }
439+ }))
440+ defer mockServer .Close ()
441+
442+ c := container .NewContainer (config .NewMockConfig (nil ))
443+
444+ a := & App {
445+ httpServer : & httpServer {
446+ router : gofrHTTP .NewRouter (),
447+ port : port ,
448+ },
449+ container : c ,
450+ }
451+
452+ // Pass full JWKS URL with path — the fix should extract the base URL
453+ a .EnableOAuth (mockServer .URL + "/.well-known/jwks.json" , 600 )
454+
455+ // Verify the service is registered
456+ oauthService := a .container .GetHTTPService ("gofr_oauth" )
457+ require .NotNil (t , oauthService , "gofr_oauth service should be registered" )
458+
459+ // Health check should hit mockServer/.well-known/alive (not mockServer/.well-known/jwks.json/.well-known/alive)
460+ health := oauthService .HealthCheck (t .Context ())
461+ assert .Equal (t , "UP" , health .Status , "Health check should hit the host root, not the JWKS path" )
462+
463+ // JWKS fetch should hit mockServer/.well-known/jwks.json (not mockServer//.well-known/jwks.json)
464+ resp , err := oauthService .GetWithHeaders (t .Context (), ".well-known/jwks.json" , nil , nil )
465+ require .NoError (t , err )
466+
467+ defer resp .Body .Close ()
468+
469+ assert .Equal (t , http .StatusOK , resp .StatusCode , "JWKS fetch should hit the correct path without double slash" )
470+ }
471+
472+ func TestEnableOAuth_InvalidEndpoints (t * testing.T ) {
473+ invalidEndpoints := []string {
474+ "" ,
475+ "not-a-url" ,
476+ "/.well-known/jwks.json" ,
477+ "http://" ,
478+ "ftp://host/.well-known/jwks.json" ,
479+ }
480+
481+ for _ , endpoint := range invalidEndpoints {
482+ t .Run (endpoint , func (t * testing.T ) {
483+ port := testutil .GetFreePort (t )
484+ c := container .NewContainer (config .NewMockConfig (nil ))
485+
486+ a := & App {
487+ httpServer : & httpServer {
488+ router : gofrHTTP .NewRouter (),
489+ port : port ,
490+ },
491+ container : c ,
492+ }
493+
494+ a .EnableOAuth (endpoint , 600 )
495+
496+ // Service should NOT be registered for invalid endpoints
497+ assert .Nil (t , a .container .GetHTTPService ("gofr_oauth" ),
498+ "gofr_oauth service should not be registered for invalid endpoint: %q" , endpoint )
499+ })
500+ }
501+ }
502+
424503func encodeBasicAuthorization (t * testing.T , arg string ) string {
425504 t .Helper ()
426505
0 commit comments