|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewWellKnownHandler(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + authInfoHandler http.Handler |
| 18 | + expectedNil bool |
| 19 | + testRequests []testRequest |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "nil authInfoHandler returns nil", |
| 23 | + authInfoHandler: nil, |
| 24 | + expectedNil: true, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "exact path /.well-known/oauth-protected-resource routes to authInfoHandler", |
| 28 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 29 | + w.WriteHeader(http.StatusOK) |
| 30 | + _, _ = w.Write([]byte("auth-info")) |
| 31 | + }), |
| 32 | + expectedNil: false, |
| 33 | + testRequests: []testRequest{ |
| 34 | + { |
| 35 | + path: "/.well-known/oauth-protected-resource", |
| 36 | + expectedStatus: http.StatusOK, |
| 37 | + expectedBody: "auth-info", |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "subpath /.well-known/oauth-protected-resource/mcp routes to authInfoHandler", |
| 43 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 44 | + w.WriteHeader(http.StatusOK) |
| 45 | + _, _ = w.Write([]byte("auth-info-mcp")) |
| 46 | + }), |
| 47 | + expectedNil: false, |
| 48 | + testRequests: []testRequest{ |
| 49 | + { |
| 50 | + path: "/.well-known/oauth-protected-resource/mcp", |
| 51 | + expectedStatus: http.StatusOK, |
| 52 | + expectedBody: "auth-info-mcp", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "subpath /.well-known/oauth-protected-resource/v1/metadata routes to authInfoHandler", |
| 58 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 59 | + w.WriteHeader(http.StatusOK) |
| 60 | + _, _ = w.Write([]byte("auth-info-v1")) |
| 61 | + }), |
| 62 | + expectedNil: false, |
| 63 | + testRequests: []testRequest{ |
| 64 | + { |
| 65 | + path: "/.well-known/oauth-protected-resource/v1/metadata", |
| 66 | + expectedStatus: http.StatusOK, |
| 67 | + expectedBody: "auth-info-v1", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "other .well-known paths return 404", |
| 73 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 74 | + w.WriteHeader(http.StatusOK) |
| 75 | + _, _ = w.Write([]byte("should-not-reach")) |
| 76 | + }), |
| 77 | + expectedNil: false, |
| 78 | + testRequests: []testRequest{ |
| 79 | + { |
| 80 | + path: "/.well-known/openid-configuration", |
| 81 | + expectedStatus: http.StatusNotFound, |
| 82 | + expectedBody: "404 page not found\n", |
| 83 | + }, |
| 84 | + { |
| 85 | + path: "/.well-known/other", |
| 86 | + expectedStatus: http.StatusNotFound, |
| 87 | + expectedBody: "404 page not found\n", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "RFC 9728 compliance - all oauth-protected-resource paths work", |
| 93 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 94 | + w.WriteHeader(http.StatusOK) |
| 95 | + _, _ = w.Write([]byte("discovered")) |
| 96 | + }), |
| 97 | + expectedNil: false, |
| 98 | + testRequests: []testRequest{ |
| 99 | + { |
| 100 | + path: "/.well-known/oauth-protected-resource", |
| 101 | + expectedStatus: http.StatusOK, |
| 102 | + expectedBody: "discovered", |
| 103 | + }, |
| 104 | + { |
| 105 | + path: "/.well-known/oauth-protected-resource/", |
| 106 | + expectedStatus: http.StatusOK, |
| 107 | + expectedBody: "discovered", |
| 108 | + }, |
| 109 | + { |
| 110 | + path: "/.well-known/oauth-protected-resource/any/deep/path", |
| 111 | + expectedStatus: http.StatusOK, |
| 112 | + expectedBody: "discovered", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "handler preserves request context and headers", |
| 118 | + authInfoHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 | + // Verify request is passed through correctly |
| 120 | + if r.Header.Get("X-Test-Header") == "test-value" { |
| 121 | + w.Header().Set("X-Response-Header", "response-value") |
| 122 | + w.WriteHeader(http.StatusOK) |
| 123 | + _, _ = w.Write([]byte("headers-ok")) |
| 124 | + } else { |
| 125 | + w.WriteHeader(http.StatusBadRequest) |
| 126 | + } |
| 127 | + }), |
| 128 | + expectedNil: false, |
| 129 | + testRequests: []testRequest{ |
| 130 | + { |
| 131 | + path: "/.well-known/oauth-protected-resource", |
| 132 | + headers: map[string]string{"X-Test-Header": "test-value"}, |
| 133 | + expectedStatus: http.StatusOK, |
| 134 | + expectedBody: "headers-ok", |
| 135 | + expectedHeaders: map[string]string{"X-Response-Header": "response-value"}, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + t.Parallel() |
| 144 | + |
| 145 | + handler := NewWellKnownHandler(tt.authInfoHandler) |
| 146 | + |
| 147 | + if tt.expectedNil { |
| 148 | + assert.Nil(t, handler, "expected nil handler") |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + require.NotNil(t, handler, "expected non-nil handler") |
| 153 | + |
| 154 | + // Test each request scenario |
| 155 | + for _, req := range tt.testRequests { |
| 156 | + t.Run(req.path, func(t *testing.T) { |
| 157 | + request := httptest.NewRequest(http.MethodGet, req.path, nil) |
| 158 | + |
| 159 | + // Add test headers |
| 160 | + for key, value := range req.headers { |
| 161 | + request.Header.Set(key, value) |
| 162 | + } |
| 163 | + |
| 164 | + recorder := httptest.NewRecorder() |
| 165 | + handler.ServeHTTP(recorder, request) |
| 166 | + |
| 167 | + assert.Equal(t, req.expectedStatus, recorder.Code, "status code mismatch") |
| 168 | + assert.Equal(t, req.expectedBody, recorder.Body.String(), "body mismatch") |
| 169 | + |
| 170 | + // Check expected response headers |
| 171 | + for key, value := range req.expectedHeaders { |
| 172 | + assert.Equal(t, value, recorder.Header().Get(key), "header %s mismatch", key) |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestWellKnownHandler_HTTPMethods(t *testing.T) { |
| 181 | + t.Parallel() |
| 182 | + |
| 183 | + authInfoHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 184 | + // Echo back the HTTP method |
| 185 | + w.WriteHeader(http.StatusOK) |
| 186 | + _, _ = w.Write([]byte(req.Method)) |
| 187 | + }) |
| 188 | + |
| 189 | + handler := NewWellKnownHandler(authInfoHandler) |
| 190 | + require.NotNil(t, handler) |
| 191 | + |
| 192 | + methods := []string{ |
| 193 | + http.MethodGet, |
| 194 | + http.MethodPost, |
| 195 | + http.MethodPut, |
| 196 | + http.MethodDelete, |
| 197 | + http.MethodPatch, |
| 198 | + http.MethodOptions, |
| 199 | + } |
| 200 | + |
| 201 | + for _, method := range methods { |
| 202 | + t.Run(method, func(t *testing.T) { |
| 203 | + t.Parallel() |
| 204 | + |
| 205 | + request := httptest.NewRequest(method, "/.well-known/oauth-protected-resource", nil) |
| 206 | + recorder := httptest.NewRecorder() |
| 207 | + |
| 208 | + handler.ServeHTTP(recorder, request) |
| 209 | + |
| 210 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 211 | + assert.Equal(t, method, recorder.Body.String()) |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +func TestWellKnownHandler_EdgeCases(t *testing.T) { |
| 217 | + t.Parallel() |
| 218 | + |
| 219 | + authInfoHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 220 | + w.WriteHeader(http.StatusOK) |
| 221 | + _, _ = w.Write([]byte("ok")) |
| 222 | + }) |
| 223 | + |
| 224 | + handler := NewWellKnownHandler(authInfoHandler) |
| 225 | + require.NotNil(t, handler) |
| 226 | + |
| 227 | + tests := []struct { |
| 228 | + name string |
| 229 | + path string |
| 230 | + expectedStatus int |
| 231 | + expectedBody string |
| 232 | + }{ |
| 233 | + { |
| 234 | + name: "path with query parameters routes correctly", |
| 235 | + path: "/.well-known/oauth-protected-resource?format=json", |
| 236 | + expectedStatus: http.StatusOK, |
| 237 | + expectedBody: "ok", |
| 238 | + }, |
| 239 | + { |
| 240 | + name: "path with trailing slash routes correctly", |
| 241 | + path: "/.well-known/oauth-protected-resource/", |
| 242 | + expectedStatus: http.StatusOK, |
| 243 | + expectedBody: "ok", |
| 244 | + }, |
| 245 | + { |
| 246 | + name: "different .well-known path returns 404", |
| 247 | + path: "/.well-known/jwks.json", // Different endpoint |
| 248 | + expectedStatus: http.StatusNotFound, |
| 249 | + expectedBody: "404 page not found\n", |
| 250 | + }, |
| 251 | + { |
| 252 | + name: "path prefix match is not sufficient", |
| 253 | + path: "/.well-known/oauth", // Prefix but not full path |
| 254 | + expectedStatus: http.StatusNotFound, |
| 255 | + expectedBody: "404 page not found\n", |
| 256 | + }, |
| 257 | + } |
| 258 | + |
| 259 | + for _, tt := range tests { |
| 260 | + t.Run(tt.name, func(t *testing.T) { |
| 261 | + t.Parallel() |
| 262 | + |
| 263 | + request := httptest.NewRequest(http.MethodGet, tt.path, nil) |
| 264 | + recorder := httptest.NewRecorder() |
| 265 | + |
| 266 | + handler.ServeHTTP(recorder, request) |
| 267 | + |
| 268 | + assert.Equal(t, tt.expectedStatus, recorder.Code) |
| 269 | + assert.Equal(t, tt.expectedBody, recorder.Body.String()) |
| 270 | + }) |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +// testRequest defines a test request scenario |
| 275 | +type testRequest struct { |
| 276 | + path string |
| 277 | + headers map[string]string |
| 278 | + expectedStatus int |
| 279 | + expectedBody string |
| 280 | + expectedHeaders map[string]string |
| 281 | +} |
0 commit comments