-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoose_test.go
145 lines (118 loc) · 3.3 KB
/
goose_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
package goose
import (
"net/http"
"strconv"
"strings"
"testing"
"time"
)
func TestRun(t *testing.T) {
baseURL := "http://127.0.0.1"
port := ":8080"
g := New()
go func() {
g.GET("/", func(ctx *Context) {
ctx.String("Main Page!")
})
g.GET("/goose", func(ctx *Context) {
name := "goose"
ctx.String("I love %s!", name)
})
if err := g.Run(port); err != nil {
t.Fatal(err)
}
}()
// wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(200 * time.Millisecond)
url := baseURL + port
testResponseBody(t, url, http.StatusOK, "Main Page!")
url = baseURL + port + "/goose"
testResponseBody(t, url, http.StatusOK, "I love goose!")
}
func TestColonWildcardRouting(t *testing.T) {
g := New()
g.GET("/:name", func(ctx *Context) {
ctx.String("I love %s!", ctx.Param("name"))
})
g.GET("/category/:category", func(ctx *Context) {
ctx.String("Category: %s", ctx.Param("category"))
})
want := "I love goose!"
testResponseBodyLocal(t, g, "/goose", http.StatusOK, want)
want = "Category: history"
testResponseBodyLocal(t, g, "/category/history", http.StatusOK, want)
}
func TestRoutingWithAndWithoutTrailingSlash(t *testing.T) {
g := New()
g.GET("/info", func(ctx *Context) {
ctx.String("Information page")
})
g.GET("/info/", func(ctx *Context) {
ctx.String("Trailing information page")
})
g.GET("/animal/:name", func(ctx *Context) {
ctx.String("I love %s!", ctx.Param("name"))
})
g.GET("/animal/:name/", func(ctx *Context) {
ctx.String("I love %s 3000 times!", ctx.Param("name"))
})
want := "Information page"
testResponseBodyLocal(t, g, "/info", http.StatusOK, want)
want = "Trailing information page"
testResponseBodyLocal(t, g, "/info/", http.StatusOK, want)
want = "I love goose!"
testResponseBodyLocal(t, g, "/animal/goose", http.StatusOK, want)
want = "I love goose 3000 times!"
testResponseBodyLocal(t, g, "/animal/goose/", http.StatusOK, want)
}
func TestRouterGroup(t *testing.T) {
g := New()
g.GET("/", func(ctx *Context) {
ctx.String("Root page")
})
v1 := g.Group("v1")
{
v1.GET("/", func(ctx *Context) {
ctx.String("Group V1 is here!")
})
v1.GET("/hello", func(ctx *Context) {
ctx.String("Hello Group V1!")
})
v2 := v1.Group("v2")
{
v2.GET("/hello", func(ctx *Context) {
ctx.String("Hello Group V2!")
})
}
}
want := "Root page"
testResponseBodyLocal(t, g, "/", http.StatusOK, want)
want = "Group V1 is here!"
testResponseBodyLocal(t, g, "/v1/", http.StatusOK, want)
want = "Hello Group V1!"
testResponseBodyLocal(t, g, "/v1/hello", http.StatusOK, want)
want = "Hello Group V2!"
testResponseBodyLocal(t, g, "/v1/v2/hello", http.StatusOK, want)
}
func TestTemplate(t *testing.T) {
g := New()
g.FuncMap(X{
"appendYear": func(s string) string {
year := time.Now().Year()
return strings.Join([]string{s, strconv.Itoa(year)}, " - ")
},
})
g.Set("toUpper", strings.ToUpper)
g.LoadHTMLGlob("testfiles/templates/*")
g.GET("/t", func(ctx *Context) {
ctx.HTML("hello.tmpl", X{"name": "Goose"})
})
g.GET("/func", func(ctx *Context) {
ctx.HTML("funcmaps.tmpl", X{"msg": "I love goose!"})
})
want := "<h1>Hello Goose</h1>"
testResponseBodyLocal(t, g, "/t", http.StatusOK, want)
want = "I LOVE GOOSE! - 2020"
testResponseBodyLocal(t, g, "/func", http.StatusOK, want)
}