This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
router_test.go
76 lines (60 loc) · 1.65 KB
/
router_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
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
package goat
import (
"net/http/httptest"
"reflect"
"testing"
)
var emptyHandler Handle
func TestSubPath(t *testing.T) {
r := New()
out := r.subPath("/foo")
exp := "/foo"
if out != exp {
t.Errorf("subPath should return %s, but did return %s", exp, out)
}
}
func TestAddRoute(t *testing.T) {
r := New()
r.addRoute("GET", "/test", "", emptyHandler)
expected := make(map[string]string)
if !reflect.DeepEqual(r.index, expected) {
t.Errorf("addRoute with empty title should modify Router.index to %v, but it's %v", expected, r.index)
}
r.addRoute("GET", "/foo/bar", "foo_bar_url", emptyHandler)
expected = map[string]string{
"foo_bar_url": "/foo/bar",
}
if !reflect.DeepEqual(r.index, expected) {
t.Errorf("addRoute should modify Router.index to %v, but did modify it to %v", expected, r.index)
}
}
func TestSubrouter(t *testing.T) {
pre := "/user"
r := New()
sr := r.Subrouter(pre)
if sr.prefix != pre {
t.Errorf("Subrouter should set the prefix %s, but did set %s", pre, sr.prefix)
}
if sr.parent != r {
t.Errorf("Subrouter should set %v as parent router, but did set %v", r, sr.parent)
}
if r.children[len(r.children)-1] != sr {
t.Errorf("Subrouter should add %v to children of %v, but didn't", sr, r)
}
}
func TestNotFoundHandler(t *testing.T) {
w := httptest.NewRecorder()
r := New()
r.notFoundHandler(w, nil)
expCode := 404
if w.Code != expCode {
t.Errorf("NotFoundHandler should set the status code to %i, but didn't", expCode)
}
expBody := `{
"error": "404 Not Found"
}`
body := string(w.Body.Bytes())
if body != expBody {
t.Errorf("NotFoundHandler set the Body to %s, but should set it to %s", body, expBody)
}
}