-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
route_test.go
87 lines (70 loc) · 1.85 KB
/
route_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
77
78
79
80
81
82
83
84
85
86
87
package gorouter
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/vardius/gorouter/v4/context"
"github.com/vardius/gorouter/v4/middleware"
)
func TestRouter(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if _, err := w.Write([]byte("4")); err != nil {
t.Fatal(err)
}
})
buildMiddlewareFunc := func(body string) middleware.Middleware {
fn := func(h middleware.Handler) middleware.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(body)); err != nil {
t.Fatal(err)
}
h.(http.Handler).ServeHTTP(w, r)
})
}
return middleware.WrapperFunc(fn)
}
m1 := buildMiddlewareFunc("1")
m2 := buildMiddlewareFunc("2")
m3 := buildMiddlewareFunc("3")
r := newRoute(handler)
m := middleware.NewCollection(m1, m2, m3)
h := m.Compose(r.Handler())
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
h.(http.Handler).ServeHTTP(w, req)
if w.Body.String() != "1234" {
t.Errorf("The router doesn't work correctly. Expected 1234, Actual: %s", w.Body.String())
}
}
func TestParams(t *testing.T) {
param := context.Param{Key: "key", Value: "value"}
params := context.Params{param}
if params.Value("key") != "value" {
t.Error("Invalid params value")
}
}
func TestInvalidParams(t *testing.T) {
param := context.Param{Key: "key", Value: "value"}
params := context.Params{param}
if params.Value("invalid_key") != "" {
t.Error("Invalid params value")
}
}
func TestNilHandler(t *testing.T) {
panicked := false
defer func() {
if rcv := recover(); rcv != nil {
panicked = true
}
}()
r := newRoute(nil)
if h := r.Handler(); h != nil {
t.Error("Handler should be equal nil")
}
if panicked != true {
t.Error("Router should panic if handler is nil")
}
}