@@ -59,17 +59,29 @@ func TestPanic(t *testing.T) {
5959func 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\n Did 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\n Did 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
7587var 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+
97117func 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
129149func 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}
0 commit comments