Skip to content

Commit 4b0370f

Browse files
committed
fix bugs in ApplyStandardMiddleware
1 parent d3800c9 commit 4b0370f

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

request_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,29 @@ func TestPanic(t *testing.T) {
5959
func TestApplyStandardMiddleware(t *testing.T) {
6060
{
6161
h := httperror.ApplyStandardMiddleware(okHandler, myMiddleware)
62-
s, m := testRequest(h, "/")
62+
s, _ := testRequest(h, "/")
6363
assert.Equal(t, 200, s)
64-
assert.Equal(t, "OK\nDid Middleware\n", m, "got middleware output")
6564
}
6665

6766
{
6867
h := httperror.ApplyStandardMiddleware(notFoundHandler, myMiddleware)
6968
s, m := testRequest(h, "/")
7069
assert.Equal(t, 404, s)
71-
assert.Equal(t, "404 Not Found\nDid Middleware\n", m, "got middleware output")
70+
assert.Equal(t, "404 Not Found\n", m, "got correct response status")
71+
}
72+
73+
{
74+
inner := httperror.XApplyStandardMiddleware[string](nameHandler, myMiddleware)
75+
76+
h := httperror.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
77+
return inner(w, r, "Bill")
78+
})
79+
80+
s, m := testRequest(h, "/")
81+
assert.Equal(t, 200, s)
82+
assert.Equal(t, "Hello, Bill\n", m, "got middleware output")
7283
}
84+
7385
}
7486

7587
var getMeOuttaHere = httperror.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
@@ -94,6 +106,14 @@ var notFoundHandler = httperror.HandlerFunc(func(w http.ResponseWriter, _ *http.
94106
return httperror.NotFound
95107
})
96108

109+
var nameHandler = httperror.XHandlerFunc[string](func(w http.ResponseWriter, r *http.Request, name string) error {
110+
w.Header().Set("Content-Type", "text/plain")
111+
112+
fmt.Fprintf(w, "Hello, %s\n", name)
113+
114+
return nil
115+
})
116+
97117
func helloHandler(w http.ResponseWriter, r *http.Request) error {
98118
w.Header().Set("Content-Type", "text/plain")
99119

@@ -128,7 +148,7 @@ func customErrorHandler(w http.ResponseWriter, err error) {
128148

129149
func myMiddleware(h http.Handler) http.Handler {
130150
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
151+
w.Header().Set("Foo", "Bar")
131152
h.ServeHTTP(w, r)
132-
w.Write([]byte("Did Middleware\n"))
133153
})
134154
}

standardmiddleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func XApplyStandardMiddleware[P any](h XHandler[P], m StandardMiddleware) XHandl
2828
*errPtr = err
2929
})
3030

31-
handler = m(h)
31+
handler = m(handler)
3232

3333
return func(w http.ResponseWriter, r *http.Request, p P) error {
3434
var err error
@@ -45,7 +45,6 @@ func XApplyStandardMiddleware[P any](h XHandler[P], m StandardMiddleware) XHandl
4545
// ApplyStandardMiddleware applies middleware written for a standard [http.Handler] to an [httperror.XHandler].
4646
// It works by passing parameters and returning errors through the context.
4747
func ApplyStandardMiddleware(h Handler, m StandardMiddleware) HandlerFunc {
48-
errPtrKey := contextKey("errPtr")
4948

5049
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5150
ctx := r.Context()
@@ -56,12 +55,13 @@ func ApplyStandardMiddleware(h Handler, m StandardMiddleware) HandlerFunc {
5655
*errPtr = err
5756
})
5857

59-
handler = m(h)
58+
handler = m(handler)
6059

6160
return func(w http.ResponseWriter, r *http.Request) error {
6261
var err error
6362
c := r.Context()
6463
c = context.WithValue(c, errPtrKey, &err)
64+
c = context.WithValue(c, paramsKey, "no params")
6565

6666
handler.ServeHTTP(w, r.WithContext(c))
6767

0 commit comments

Comments
 (0)