This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_test.go
174 lines (138 loc) · 4.19 KB
/
app_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package kwiscale
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
// Let each handler to get "*testing.T"
var T = make(map[*App]*testing.T)
// A test handler (simple).
type TestHandler struct {
RequestHandler
}
func (th *TestHandler) Get() {
th.WriteString("Hello")
}
// A test handler with method that handle paramters
type TestParamHandler struct{ RequestHandler }
func (th *TestParamHandler) Get(id int, name string, activated bool) {
th.WriteString(fmt.Sprintf("%d %s %v", id, name, activated))
}
// Handler to test reversed route.
type TestReverseRoute struct{ RequestHandler }
func (th *TestReverseRoute) Get() {
// Test to get route from app
app := th.App()
t := T[app]
u, err := app.GetRoute("kwiscale.TestReverseRoute").URL("category", "test")
if err != nil {
t.Error("Route from app returns error:", err)
}
if u.String() != "/product/test" {
t.Error("Route from app is not /product/test: ", u)
}
route, err := th.URL("category", "foo")
if err != nil {
fmt.Println(err)
}
th.WriteString(route.String())
}
// Create app.
func initApp(t *testing.T) *App {
conf := &Config{}
app := NewApp(conf)
T[app] = t
return app
}
// Test the app "soft close".
func TestCloser(t *testing.T) {
app := initApp(t)
app.AddRoute("/foo", &TestHandler{})
<-app.SoftStop()
}
// TestSimpleRequest should respond whit 200 and print "hello".
func TestSimpleRequest(t *testing.T) {
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
app := initApp(t)
app.AddRoute("/foo", &TestHandler{})
app.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Error("HTTP Status is not ok: ", w.Code)
}
resp, _ := ioutil.ReadAll(w.Body)
if string(resp) != "Hello" {
t.Error("Handler didn't respond with 'hello': ", string(resp))
}
}
// Try to call a bad route.
func TestBadRequest(t *testing.T) {
r, _ := http.NewRequest("GET", "http://example.com/bad", nil)
w := httptest.NewRecorder()
app := initApp(t)
app.AddRoute("/foo", &TestHandler{})
app.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Error(`HTTP Status is not "not found": `, w.Code)
}
}
// Test the reverse route to get url.
func TestRouteReverse(t *testing.T) {
r, _ := http.NewRequest("GET", "http://example.com/product/test", nil)
w := httptest.NewRecorder()
app := initApp(t)
app.AddRoute("/product/{category:.+}", &TestReverseRoute{})
app.ServeHTTP(w, r)
resp, _ := ioutil.ReadAll(w.Body)
if string(resp) != "/product/foo" {
t.Fatal(resp, "!=", "/product/foo")
}
}
// Test to get reverse route from app.
func TestReverseURLFromApp(t *testing.T) {
app := initApp(t)
app.AddRoute("/product/{category:.+}", &TestReverseRoute{})
u, err := app.GetRoute("kwiscale.TestReverseRoute").URL("category", "second")
if err != nil {
t.Fatal(err)
}
if u.String() != "/product/second" {
t.Fatal(u.String(), "!=", "/product/second")
}
}
// BUG: This is a limit case we really need to study
func _TestBestRoute(t *testing.T) {
r, _ := http.NewRequest("GET", "http://example.com/test/route", nil)
//w := httptest.NewRecorder()
app := initApp(t)
app.AddRoute("/test/route", &TestHandler{})
app.AddRoute("/{p:.*}", &TestReverseRoute{})
name, _, _ := getBestRoute(app, r)
if name != "kwiscale.TestReverseRoute" {
t.Fatal("For /test/route, the handler that matches should be kwiscale.TestReverseRoute and not", name)
}
}
func TestMethodWithArguments(t *testing.T) {
app := initApp(t)
app.AddRoute(`/user/{id:\d+}/{name:.*}/{activated:.*}`, &TestParamHandler{})
r, _ := http.NewRequest("GET", "http://example.com/user/42/foo/true", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
content, err := ioutil.ReadAll(w.Body)
if string(content) != "42 foo true" {
t.Fatal("Bad argument match:", string(content), "instead of 42 foo true", err)
}
}
func TestMethodWithBadArgument(t *testing.T) {
app := initApp(t)
app.AddRoute(`/user/{id:\d+}/{name:.*}/{activated:.*}`, &TestParamHandler{})
r, _ := http.NewRequest("GET", "http://example.com/user/42/foo/BAR", nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
content, err := ioutil.ReadAll(w.Body)
if w.Code != 500 {
t.Fatal("Handler should crash with 500 Error for bad match parameter, we've got", w.Code, content, err)
}
}