forked from drewolson/testflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestflight_test.go
More file actions
115 lines (93 loc) · 2.88 KB
/
testflight_test.go
File metadata and controls
115 lines (93 loc) · 2.88 KB
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
package testflight
import (
"encoding/json"
"github.com/bmizerany/assert"
"github.com/bmizerany/pat"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
)
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.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 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 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)
})
}