-
Notifications
You must be signed in to change notification settings - Fork 125
/
not_found_test.go
51 lines (40 loc) · 1.26 KB
/
not_found_test.go
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
package web
import (
"fmt"
"net/http"
"testing"
)
func MyNotFoundHandler(rw ResponseWriter, r *Request) {
rw.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rw, "My Not Found")
}
func (c *Context) HandlerWithContext(rw ResponseWriter, r *Request) {
rw.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rw, "My Not Found With Context")
}
func TestNoHandler(t *testing.T) {
router := New(Context{})
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Not Found", http.StatusNotFound)
}
func TestBadMethod(t *testing.T) {
router := New(Context{})
rw, req := newTestRequest("POOP", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Not Found", http.StatusNotFound)
}
func TestWithHandler(t *testing.T) {
router := New(Context{})
router.NotFound(MyNotFoundHandler)
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Not Found", http.StatusNotFound)
}
func TestWithRootContext(t *testing.T) {
router := New(Context{})
router.NotFound((*Context).HandlerWithContext)
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Not Found With Context", http.StatusNotFound)
}