|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-http-utils/headers" |
| 9 | + "github.com/platform-mesh/golang-commons/context" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestStoreAuthHeader_WithAuthHeader(t *testing.T) { |
| 14 | + expectedAuth := "Bearer token123" |
| 15 | + |
| 16 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + // Verify auth header is stored in context |
| 18 | + authFromContext, err := context.GetAuthHeaderFromContext(r.Context()) |
| 19 | + assert.NoError(t, err) |
| 20 | + assert.Equal(t, expectedAuth, authFromContext) |
| 21 | + |
| 22 | + w.WriteHeader(http.StatusOK) |
| 23 | + }) |
| 24 | + |
| 25 | + middleware := StoreAuthHeader() |
| 26 | + handlerToTest := middleware(nextHandler) |
| 27 | + |
| 28 | + req := httptest.NewRequest("GET", "http://testing", nil) |
| 29 | + req.Header.Set(headers.Authorization, expectedAuth) |
| 30 | + recorder := httptest.NewRecorder() |
| 31 | + |
| 32 | + handlerToTest.ServeHTTP(recorder, req) |
| 33 | + |
| 34 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 35 | +} |
| 36 | + |
| 37 | +func TestStoreAuthHeader_WithoutAuthHeader(t *testing.T) { |
| 38 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 39 | + // Verify empty auth header returns error when not set |
| 40 | + _, err := context.GetAuthHeaderFromContext(r.Context()) |
| 41 | + assert.Error(t, err) // Should return error when no auth header is set |
| 42 | + |
| 43 | + w.WriteHeader(http.StatusOK) |
| 44 | + }) |
| 45 | + |
| 46 | + middleware := StoreAuthHeader() |
| 47 | + handlerToTest := middleware(nextHandler) |
| 48 | + |
| 49 | + req := httptest.NewRequest("GET", "http://testing", nil) |
| 50 | + // No authorization header set |
| 51 | + recorder := httptest.NewRecorder() |
| 52 | + |
| 53 | + handlerToTest.ServeHTTP(recorder, req) |
| 54 | + |
| 55 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 56 | +} |
| 57 | + |
| 58 | +func TestStoreAuthHeader_WithEmptyAuthHeader(t *testing.T) { |
| 59 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + // Verify empty auth header returns error when empty |
| 61 | + _, err := context.GetAuthHeaderFromContext(r.Context()) |
| 62 | + assert.Error(t, err) // Should return error when auth header is empty |
| 63 | + |
| 64 | + w.WriteHeader(http.StatusOK) |
| 65 | + }) |
| 66 | + |
| 67 | + middleware := StoreAuthHeader() |
| 68 | + handlerToTest := middleware(nextHandler) |
| 69 | + |
| 70 | + req := httptest.NewRequest("GET", "http://testing", nil) |
| 71 | + req.Header.Set(headers.Authorization, "") |
| 72 | + recorder := httptest.NewRecorder() |
| 73 | + |
| 74 | + handlerToTest.ServeHTTP(recorder, req) |
| 75 | + |
| 76 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 77 | +} |
| 78 | + |
| 79 | +func TestStoreAuthHeader_MultipleAuthHeaders(t *testing.T) { |
| 80 | + // Test behavior when multiple authorization headers are present |
| 81 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 | + // Should get the first header value since http.Header.Get returns only the first value |
| 83 | + authFromContext, err := context.GetAuthHeaderFromContext(r.Context()) |
| 84 | + assert.NoError(t, err) |
| 85 | + assert.Equal(t, "Bearer token1", authFromContext) |
| 86 | + |
| 87 | + w.WriteHeader(http.StatusOK) |
| 88 | + }) |
| 89 | + |
| 90 | + middleware := StoreAuthHeader() |
| 91 | + handlerToTest := middleware(nextHandler) |
| 92 | + |
| 93 | + req := httptest.NewRequest("GET", "http://testing", nil) |
| 94 | + req.Header.Add(headers.Authorization, "Bearer token1") |
| 95 | + req.Header.Add(headers.Authorization, "Bearer token2") |
| 96 | + recorder := httptest.NewRecorder() |
| 97 | + |
| 98 | + handlerToTest.ServeHTTP(recorder, req) |
| 99 | + |
| 100 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 101 | +} |
0 commit comments