-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathhttpoison_test.exs
514 lines (428 loc) · 17.3 KB
/
httpoison_test.exs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
defmodule HTTPoisonTest do
use ExUnit.Case, async: true
import PathHelpers
alias Jason
alias HTTPoison.Request
test "get" do
assert_response(HTTPoison.get("localhost:4002/deny"), fn response ->
assert :erlang.size(response.body) == 197
end)
end
test "get with params" do
resp = HTTPoison.get("localhost:4002/get", [], params: %{foo: "bar", baz: "bong"})
assert_response(resp, fn response ->
args = Jason.decode!(response.body)["args"]
assert args["foo"] == "bar"
assert args["baz"] == "bong"
assert args |> Map.keys() |> length == 2
assert {:ok, "curl -X GET http://localhost:4002/get?" <> query} =
Request.to_curl(response.request)
assert %{"baz" => "bong", "foo" => "bar"} == URI.decode_query(query)
end)
end
test "get with params in url and options" do
resp =
HTTPoison.get(
"localhost:4002/get?bar=zing&foo=first",
[],
params: [{"foo", "second"}, {"baz", "bong"}]
)
assert_response(resp, fn response ->
args = Jason.decode!(response.body)["args"]
assert args["foo"] == ["first", "second"]
assert args["baz"] == "bong"
assert args["bar"] == "zing"
assert args |> Map.keys() |> length == 3
assert Request.to_curl(response.request) ==
{:ok,
"curl -X GET http://localhost:4002/get?bar=zing&foo=first&foo=second&baz=bong"}
end)
end
test "head" do
assert_response(HTTPoison.head("localhost:4002/get"), fn response ->
assert response.body == ""
assert Request.to_curl(response.request) == {:ok, "curl -X HEAD http://localhost:4002/get"}
end)
end
test "post charlist body" do
assert_response(HTTPoison.post("localhost:4002/post", ~c"test"), fn response ->
assert Request.to_curl(response.request) == {:ok, "curl -X POST http://localhost:4002/post"}
end)
end
test "post binary body" do
{:ok, file} = File.read(fixture_path("image.png"))
assert_response(HTTPoison.post("localhost:4002/post", file), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X POST -d '#{file}' http://localhost:4002/post"}
end)
end
test "post form data" do
assert_response(
HTTPoison.post("localhost:4002/post", {:form, [key: "value"]}, %{
"Content-type" => "application/x-www-form-urlencoded"
}),
fn response ->
Regex.match?(~r/"key".*"value"/, response.body)
assert Request.to_curl(response.request) ==
{:ok,
"curl -X POST -H 'Content-type: application/x-www-form-urlencoded' -F 'key=value' http://localhost:4002/post"}
end
)
end
test "post nested form data when given as keyword list" do
assert_response(
HTTPoison.post(
"localhost:4002/post",
{:form, [not_nested: [1, 2], nested: [foo: "bar", again: [hello: "world"]]]},
%{"Content-type" => "application/x-www-form-urlencoded"}
),
fn %HTTPoison.Response{request: request} = response ->
assert {:form,
[{:not_nested, [1, 2]}, {"nested[foo]", "bar"}, {"nested[again][hello]", "world"}]} =
request.body
Regex.match?(~r/"not_nested".*\x01\x02/, response.body)
Regex.match?(~r/"nested\[foo\]".*"bar"/, response.body)
Regex.match?(~r/"nested\[again\]\[hello\]".*"world"/, response.body)
assert Request.to_curl(response.request) ==
{:ok,
"curl -X POST -H 'Content-type: application/x-www-form-urlencoded' -F 'not_nested=\x01\x02' -F 'nested[foo]=bar' -F 'nested[again][hello]=world' http://localhost:4002/post"}
end
)
end
test "post nested form data when given as map (no order guaranteed)" do
assert_response(
HTTPoison.post(
"localhost:4002/post",
{:form, %{not_nested: [1, 2], nested: %{foo: "bar", again: %{hello: "world"}}}},
%{"Content-type" => "application/x-www-form-urlencoded"}
),
fn %HTTPoison.Response{request: request} = response ->
assert {:form, body} = request.body
assert [{:not_nested, [1, 2]}, {"nested[foo]", "bar"}, {"nested[again][hello]", "world"}]
|> MapSet.new()
|> MapSet.equal?(MapSet.new(body))
Regex.match?(~r/"not_nested".*\x01\x02/, response.body)
Regex.match?(~r/"nested\[foo\]".*"bar"/, response.body)
Regex.match?(~r/"nested\[again\]\[hello\]".*"world"/, response.body)
assert {:ok,
"curl -X POST -H 'Content-type: application/x-www-form-urlencoded'" <> form_params} =
Request.to_curl(response.request)
assert form_params =~ "-F 'not_nested=\x01\x02'"
assert form_params =~ "-F 'nested[foo]=bar'"
assert form_params =~ "-F 'nested[again][hello]=world'"
end
)
end
test "put" do
assert_response(HTTPoison.put("localhost:4002/put", "test"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X PUT -d 'test' http://localhost:4002/put"}
end)
end
test "put without body" do
assert_response(HTTPoison.put("localhost:4002/put"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X PUT http://localhost:4002/put"}
end)
end
test "patch" do
assert_response(HTTPoison.patch("localhost:4002/patch", "test"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X PATCH -d 'test' http://localhost:4002/patch"}
end)
end
test "delete" do
assert_response(HTTPoison.delete("localhost:4002/delete"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X DELETE http://localhost:4002/delete"}
end)
end
test "options" do
assert_response(HTTPoison.options("localhost:4002/get"), fn response ->
assert get_header(response.headers, "content-length") == "0"
assert is_binary(get_header(response.headers, "allow"))
assert Request.to_curl(response.request) ==
{:ok, "curl -X OPTIONS http://localhost:4002/get"}
end)
end
test "option follow redirect absolute url" do
assert_response(
HTTPoison.get(
"http://localhost:4002/redirect-to?url=http%3A%2F%2Flocalhost:4002%2Fget",
[],
follow_redirect: true
),
fn response ->
assert Request.to_curl(response.request) ==
{:ok,
"curl -L --max-redirs 5 -X GET http://localhost:4002/redirect-to?url=http%3A%2F%2Flocalhost:4002%2Fget"}
end
)
end
test "option follow redirect relative url" do
assert_response(
HTTPoison.get("http://localhost:4002/relative-redirect/1", [], follow_redirect: true),
fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -L --max-redirs 5 -X GET http://localhost:4002/relative-redirect/1"}
end
)
end
test "basic_auth hackney option" do
hackney = [basic_auth: {"user", "pass"}]
assert_response(
HTTPoison.get("http://localhost:4002/basic-auth/user/pass", [], hackney: hackney)
)
end
test "explicit http scheme" do
assert_response(HTTPoison.head("http://localhost:4002/get"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X HEAD http://localhost:4002/get"}
end)
end
test "http+unix scheme" do
if Application.get_env(:httparrot, :unix_socket, false) do
case {HTTParrot.unix_socket_supported?(), Application.fetch_env(:httparrot, :socket_path)} do
{true, {:ok, path}} ->
path = URI.encode_www_form(path)
assert_response(
HTTPoison.get("http+unix://#{path}/get"),
fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl --unix-socket #{path} -X GET http:/get"}
end
)
_ ->
:ok
end
end
end
test "char list URL" do
assert_response(HTTPoison.head(~c"localhost:4002/get"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X HEAD http://localhost:4002/get"}
end)
end
test "request headers as a map" do
map_header = %{"X-Header" => "X-Value"}
assert response = HTTPoison.get!("localhost:4002/get", map_header)
assert response.body =~ "X-Value"
assert Request.to_curl(response.request) ==
{:ok, "curl -X GET -H 'X-Header: X-Value' http://localhost:4002/get"}
end
test "cached request" do
if_modified = %{"If-Modified-Since" => "Tue, 11 Dec 2012 10:10:24 GMT"}
response = HTTPoison.get!("localhost:4002/cache", if_modified)
assert %HTTPoison.Response{status_code: 304, body: ""} = response
assert Request.to_curl(response.request) ==
{:ok,
"curl -X GET -H 'If-Modified-Since: Tue, 11 Dec 2012 10:10:24 GMT' http://localhost:4002/cache"}
end
test "send cookies" do
response = HTTPoison.get!("localhost:4002/cookies", %{}, hackney: [cookie: ["foo=1; bar=2"]])
assert Jason.decode!(response.body) == %{"cookies" => %{"foo" => "1", "bar" => "2"}}
end
test "receive cookies" do
response = HTTPoison.get!("localhost:4002/cookies/set?foo=1&bar=2")
has_foo = Enum.member?(response.headers, {"set-cookie", "foo=1; Version=1; Path=/"})
has_bar = Enum.member?(response.headers, {"set-cookie", "bar=2; Version=1; Path=/"})
assert has_foo and has_bar
assert Request.to_curl(response.request) ==
{:ok, "curl -X GET http://localhost:4002/cookies/set?foo=1&bar=2"}
end
test "exception" do
assert HTTPoison.get("localhost:1" == {:error, %HTTPoison.Error{reason: :econnrefused}})
assert_raise HTTPoison.Error, ":econnrefused", fn ->
HTTPoison.get!("localhost:1")
end
end
test "asynchronous request" do
{:ok, %HTTPoison.AsyncResponse{id: id}} =
HTTPoison.get("localhost:4002/get", [], stream_to: self())
assert_receive %HTTPoison.AsyncStatus{id: ^id, code: 200}, 1_000
assert_receive %HTTPoison.AsyncHeaders{id: ^id, headers: headers}, 1_000
assert_receive %HTTPoison.AsyncChunk{id: ^id, chunk: _chunk}, 1_000
assert_receive %HTTPoison.AsyncEnd{id: ^id}, 1_000
assert is_list(headers)
end
test "asynchronous request with explicit streaming using [async: :once]" do
{:ok, resp = %HTTPoison.AsyncResponse{id: id}} =
HTTPoison.get("localhost:4002/get", [], stream_to: self(), async: :once)
assert_receive %HTTPoison.AsyncStatus{id: ^id, code: 200}, 100
refute_receive %HTTPoison.AsyncHeaders{id: ^id, headers: _headers}, 100
{:ok, ^resp} = HTTPoison.stream_next(resp)
assert_receive %HTTPoison.AsyncHeaders{id: ^id, headers: headers}, 100
refute_receive %HTTPoison.AsyncChunk{id: ^id, chunk: _chunk}, 100
{:ok, ^resp} = HTTPoison.stream_next(resp)
assert_receive %HTTPoison.AsyncChunk{id: ^id, chunk: _chunk}, 100
refute_receive %HTTPoison.AsyncEnd{id: ^id}, 100
{:ok, ^resp} = HTTPoison.stream_next(resp)
assert_receive %HTTPoison.AsyncEnd{id: ^id}, 100
assert is_list(headers)
end
test "asynchronous redirected get request" do
{:ok, %HTTPoison.AsyncResponse{id: id}} =
HTTPoison.get(
"localhost:4002/redirect/2",
[],
stream_to: self(),
hackney: [follow_redirect: true]
)
assert_receive %HTTPoison.AsyncRedirect{id: ^id, to: to, headers: headers}, 1_000
assert to == "http://localhost:4002/redirect/1"
assert is_list(headers)
end
test "multipart upload" do
response =
HTTPoison.post(
"localhost:4002/post",
{:multipart, [{:file, "test/test_helper.exs"}, {"name", "value"}]}
)
assert_response(response)
end
test "post streaming body" do
expected = %{"some" => "bytes"}
enumerable = Jason.encode!(expected) |> String.split("")
headers = %{"Content-type" => "application/json"}
response = HTTPoison.post("localhost:4002/post", {:stream, enumerable}, headers)
assert_response(response, fn response ->
assert Jason.decode!(response.body)["json"] == expected
assert Request.to_curl(response.request) ==
{:ok,
"curl -X POST -H 'Content-type: application/json' -d '{\"some\":\"bytes\"}' http://localhost:4002/post"}
end)
end
test "max_body_length limits body size" do
{:ok, socket} = :gen_tcp.listen(0, [:binary, {:active, false}, {:packet, :raw}])
{:ok, [buffer: buffer_size]} = :inet.getopts(socket, [:buffer])
:ok = :gen_tcp.close(socket)
max_length = Kernel.trunc(buffer_size * 1.5)
expected_length =
Float.ceil(max_length / buffer_size)
|> Kernel.*(buffer_size)
|> Kernel.trunc()
resp = HTTPoison.get("localhost:4002/stream/20", [], max_body_length: max_length)
assert_response(resp, fn response ->
assert byte_size(response.body) <= expected_length
assert byte_size(response.body) >= max_length
end)
end
defp assert_response({:ok, response}, function \\ nil) do
assert is_list(response.headers)
assert response.status_code == 200
assert is_binary(response.body)
unless function == nil, do: function.(response)
end
defp get_header(headers, key) do
headers
|> Enum.filter(fn {k, _} -> k == key end)
|> hd
|> elem(1)
end
describe "ssl config tests" do
test "https scheme" do
httparrot_priv_dir = :code.priv_dir(:httparrot)
cacert_file = "#{httparrot_priv_dir}/ssl/server-ca.crt"
cert_file = "#{httparrot_priv_dir}/ssl/server.crt"
key_file = "#{httparrot_priv_dir}/ssl/server.key"
assert_response(
HTTPoison.get(
"https://localhost:8433/get",
[],
ssl: [cacertfile: cacert_file, keyfile: key_file, certfile: cert_file]
),
fn response ->
assert Request.to_curl(response.request) ==
{:ok,
"curl --cert #{cert_file} --key #{key_file} --cacert #{cacert_file} -X GET https://localhost:8433/get"}
end
)
end
test "expired certificate" do
assert {:error, %HTTPoison.Error{reason: {:tls_alert, {:certificate_expired, reason}}}} =
HTTPoison.get("https://expired.badssl.com/")
assert to_string(reason) =~ "Certificate Expired"
# TLS version should not matter
assert {:error, %HTTPoison.Error{reason: {:tls_alert, _}}} =
HTTPoison.get("https://expired.badssl.com/", [], ssl: [{:versions, [:"tlsv1.2"]}])
assert {:ok, _} =
HTTPoison.get("https://expired.badssl.com/", [], ssl: [{:verify, :verify_none}])
# Can be disabled via verify_fun
assert {:ok, _} =
HTTPoison.get("https://expired.badssl.com/", [],
ssl: [
verify_fun:
{fn _, reason, state ->
case reason do
{:bad_cert, :cert_expired} -> {:valid, state}
{:bad_cert, :unknown_ca} -> {:valid, state}
{:extension, _} -> {:valid, state}
:valid -> {:valid, state}
:valid_peer -> {:valid, state}
error -> {:fail, error}
end
end, []}
]
)
end
test "allows changing TLS1.0 settings" do
assert {:error,
%HTTPoison.Error{
reason: {:tls_alert, {:protocol_version, reason}}
}} =
HTTPoison.get("https://tls-v1-0.badssl.com:1010/", [],
ssl: [{:versions, [:"tlsv1.2"]}]
)
assert to_string(reason) =~ "Protocol Version"
if :tlsv1 in :ssl.versions()[:available] do
assert {:ok, _} =
HTTPoison.get("https://tls-v1-0.badssl.com:1010/", [],
ssl: [
{:versions, [:tlsv1]}
]
)
end
end
test "allows changing TLS1.1 settings" do
assert {:error,
%HTTPoison.Error{
reason: {:tls_alert, {:protocol_version, reason}}
}} =
HTTPoison.get("https://tls-v1-1.badssl.com:1011/", [],
ssl: [versions: [:"tlsv1.2", :tlsv1]]
)
assert to_string(reason) =~ "Protocol Version"
if :"tlsv1.1" in :ssl.versions()[:available] do
assert {:ok, _} =
HTTPoison.get("https://tls-v1-1.badssl.com:1011/", [],
ssl: [versions: [:"tlsv1.1"]]
)
end
end
test "does support tls1.2" do
if :"tlsv1.2" in :ssl.versions()[:supported] do
assert {:ok, _} = HTTPoison.get("https://tls-v1-2.badssl.com:1012/", [])
end
if :"tlsv1.2" in :ssl.versions()[:available] do
assert {:ok, _} =
HTTPoison.get("https://tls-v1-2.badssl.com:1012/", [],
ssl: [versions: [:"tlsv1.2"]]
)
end
end
test "invalid common name" do
assert {:error,
%HTTPoison.Error{
reason: {:tls_alert, {:handshake_failure, reason}}
}} = HTTPoison.get("https://wrong.host.badssl.com/")
assert to_string(reason) =~ ~r"hostname|altnames"
assert {:error,
%HTTPoison.Error{
reason: {:tls_alert, _}
}} =
HTTPoison.get("https://wrong.host.badssl.com/", [],
ssl: [{:versions, [:"tlsv1.2"]}]
)
end
end
end