forked from micha/resty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresty_shpec.sh
241 lines (215 loc) · 9.09 KB
/
resty_shpec.sh
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
[[ "$SHELL" == "bash" ]] && shopt -s expand_aliases # needed for bash
describe "Resty"
describe "Basic"
it "load resty function without error"
source ./resty -W # source resty with no history mode
assert equal $? 0
end
it "can define a target"
output=$(resty localhost:4004 2>&1)
assert equal $? 0
assert equal "$output" "http://localhost:4004*"
end
# Launch server
resty localhost:4004 2>/dev/null
it "can access to it with a get"
output=$(GET / 2>&1)
assert present "$output"
end
it "get the good content"
output=$(GET /simple.txt)
assert equal "$output" "hi there"
end
end
describe "HTTP Verbs"
data='data here'
it "GET"
output=$(GET /echo)
assert equal "$output" "get"
end
it "GET with query string"
output=$(GET "/echo?a=b" -Q)
assert equal "$output" 'get\n{"a":"b"}'
end
it "GET with query string by argument"
output=$(GET /echo -q "a=b")
assert equal "$output" 'get\n{"a":"b"}'
end
it "POST with data"
output=$(POST /echo "$data")
assert equal "$output" "post\n$data"
end
it "POST with data and query parameter"
output=$(POST /echo "$data" -q "a=b")
assert equal "$output" 'post\n'"$data"'\n{"a":"b"}'
end
it "PUT with data"
output=$(PUT /echo "$data")
assert equal "$output" "put\n$data"
end
it "PUT with data and query parameter"
output=$(PUT /echo "$data" -q "a=b")
assert equal "$output" 'put\n'"$data"'\n{"a":"b"}'
end
it "PATCH with data"
output=$(PATCH /echo "$data")
assert equal "$output" "patch\n$data"
end
it "PATCH with data and query parameter"
output=$(PATCH /echo "$data" -q "a=b")
assert equal "$output" 'patch\n'"$data"'\n{"a":"b"}'
end
it "DELETE"
output=$(DELETE /echo)
assert equal "$output" "delete"
end
it "DELETE with data"
output=$(DELETE /echo "$data")
assert equal "$output" "delete\n$data"
end
it "TRACE"
output=$(TRACE /echo "$data")
assert equal "$output" "trace\n$data"
end
it "OPTIONS"
output=$(OPTIONS /echo)
assert equal "$output" "options"
end
it "HEAD"
output=$(HEAD /echo)
assert grep "$output" "200"
end
end
describe "Viewer"
it "prety format when lynx is installed"
output=$(GET /simple.html | tr -d ' ')
assert grep $(which lynx) lynx
assert equal "$output" "$(cat test/test-data/simple-html-lynx.txt | tr -d ' ')"
# note : hack to cope with lynx version differencies and different handling of table headers
end
it "does not prety format on raw mode"
output=$(GET /simple.html -Z)
assert equal "$output" "$(< test/data/simple.html)"
end
it "json pretty-print formatting with pypp"
output=$(GET /simple.json | ./pypp)
assert equal "$output" '{\n "bar": {
"cat": "meow",\n "dog": "woof",\n "fish": "banana"\n },\n "foo": [
1,\n 2,\n 3\n ]\n}'
end
end
describe "Curl Failures"
it "are reported to the user"
stub_command curl "echo '* Connected to mysite.com (64.136.20.67) port 443 (#0)
* Server aborted the SSL handshake
* Closing connection 0' >&2; return 2"
output=$(GET /simple.html -Z 2>&1)
assert unequal $? 0
assert equal "$output" "* Connected to mysite.com (64.136.20.67) port 443 (#0)
* Server aborted the SSL handshake
* Closing connection 0"
unstub_command curl
end
end
describe "Options"
it "GET with headers"
output=$(GET /echo -v 2> /tmp/resty-getheader-error)
erroroutput=$(< /tmp/resty-getheader-error)
assert equal "$output" "get"
assert grep "$erroroutput" "content-type:"
assert grep "$erroroutput" "cache-control:"
end
it "POST with data sent in query string"
output=$(POST /echo -d 'foo=bar' -G)
assert equal "$output" 'post\n\n{"foo":"bar"}'
echo " note a strange behavior on osx where query send has a '&' in it"
end
it "POST with extra header and basic auth, data in message body"
output=$(POST /echo -u "user:secret" -H "Accept: application/json" \
-v < test/data/simple.json 2> /tmp/resty-getheader-error)
erroroutput=$(< /tmp/resty-getheader-error)
assert equal "$output" 'post\n{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}'
assert grep "$erroroutput" "Authorization:\ Basic\ dXNlcjpzZWNyZXQ="
assert grep "$erroroutput" "Accept:\ application/json"
end
it "POST with extra header and basic auth, data in message body and --json option"
output=$(POST /echo -u "user:secret" --json \
-v < test/data/simple.json 2> /tmp/resty-getheader-error)
erroroutput=$(< /tmp/resty-getheader-error)
assert equal "$output" 'post\n{"foo":[1,2,3],"bar":{"dog":"woof","cat":"meow","fish":"banana"}}'
assert grep "$erroroutput" "Authorization:\ Basic\ dXNlcjpzZWNyZXQ="
assert grep "$erroroutput" "Accept:\ application/json"
end
end
describe "Resty Global options"
it "Setting new options"
resty localhost:4004 -u "user:secret" -H "Accept: application/json" 2> /dev/null
output=$(GET /echo -v 2> /tmp/resty-newopt-error)
erroroutput=$(< /tmp/resty-newopt-error)
assert equal "$output" "get"
assert grep "$erroroutput" "Authorization:\ Basic\ dXNlcjpzZWNyZXQ="
assert grep "$erroroutput" "Accept:\ application/json"
end
it "Resetting options"
resty localhost:4004 2> /dev/null
output=$(GET /echo -v 2> /tmp/resty-resetopt-error)
erroroutput=$(< /tmp/resty-resetopt-error)
assert equal "$output" "get"
assert no_grep "$erroroutput" "Authorization:\ Basic\ dXNlcjpzZWNyZXQ="
assert no_grep "$erroroutput" "Accept:\ application/json"
end
end
describe "Preserve Path Argument"
it "works by default"
GET /echo > /dev/null
assert equal "$_RESTY_PATH" "/echo"
output=$(GET)
assert equal "$output" "get"
end
it "can be disabled"
RESTY_NO_PRESERVE_PATH="yes"
_RESTY_PATH="" # reset RESTY_PATH
GET /echo > /dev/null
assert equal "$_RESTY_PATH" ""
output=$(GET 2>&1)
assert grep "$output" '{"statusCode":403'
RESTY_NO_PRESERVE_PATH="no"
end
end
describe "Host defined Options"
it "are setted at resty when host match"
XDG_CONFIG_HOME=./test/test-data
resty localhost:4004 2> /dev/null
assert equal "$_RESTY_OPT_DEFAULT_GET" "-Q"
assert equal "${_RESTY_OPT_HOST_GET[*]}" "-H 'Accept: text/plain'"
assert equal "$_RESTY_OPT_HOST_POST" "--json"
end
it "are used when performing a query"
output=$(GET "/echo?a=b" -v 2> /tmp/resty-resetopt-error)
erroroutput=$(< /tmp/resty-resetopt-error)
assert equal "$output" 'get\n{"a":"b"}'
assert grep "$erroroutput" "Accept:\ text/plain"
output=$(POST /echo "{\"a\": \"b\"}" -v 2> /tmp/resty-resetopt-error)
erroroutput=$(< /tmp/resty-resetopt-error)
assert equal "$output" 'post\n{"a": "b"}'
assert grep "$erroroutput" "Content-Type:\ application/json"
assert grep "$erroroutput" "Accept:\ application/json"
end
it "are setted at resty with host match and defaults and config are merged"
XDG_CONFIG_HOME=./test/test-data
resty localhost:4004 -u "user:secret with space" 2> /dev/null
assert equal "$_RESTY_OPT_DEFAULT_GET" "-Q"
assert equal "${_RESTY_OPT_HOST_GET[*]}" "-H 'Accept: text/plain'"
assert equal "$_RESTY_OPT_HOST_POST" "--json"
assert equal "$_RESTY_OPTS" "-u user:secret\\ with\\ space"
query="$(GET "/echo?a=b" --dry-run)"
assert equal "$query" "curl -sLv '' -X GET -b \"/resty/c/localhost:4004\" -c \"/resty/c/localhost:4004\" -H 'Accept: text/plain' -u user:secret\\ with\\ space \"http://localhost:4004/echo?a=b\""
end
it "are unsetted at resty when no host match"
resty localhost:4005 2> /dev/null
assert equal "$_RESTY_OPT_DEFAULT_GET" "-Q"
assert equal "$_RESTY_OPT_HOST_GET" ""
assert equal "$_RESTY_OPT_HOST_POST" ""
end
end
end