-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_test.go
185 lines (161 loc) · 4.29 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
175
176
177
178
179
180
181
182
183
184
185
package crater
import (
"errors"
"testing"
)
func TestNewApp(t *testing.T) {
app := NewApp()
if app.craterRequestHandler == nil {
t.Error("request handler shouldn't be nil")
}
if app.htmlTemplates == nil {
t.Error("htmlTemplates shouldn't be nil")
}
if app.settings == nil {
t.Error("settings shouldn't be nil")
}
if app.middleware == nil {
t.Error("middleware shouldn't be nil")
}
}
func TestGet(t *testing.T) {
url := "/url"
app := NewApp()
app.Get(url, func(req *Request, res *Response) {
res.Send("<h1>html</h1>")
})
if len(app.craterRequestHandler.getRoutes) != 1 {
t.Error("get routes should have 1 hander")
}
err := CheckEmptyRoutesRoutes(app.craterRequestHandler.postRoutes,
app.craterRequestHandler.putRoutes, app.craterRequestHandler.deleteRoutes)
if err != nil {
t.Error(err)
}
route := app.craterRequestHandler.getRoutes[0]
if route.routeHandler == nil {
t.Error("Route hander is nil")
}
if !route.pattern.MatchString(url) {
t.Error("Route pattern doesn't match url")
}
}
func TestPost(t *testing.T) {
url := "/url"
app := NewApp()
app.Post(url, func(req *Request, res *Response) {
res.Send("<h1>html</h1>")
})
if len(app.craterRequestHandler.postRoutes) != 1 {
t.Error("post routes should have 1 hander")
}
err := CheckEmptyRoutesRoutes(app.craterRequestHandler.getRoutes,
app.craterRequestHandler.putRoutes, app.craterRequestHandler.deleteRoutes)
if err != nil {
t.Error(err)
}
route := app.craterRequestHandler.postRoutes[0]
if route.routeHandler == nil {
t.Error("Route hander is nil")
}
if !route.pattern.MatchString(url) {
t.Error("Route pattern doesn't match url")
}
}
func TestPut(t *testing.T) {
url := "/url"
app := NewApp()
app.Put(url, func(req *Request, res *Response) {
res.Send("<h1>html</h1>")
})
if len(app.craterRequestHandler.putRoutes) != 1 {
t.Error("put routes should have 1 hander")
}
err := CheckEmptyRoutesRoutes(app.craterRequestHandler.getRoutes,
app.craterRequestHandler.postRoutes, app.craterRequestHandler.deleteRoutes)
if err != nil {
t.Error(err)
}
route := app.craterRequestHandler.putRoutes[0]
if route.routeHandler == nil {
t.Error("Route hander is nil")
}
if !route.pattern.MatchString(url) {
t.Error("Route pattern doesn't match url")
}
}
func TestDelete(t *testing.T) {
url := "/url"
app := NewApp()
app.Delete(url, func(req *Request, res *Response) {
res.Send("<h1>html</h1>")
})
if len(app.craterRequestHandler.deleteRoutes) != 1 {
t.Error("delete routes should have 1 hander")
}
err := CheckEmptyRoutesRoutes(app.craterRequestHandler.getRoutes,
app.craterRequestHandler.postRoutes, app.craterRequestHandler.putRoutes)
if err != nil {
t.Error(err)
}
route := app.craterRequestHandler.deleteRoutes[0]
if route.routeHandler == nil {
t.Error("Route hander is nil")
}
if !route.pattern.MatchString(url) {
t.Error("Route pattern doesn't match url")
}
}
func TestStatic(t *testing.T) {
content := "/content"
app := NewApp()
app.Static("/content")
if len(app.craterRequestHandler.getRoutes) != 1 {
t.Error("get routes should have 1 hander")
}
err := CheckEmptyRoutesRoutes(app.craterRequestHandler.postRoutes,
app.craterRequestHandler.putRoutes, app.craterRequestHandler.deleteRoutes)
if err != nil {
t.Error(err)
}
route := app.craterRequestHandler.getRoutes[0]
if route.routeHandler == nil {
t.Error("Route hander is nil")
}
if !route.pattern.MatchString(content) {
t.Error("Route pattern doesn't match content path")
}
}
func TestUse(t *testing.T) {
app := NewApp()
app.Use(func(req *Request, res *Response) {})
if len(app.middleware) != 1 {
t.Error("middleware should have 1 handler")
}
}
func TestSettings(t *testing.T) {
app := NewApp()
settings := &Settings{
ViewsPath: "./folder",
StaticPath: "./folder",
ViewExtension: "tmpl",
}
app.Settings(settings)
if app.settings.ViewsPath != settings.ViewsPath {
t.Error("ViewsPath was not set correctly")
}
if app.settings.StaticPath != settings.StaticPath {
t.Error("StaticFilesPath was not set correctly")
}
if app.settings.ViewExtension != settings.ViewExtension {
t.Error("ViewExtension was not set correctly")
}
}
func CheckEmptyRoutesRoutes(r ...[]*route) error {
for i := 0; i < len(r); i++ {
if len(r[i]) != 0 {
return errors.New("routes should be empty")
}
}
return nil
}