|
| 1 | +package restapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestRequestIDMiddleware(t *testing.T) { |
| 13 | + t.Run("should generate request ID if missing", func(t *testing.T) { |
| 14 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | + reqID, ok := r.Context().Value(RequestIDKey).(string) |
| 16 | + assert.True(t, ok, "Context should contain request ID") |
| 17 | + assert.NotEmpty(t, reqID, "Request ID should not be empty") |
| 18 | + }) |
| 19 | + |
| 20 | + handlerToTest := RequestIDMiddleware(nextHandler) |
| 21 | + |
| 22 | + req := httptest.NewRequest("GET", "http://example.com/foo", nil) |
| 23 | + rec := httptest.NewRecorder() |
| 24 | + |
| 25 | + handlerToTest.ServeHTTP(rec, req) |
| 26 | + |
| 27 | + respID := rec.Header().Get("X-Request-ID") |
| 28 | + assert.NotEmpty(t, respID, "Response header should contain X-Request-ID") |
| 29 | + assert.Regexp(t, `^[0-9a-f-]{36}$`, respID) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("should preserve existing valid request ID", func(t *testing.T) { |
| 33 | + existingID := "my-custom-trace-id-123" |
| 34 | + |
| 35 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | + reqID, ok := r.Context().Value(RequestIDKey).(string) |
| 37 | + assert.True(t, ok) |
| 38 | + assert.Equal(t, existingID, reqID) |
| 39 | + }) |
| 40 | + |
| 41 | + handlerToTest := RequestIDMiddleware(nextHandler) |
| 42 | + |
| 43 | + req := httptest.NewRequest("GET", "http://example.com/foo", nil) |
| 44 | + req.Header.Set("X-Request-ID", existingID) |
| 45 | + rec := httptest.NewRecorder() |
| 46 | + |
| 47 | + handlerToTest.ServeHTTP(rec, req) |
| 48 | + |
| 49 | + assert.Equal(t, existingID, rec.Header().Get("X-Request-ID")) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("should replace invalid request ID", func(t *testing.T) { |
| 53 | + testCases := []struct { |
| 54 | + name string |
| 55 | + invalidID string |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "ID too long (>128 chars)", |
| 59 | + invalidID: strings.Repeat("a", 129), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "ID contains invalid characters", |
| 63 | + invalidID: "bad-id-<script>", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tc := range testCases { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 70 | + reqID, ok := r.Context().Value(RequestIDKey).(string) |
| 71 | + assert.True(t, ok) |
| 72 | + assert.NotEqual(t, tc.invalidID, reqID) |
| 73 | + assert.Regexp(t, `^[0-9a-f-]{36}$`, reqID) |
| 74 | + }) |
| 75 | + |
| 76 | + handlerToTest := RequestIDMiddleware(nextHandler) |
| 77 | + |
| 78 | + req := httptest.NewRequest("GET", "http://example.com/foo", nil) |
| 79 | + req.Header.Set("X-Request-ID", tc.invalidID) |
| 80 | + rec := httptest.NewRecorder() |
| 81 | + |
| 82 | + handlerToTest.ServeHTTP(rec, req) |
| 83 | + }) |
| 84 | + } |
| 85 | + }) |
| 86 | +} |
0 commit comments