-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtestflight_test.go
143 lines (116 loc) · 3.68 KB
/
testflight_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 testflight
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/bmizerany/assert"
"github.com/bmizerany/pat"
)
type person struct {
Name string `json:"name"`
}
func handler() http.Handler {
m := pat.New()
m.Get("/hello/:name", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, "+req.URL.Query().Get(":name"))
}))
m.Post("/post/json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
person := &person{}
body, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(body, person)
w.WriteHeader(201)
io.WriteString(w, person.Name+" created")
}))
m.Post("/post/form", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
name := req.Form.Get("name")
w.WriteHeader(201)
io.WriteString(w, name+" created")
}))
m.Put("/put/json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
person := &person{}
body, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(body, person)
w.WriteHeader(200)
io.WriteString(w, person.Name+" updated")
}))
m.Add("PATCH", "/patch/json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
person := &person{}
body, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(body, person)
w.WriteHeader(200)
io.WriteString(w, person.Name+" updated")
}))
m.Del("/delete/json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
person := &person{}
body, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(body, person)
w.WriteHeader(200)
io.WriteString(w, person.Name+" deleted")
}))
return m
}
func TestGet(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Get("/hello/drew")
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "hello, drew", response.Body)
assert.Equal(t, []byte("hello, drew"), response.RawBody)
})
}
func TestPostWithJson(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Post("/post/json", JSON, `{"name": "Drew"}`)
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Drew created", response.Body)
})
}
func TestPostWithForm(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Post("/post/form", FORM_ENCODED, "name=Drew")
assert.Equal(t, 201, response.StatusCode)
assert.Equal(t, "Drew created", response.Body)
})
}
func TestPut(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Put("/put/json", JSON, `{"name": "Drew"}`)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "Drew updated", response.Body)
})
}
func TestPatch(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Patch("/patch/json", JSON, `{"name": "Yograterol"}`)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "Yograterol updated", response.Body)
})
}
func TestDelete(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Delete("/delete/json", JSON, `{"name": "Drew"}`)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "Drew deleted", response.Body)
})
}
func TestResponeHeaders(t *testing.T) {
WithServer(handler(), func(r *Requester) {
response := r.Get("/hello/again_drew")
assert.Equal(t, 200, response.StatusCode)
header := response.Header
assert.NotEqual(t, nil, header)
assert.Equal(t, "text/plain; charset=utf-8", header.Get("Content-Type"))
})
}
func TestDo(t *testing.T) {
WithServer(handler(), func(r *Requester) {
request, _ := http.NewRequest("DELETE", "/delete/json", strings.NewReader(`{"name": "Drew"}`))
request.Header.Add("Content-Type", JSON)
response := r.Do(request)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "Drew deleted", response.Body)
})
}