-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp_test.go
111 lines (83 loc) · 1.96 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
package app
import . "github.com/franela/go-supertest"
import "net/http/httptest"
import "net/http"
import "testing"
import "fmt"
// test GET
func TestGet(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Get("/").
Expect(200, "hello")
}
// test HEAD
func TestHead(t *testing.T) {
app := New()
app.Head("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Head("/").
Expect(200)
}
// test HEAD for GET route
func TestHeadGet(t *testing.T) {
app := New()
app.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Head("/").
Expect(200)
}
// test route precedence
func TestPrecedence(t *testing.T) {
app := New()
app.Get("/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
app.Get("/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("world"))
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Get("/foo").
Expect(200, "hello")
}
// test many routes
func TestMany(t *testing.T) {
app := New()
app.Get("/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
})
app.Get("/bar", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("world"))
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Get("/foo").
Expect(200, "hello")
NewRequest(s.URL).
Get("/bar").
Expect(200, "world")
}
// test params
func TestParams(t *testing.T) {
app := New()
app.Get("/user/:name/pet/:pet", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
pet := r.URL.Query().Get(":pet")
fmt.Fprint(w, "user %s's pet %s", name, pet)
})
s := httptest.NewServer(app)
NewRequest(s.URL).
Get("/user/tobi/pet/loki").
Expect(200, "user tobi's pet loki")
}