-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmid_test.go
More file actions
132 lines (122 loc) · 3.42 KB
/
mid_test.go
File metadata and controls
132 lines (122 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package hx_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/struct0x/hx"
)
func TestAdaptMiddleware(t *testing.T) {
type check func(t *testing.T, wr *httptest.ResponseRecorder)
checks := func(ch ...check) []check { return ch }
hasStatus := func(status int) check {
return func(t *testing.T, wr *httptest.ResponseRecorder) {
t.Helper()
if wr.Code != status {
t.Errorf("expected status %d, got %d", status, wr.Code)
}
}
}
hasBody := func(body string) check {
return func(t *testing.T, wr *httptest.ResponseRecorder) {
t.Helper()
if wr.Body.String() != body {
t.Errorf("expected body %q, got %q", body, wr.Body.String())
}
}
}
hasHeader := func(key, value string) check {
return func(t *testing.T, wr *httptest.ResponseRecorder) {
t.Helper()
if got := wr.Header().Get(key); got != value {
t.Errorf("header %q: expected %q, got %q", key, value, got)
}
}
}
tests := []struct {
name string
middleware func(http.Handler) http.Handler
handler hx.HandlerFunc
checks []check
}{
{
name: "passthrough_propagates_handler_response",
middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
},
handler: func(ctx context.Context, r *http.Request) error {
return hx.OK("hello")
},
checks: checks(
hasStatus(http.StatusOK),
),
},
{
name: "passthrough_sets_header_before_next",
middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-From-Middleware", "yes")
next.ServeHTTP(w, r)
})
},
handler: func(ctx context.Context, r *http.Request) error {
return hx.OK("hello")
},
checks: checks(
hasStatus(http.StatusOK),
hasHeader("X-From-Middleware", "yes"),
),
},
{
name: "shortcircuit_via_write_preserves_response",
middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte("rate limited"))
// does not call next
})
},
handler: func(ctx context.Context, r *http.Request) error {
t.Fatal("handler should not be called when middleware short-circuits")
return nil
},
checks: checks(
hasStatus(http.StatusTooManyRequests),
hasBody("rate limited"),
),
},
{
name: "context_enrichment_is_visible_to_handler",
middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "key", "injected")
next.ServeHTTP(w, r.WithContext(ctx))
})
},
handler: func(ctx context.Context, r *http.Request) error {
val, _ := r.Context().Value("key").(string)
if val != "injected" {
t.Errorf("expected context value %q, got %q", "injected", val)
}
return hx.OK("ok")
},
checks: checks(
hasStatus(http.StatusOK),
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := hx.New(hx.WithCustomMux(http.NewServeMux()))
server.HandleFunc("/", tt.handler, hx.AdaptMiddleware(tt.middleware))
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
server.ServeHTTP(w, req)
for _, check := range tt.checks {
check(t, w)
}
})
}
}