-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes_test.go
143 lines (127 loc) · 3.91 KB
/
routes_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
132
133
134
135
136
137
138
139
140
141
142
143
package goRouter
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var HandlerOk = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
w.WriteHeader(http.StatusOK)
}
var HandlerErr = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusBadRequest)
}
var handler = GetMuxInstance()
func TestRouteSetting(t *testing.T) {
handler.Get(`/user/:username/:id(\d+)\.:format(\w+)`, HandlerOk)
handler.Get(`/user/:id(\d+)`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("get user"))
})
handler.Get(`/user/:id(\d+)/create`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("get create user form"))
})
handler.Get(`/user/:id(\d+)/edit`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("get edit user form"))
})
handler.Get(`/user/`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("get users"))
})
handler.Post(`/user/`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("create user"))
})
handler.Put(`/user/:id(\d+)`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("update user"))
})
handler.Delete(`/user/:id(\d+)`, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("delete user"))
})
}
func TestRouteParams(t *testing.T) {
r, _ := http.NewRequest("GET", "/user/barbery/123.json?learn=kungfu", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
var (
username = r.URL.Query().Get(":username")
id = r.URL.Query().Get(":id")
format = r.URL.Query().Get(":format")
learnParam = r.URL.Query().Get("learn")
)
if username != "barbery" {
t.Errorf("url param set to [%s]; want [%s]", username, "barbery")
}
if id != "123" {
t.Errorf("url param set to [%s]; want [%s]", id, "123")
}
if learnParam != "kungfu" {
t.Errorf("url param set to [%s]; want [%s]", learnParam, "kungfu")
}
if format != "json" {
t.Errorf("url param set to [%s]; want [%s]", format, "json")
}
}
func TestGetMethod(t *testing.T) {
r, _ := http.NewRequest("GET", "/user/1", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
body := w.Body.String()
if body != "get user" {
t.Errorf("url content set to [%s]; want [%s]", body, "get user")
}
r, _ = http.NewRequest("GET", "/user/1/create", nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
body = w.Body.String()
if body != "get create user form" {
t.Errorf("url content set to [%s]; want [%s]", body, "get create user form")
}
r, _ = http.NewRequest("GET", "/user/1/edit", nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
body = w.Body.String()
if body != "get edit user form" {
t.Errorf("url content set to [%s]; want [%s]", body, "get edit user form")
}
r, _ = http.NewRequest("GET", "/user", nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
body = w.Body.String()
if body != "get users" {
t.Errorf("url content set to [%s]; want [%s]", body, "get users")
}
}
func TestPostMethod(t *testing.T) {
r, _ := http.NewRequest("POST", "/user/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
body := w.Body.String()
if body != "create user" {
t.Errorf("url content set to [%s]; want [%s]", body, "create user")
}
}
func TestPutMethod(t *testing.T) {
r, _ := http.NewRequest("PUT", "/user/789", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
body := w.Body.String()
id := r.URL.Query().Get(":id")
if id != "789" {
t.Errorf("url param set to [%s]; want [%s]", id, "789")
}
if body != "update user" {
t.Errorf("url content set to [%s]; want [%s]", body, "update user")
}
}
func TestDeleteMethod(t *testing.T) {
r, _ := http.NewRequest("DELETE", "/user/456", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
body := w.Body.String()
id := r.URL.Query().Get(":id")
if id != "456" {
t.Errorf("url param set to [%s]; want [%s]", id, "456")
}
if body != "delete user" {
t.Errorf("url content set to [%s]; want [%s]", body, "delete user")
}
}