-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
131 lines (111 loc) · 3.85 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package pine
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNodeTraversal(t *testing.T) {
root := &node{
Path: "", // this should match anything
children: []*node{},
Name: "root",
}
root.children = append(root.children, &node{
Path: "hello",
Name: "hello folder",
children: []*node{{
Path: "", // this should match anything
Name: "hello name handler",
children: []*node{{
Method: "GET",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome to the forest"))
}),
Name: "get handler",
}, {
Method: "POST",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("thank you for the mail"))
}),
Name: "post handler",
}},
}, {
Method: "GET",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello there!"))
}),
}},
})
req := httptest.NewRequest("GET", "https://lukaswerner.com/hello/world", nil)
handler := findMatchingHandler(root, req)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result()
if b, _ := io.ReadAll(resp.Body); bytes.Compare(b, []byte("welcome to the forest")) != 0 {
t.Errorf("the body response did not match the expected body: %s != %s", string(b), "welcome to the forest")
}
req = httptest.NewRequest("POST", "https://lukaswerner.com/hello/world", nil)
handler = findMatchingHandler(root, req)
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp = w.Result()
if b, _ := io.ReadAll(resp.Body); bytes.Compare(b, []byte("thank you for the mail")) != 0 {
t.Errorf("the body response did not match the expected body: %s != %s", string(b), "thank you for the mail")
}
req = httptest.NewRequest("GET", "https://lukaswerner.com/hello", nil)
handler = findMatchingHandler(root, req)
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp = w.Result()
if b, _ := io.ReadAll(resp.Body); bytes.Compare(b, []byte("hello there!")) != 0 {
t.Errorf("the body response did not match the expected body: %s != %s", string(b), "hello there!")
}
}
func TestInsertingNodes(t *testing.T) {
p := New()
p.Handle("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world!"))
})
req := httptest.NewRequest("GET", "https://lukaswerner.com/hello/world", nil)
handler := findMatchingHandler(p.RootNode, req)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result()
if b, _ := io.ReadAll(resp.Body); bytes.Compare(b, []byte("hello world!")) != 0 {
t.Errorf("the body response did not match the expected body: %q != %q", string(b), "hello world!")
}
}
func TestPathVarsInsertion(t *testing.T) {
p := New()
p.Handle("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world!"))
})
req := httptest.NewRequest("GET", "https://lukaswerner.com/hello/world", nil)
vars := makeVarsFromRequest(p.RootNode, req)
if len(vars) > 1 || len(vars) <= 0 {
t.Errorf("vars was %d not the correct size as expected %d", len(vars), 1)
}
if vars["name"] != "world" {
t.Errorf("value for \"name\" was not what was expected: %q got: %q", "world", vars["name"])
}
}
func TestPathVarsUsage(t *testing.T) {
value := ""
p := New()
p.Handle("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
value = Var(r, "name")
w.Write([]byte("hello " + value + "!"))
})
req := httptest.NewRequest("GET", "https://lukaswerner.com/hello/world", nil)
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
resp := w.Result()
if b, _ := io.ReadAll(resp.Body); bytes.Compare(b, []byte("hello world!")) != 0 {
t.Errorf("the body response did not match the expected body: %s != %s", string(b), "hello world!")
}
if value != "world" {
t.Errorf("answer for path variable loading was not what was expected: %s but got %s instead", "world", value)
}
}