-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrequest_config_test.go
185 lines (167 loc) · 4.53 KB
/
request_config_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 forest
import (
"encoding/hex"
"io/ioutil"
"net/url"
"strings"
"testing"
)
type car struct {
Brand string
Hp int
}
func TestThatContentIsMarshalledToXml(t *testing.T) {
conf := Path("/")
c := car{
Brand: "Tesla",
Hp: 500,
}
conf.Content(c, "application/xml")
if conf.BodyReader == nil {
t.Fatalf("expected body reader, got nil")
}
if conf.HeaderMap.Get("Content-Type") != "application/xml" {
t.Fatalf("expected xml content-type")
}
data, _ := ioutil.ReadAll(conf.BodyReader)
if string(data) != "<car><Brand>Tesla</Brand><Hp>500</Hp></car>" {
t.Errorf("expected xml document, got:%s", string(data))
}
}
func TestThatContentIsMarshalledToJson(t *testing.T) {
conf := Path("/")
c := car{
Brand: "Tesla",
Hp: 500,
}
conf.Content(c, "application/json")
if conf.BodyReader == nil {
t.Fatalf("expected body reader, got nil")
}
if conf.HeaderMap.Get("Content-Type") != "application/json" {
t.Fatalf("expected json content-type")
}
data, _ := ioutil.ReadAll(conf.BodyReader)
if string(data) != `{"Brand":"Tesla","Hp":500}` {
t.Errorf("expected json document, got:%s", string(data))
}
}
func TestThatContentIsMarshalledToPlainText(t *testing.T) {
conf := Path("/")
conf.Content("hello", "text/plain;charset=utf8")
if conf.BodyReader == nil {
t.Fatalf("expected body reader, got nil")
}
if conf.HeaderMap.Get("Content-Type") != "text/plain;charset=utf8" {
t.Fatalf("expected plain content-type")
}
data, _ := ioutil.ReadAll(conf.BodyReader)
if string(data) != "hello" {
t.Errorf("expected plain document, got:%s", string(data))
}
}
func TestThatContentCanBeBytes(t *testing.T) {
conf := Path("/")
conf.Content([]byte{1, 2, 3, 4}, "application/octet-stream")
if conf.BodyReader == nil {
t.Fatalf("expected body reader, got nil")
}
if conf.HeaderMap.Get("Content-Type") != "application/octet-stream" {
t.Fatalf("expected octet content-type")
}
data, _ := ioutil.ReadAll(conf.BodyReader)
if data[0] != 1 || data[3] != 4 {
t.Errorf("expected 1,2,3,4 bytes, got:%v", hex.Dump(data))
}
}
func setXHeader(r *RequestConfig) {
r.Header("X", "Y")
}
func TestThatCustomDoIsCalled(t *testing.T) {
conf := Path("/")
conf.Do(setXHeader)
if conf.HeaderMap.Get("X") != "Y" {
t.Fail()
}
}
func TestThatPathCanBeOverriden(t *testing.T) {
conf := Path("/a")
conf.Path("/b")
if conf.URI != "/b" {
t.Errorf("got %v want %v", conf.URI, "/b")
}
}
func TestThatOneQueryParameterCanBeAdded(t *testing.T) {
conf := NewConfig("/a")
conf.Query("b", 100)
if got, want := conf.pathAndQuery(), "/a?b=100"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
func TestThatQueryParametersCanBeAddedToTheUri(t *testing.T) {
conf := Path("/test")
conf.Query("zoom", true)
conf.Query("scale", 1)
conf.Query("slash", "/")
if got, want := conf.pathAndQuery(), "/test?scale=1&slash=%2F&zoom=true"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
func TestThatPathParametersAndSubstitutedInTheUri(t *testing.T) {
conf := Path("/{p1}/with/{p2}", "play", url.QueryEscape("/s:las:h/"))
pq := conf.pathAndQuery()
want := "/play/with/%2Fs%3Alas%3Ah%2F"
if pq != want {
t.Errorf("got %s want %s", pq, want)
}
}
func TestThatPathParametersAreEncoded(t *testing.T) {
conf := Path("/{p1}/with/{p2}", "play", " me")
pq := conf.pathAndQuery()
want := "/play/with/%20me"
if pq != want {
t.Errorf("got %s want %s", pq, want)
}
}
func TestThatColonParameterIsSubstituted(t *testing.T) {
conf := Path("/:p1/messages", "order-queue")
pq := conf.pathAndQuery()
want := "/order-queue/messages"
if pq != want {
t.Errorf("got %s want %s", pq, want)
}
}
func TestThatStarParameterIsSubstituted(t *testing.T) {
conf := Path("/:p1/messages", "order-queue")
pq := conf.pathAndQuery()
want := "/order-queue/messages"
if pq != want {
t.Errorf("got %s want %s", pq, want)
}
}
func TestThatContentCanBeStringAsIs(t *testing.T) {
conf := Path("")
conf.Content("{}", "application/json")
if conf.BodyReader == nil {
t.Error("expected BodyReader initialized with reader on string")
}
}
func TestThatPathCanContainEmptyElements(t *testing.T) {
conf := Path("/books/{}/{}", "", "")
pq := conf.pathAndQuery()
want := "/books//"
if pq != want {
t.Errorf("got %s want %s", pq, want)
}
}
func TestThatFormDataIsPresentInRequest(t *testing.T) {
conf := Path("/form", "", "")
data := url.Values{"key": []string{"value"}}
conf.Form(data)
r := tsAPI.POST(t, conf)
ExpectString(t, r, func(m string) {
if got, want := strings.Trim(m, "\n"), "map[key:[value]]"; got != want {
t.Errorf("got (%s) want (%s)", got, want)
}
})
}