Skip to content

Commit a8dc8ea

Browse files
committed
TESTS: Add routing verb tests.
1 parent d90e771 commit a8dc8ea

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

TODO

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ TODO:
44
- HTTPMethod
55
- HTTPMethods
66
- Code coverage
7-
- setup: Post, Put, Delete, Patch
8-
- show errors middleware
97
- IsRouted, RoutePath
108
- PanicHandler
119
- Invalid handlers?

show_errors_middleware_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestShowErrorsMiddleware(t *testing.T) {
2222
if rw.Code != 500 {
2323
t.Errorf("Expected status code 500 but got %d", rw.Code)
2424
}
25-
25+
2626
body := strings.TrimSpace(string(rw.Body.Bytes()))
2727
if !strings.HasPrefix(body, "<html>") {
2828
t.Errorf("Expected an HTML page but got '%s'", body)

tree_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,40 @@ func TestRoutesWithPrefix(t *testing.T) {
288288
}
289289
}
290290
}
291+
292+
func TestRouteVerbs(t *testing.T) {
293+
router := New(Context{})
294+
router.Get("/a", func(w ResponseWriter, r *Request) {
295+
fmt.Fprintf(w, "GET")
296+
})
297+
router.Put("/a", func(w ResponseWriter, r *Request) {
298+
fmt.Fprintf(w, "PUT")
299+
})
300+
router.Post("/a", func(w ResponseWriter, r *Request) {
301+
fmt.Fprintf(w, "POST")
302+
})
303+
router.Delete("/a", func(w ResponseWriter, r *Request) {
304+
fmt.Fprintf(w, "DELETE")
305+
})
306+
router.Patch("/a", func(w ResponseWriter, r *Request) {
307+
fmt.Fprintf(w, "PATCH")
308+
})
309+
310+
for _, method := range HTTPMethods {
311+
method := string(method)
312+
313+
recorder := httptest.NewRecorder()
314+
request, _ := http.NewRequest(method, "/a", nil)
315+
316+
router.ServeHTTP(recorder, request)
317+
318+
if recorder.Code != 200 {
319+
t.Error("Test:", method, " Didn't get Code=200. Got Code=", recorder.Code)
320+
}
321+
322+
body := strings.TrimSpace(string(recorder.Body.Bytes()))
323+
if body != method {
324+
t.Error("Test:", method, " Didn't get Body=", method, ". Got Body=", body)
325+
}
326+
}
327+
}

web_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func (l nullPanicReporter) Panic(url string, err interface{}, stack string) {
1919
// no op
2020
}
2121
func init() {
22+
// This disables printing panics to stderr during testing, because that is very noisy,
23+
// and we purposefully test some panics.
2224
PanicHandler = nullPanicReporter{}
2325
}
2426

0 commit comments

Comments
 (0)