|
| 1 | +package errhandler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +type contextKey string |
| 11 | + |
| 12 | +func TestMiddleware(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + handler Wrap |
| 16 | + middleware Middleware |
| 17 | + expectedStatus int |
| 18 | + expectedBody string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "single middleware", |
| 22 | + middleware: func(n Wrap) Wrap { |
| 23 | + return func(w http.ResponseWriter, r *http.Request) error { |
| 24 | + ctx := context.WithValue(r.Context(), contextKey("value"), 1) |
| 25 | + r = r.WithContext(ctx) |
| 26 | + |
| 27 | + return n(w, r) |
| 28 | + } |
| 29 | + }, |
| 30 | + handler: Wrap(func(w http.ResponseWriter, r *http.Request) error { |
| 31 | + m := map[string]any{ |
| 32 | + "value": r.Context().Value(contextKey("value")), |
| 33 | + } |
| 34 | + |
| 35 | + return SendJSON(w, m) |
| 36 | + }), |
| 37 | + expectedStatus: http.StatusOK, |
| 38 | + expectedBody: "{\"value\":1}\n", |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + for _, tt := range tests { |
| 43 | + t.Run(tt.name, func(t *testing.T) { |
| 44 | + req := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil) |
| 45 | + w := httptest.NewRecorder() |
| 46 | + |
| 47 | + tt.middleware(tt.handler).ServeHTTP(w, req) |
| 48 | + |
| 49 | + res := w.Result() |
| 50 | + defer res.Body.Close() |
| 51 | + |
| 52 | + if res.StatusCode != tt.expectedStatus { |
| 53 | + t.Errorf("expected status %d, got %d", tt.expectedStatus, res.StatusCode) |
| 54 | + } |
| 55 | + |
| 56 | + actualBody := w.Body.String() |
| 57 | + if actualBody != tt.expectedBody { |
| 58 | + t.Errorf("expected body %q, got %q", tt.expectedBody, actualBody) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestChain(t *testing.T) { |
| 65 | + tests := []struct { |
| 66 | + name string |
| 67 | + handler Wrap |
| 68 | + middlewares []Middleware |
| 69 | + expectedStatus int |
| 70 | + expectedBody string |
| 71 | + }{ |
| 72 | + { |
| 73 | + name: "multiple middlewares", |
| 74 | + middlewares: []Middleware{ |
| 75 | + func(n Wrap) Wrap { |
| 76 | + return func(w http.ResponseWriter, r *http.Request) error { |
| 77 | + ctx := context.WithValue(r.Context(), contextKey("value"), 1) |
| 78 | + r = r.WithContext(ctx) |
| 79 | + |
| 80 | + return n(w, r) |
| 81 | + } |
| 82 | + }, |
| 83 | + func(n Wrap) Wrap { |
| 84 | + return func(w http.ResponseWriter, r *http.Request) error { |
| 85 | + value, ok := r.Context().Value(contextKey("value")).(int) |
| 86 | + if !ok { |
| 87 | + t.Fatalf("expected integer but didn't get one") |
| 88 | + } |
| 89 | + |
| 90 | + ctx := context.WithValue(r.Context(), contextKey("value"), value+2) |
| 91 | + r = r.WithContext(ctx) |
| 92 | + |
| 93 | + return n(w, r) |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + handler: Wrap(func(w http.ResponseWriter, r *http.Request) error { |
| 98 | + m := map[string]any{ |
| 99 | + "value": r.Context().Value(contextKey("value")), |
| 100 | + } |
| 101 | + |
| 102 | + return SendJSON(w, m) |
| 103 | + }), |
| 104 | + expectedStatus: http.StatusOK, |
| 105 | + expectedBody: "{\"value\":3}\n", |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tt := range tests { |
| 110 | + t.Run(tt.name, func(t *testing.T) { |
| 111 | + req := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil) |
| 112 | + w := httptest.NewRecorder() |
| 113 | + |
| 114 | + chain := Chain(tt.middlewares...) |
| 115 | + chain(tt.handler).ServeHTTP(w, req) |
| 116 | + |
| 117 | + res := w.Result() |
| 118 | + defer res.Body.Close() |
| 119 | + |
| 120 | + if res.StatusCode != tt.expectedStatus { |
| 121 | + t.Errorf("expected status %d, got %d", tt.expectedStatus, res.StatusCode) |
| 122 | + } |
| 123 | + |
| 124 | + actualBody := w.Body.String() |
| 125 | + if actualBody != tt.expectedBody { |
| 126 | + t.Errorf("expected body %q, got %q", tt.expectedBody, actualBody) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments