forked from edgurgel/httpoison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpoison_test.exs
203 lines (162 loc) · 6.83 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
defmodule HTTPoisonTest do
use ExUnit.Case, async: true
import PathHelpers
setup_all do
{:ok, _} = :application.ensure_all_started(:httparrot)
:ok
end
test "get" do
assert_response HTTPoison.get("localhost:8080/deny"), fn(response) ->
assert :erlang.size(response.body) == 197
end
end
test "get with params" do
resp = HTTPoison.get("localhost:8080/get", [], params: %{foo: "bar", baz: "bong"})
assert_response resp, fn(response) ->
args = JSX.decode!(response.body)["args"]
assert args["foo"] == "bar"
assert args["baz"] == "bong"
assert (args |> Map.keys |> length) == 2
end
end
test "head" do
assert_response HTTPoison.head("localhost:8080/get"), fn(response) ->
assert response.body == ""
end
end
test "post charlist body" do
assert_response HTTPoison.post("localhost:8080/post", 'test')
end
test "post binary body" do
{ :ok, file } = File.read(fixture_path("image.png"))
assert_response HTTPoison.post("localhost:8080/post", file)
end
test "post form data" do
assert_response HTTPoison.post("localhost:8080/post", {:form, [key: "value"]}, %{"Content-type" => "application/x-www-form-urlencoded"}), fn(response) ->
Regex.match?(~r/"key".*"value"/, response.body)
end
end
test "put" do
assert_response HTTPoison.put("localhost:8080/put", "test")
end
test "put without body" do
assert_response HTTPoison.put("localhost:8080/put")
end
test "patch" do
assert_response HTTPoison.patch("localhost:8080/patch", "test")
end
test "delete" do
assert_response HTTPoison.delete("localhost:8080/delete")
end
test "options" do
assert_response HTTPoison.options("localhost:8080/get"), fn(response) ->
assert get_header(response.headers, "content-length") == "0"
assert is_binary(get_header(response.headers, "allow"))
end
end
test "option follow redirect absolute url" do
assert_response HTTPoison.get("http://localhost:8080/redirect-to?url=http%3A%2F%2Flocalhost:8080%2Fget", [], [follow_redirect: true])
end
test "option follow redirect relative url" do
assert_response HTTPoison.get("http://localhost:8080/relative-redirect/1", [], [follow_redirect: true])
end
test "basic_auth hackney option" do
hackney = [basic_auth: {"user", "pass"}]
assert_response HTTPoison.get("http://localhost:8080/basic-auth/user/pass", [], [ hackney: hackney ])
end
test "explicit http scheme" do
assert_response HTTPoison.head("http://localhost:8080/get")
end
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])
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")
_ -> :ok
end
end
end
test "char list URL" do
assert_response HTTPoison.head('localhost:8080/get')
end
test "request headers as a map" do
map_header = %{"X-Header" => "X-Value"}
assert HTTPoison.get!("localhost:8080/get", map_header).body =~ "X-Value"
end
test "cached request" do
if_modified = %{"If-Modified-Since" => "Tue, 11 Dec 2012 10:10:24 GMT"}
response = HTTPoison.get!("localhost:8080/cache", if_modified)
assert %HTTPoison.Response{status_code: 304, body: ""} = response
end
test "send cookies" do
response = HTTPoison.get!("localhost:8080/cookies", %{}, hackney: [cookie: ["foo=1; bar=2"]])
assert response.body |> String.replace( ~r/\s|\r?\n/, "") |> String.replace(~r/\"/, "'") |> JSX.decode! == %{"cookies" => %{"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:8080/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:8080/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:8080/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:8080/redirect/1"
assert is_list(headers)
end
test "multipart upload" do
response = HTTPoison.post("localhost:8080/post", {:multipart, [{:file, "test/test_helper.exs"}, {"name", "value"}]})
assert_response(response)
end
test "post streaming body" do
expected = %{"some" => "bytes"}
enumerable = JSX.encode!(expected) |> String.split("")
headers = %{"Content-type" => "application/json"}
response = HTTPoison.post("localhost:8080/post", {:stream, enumerable}, headers)
assert_response response
{:ok, %HTTPoison.Response{body: body}} = response
assert JSX.decode!(body)["json"] == expected
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
end