forked from edgurgel/httpoison
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpoison_base_test.exs
More file actions
160 lines (126 loc) · 5.83 KB
/
httpoison_base_test.exs
File metadata and controls
160 lines (126 loc) · 5.83 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
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
defmodule HTTPoisonBaseTest do
use ExUnit.Case
import :meck
defmodule Example do
use HTTPoison.Base
def process_url(url), do: "http://" <> url
def process_request_body(body), do: {:req_body, body}
def process_request_headers(headers), do: {:req_headers, headers}
def process_response_body(body), do: {:resp_body, body}
def process_headers(headers), do: {:headers, headers}
def process_status_code(code), do: {:code, code}
end
defmodule ExampleDefp do
use HTTPoison.Base
defp process_url(url), do: "http://" <> url
defp process_request_body(body), do: {:req_body, body}
defp process_request_headers(headers), do: {:req_headers, headers}
defp process_response_body(body), do: {:resp_body, body}
defp process_headers(headers), do: {:headers, headers}
defp process_status_code(code), do: {:code, code}
end
setup do
new :hackney
on_exit fn -> unload() end
:ok
end
test "request body using Example" do
expect(:hackney, :request, [{[:post, "http://localhost", {:req_headers, []}, {:req_body, "body"}, []],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert Example.post!("localhost", "body") ==
%HTTPoison.Response{ status_code: {:code, 200},
headers: {:headers, "headers"},
body: {:resp_body, "response"} }
assert validate :hackney
end
test "request body using ExampleDefp" do
expect(:hackney, :request, [{[:post, "http://localhost", {:req_headers, []}, {:req_body, "body"}, []],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert ExampleDefp.post!("localhost", "body") ==
%HTTPoison.Response{ status_code: {:code, 200},
headers: {:headers, "headers"},
body: {:resp_body, "response"} }
assert validate :hackney
end
test "request raises error tuple" do
reason = {:closed, "Something happened"}
expect(:hackney, :request, 5, {:error, reason})
assert_raise HTTPoison.Error, "{:closed, \"Something happened\"}", fn ->
HTTPoison.get!("http://localhost")
end
assert HTTPoison.get("http://localhost") == {:error, %HTTPoison.Error{reason: reason}}
assert validate :hackney
end
test "passing connect_timeout option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [connect_timeout: 12345]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], timeout: 12345) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing recv_timeout option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [recv_timeout: 12345]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], recv_timeout: 12345) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing proxy option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [proxy: "proxy"]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], proxy: "proxy") ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing proxy option with proxy_auth" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [proxy_auth: {"username", "password"}, proxy: "proxy"]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], [proxy: "proxy", proxy_auth: {"username", "password"}]) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing ssl option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [ssl_options: [certfile: "certs/client.crt"]]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], ssl: [certfile: "certs/client.crt"]) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing follow_redirect option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [follow_redirect: true]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], follow_redirect: true) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
test "passing max_redirect option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [max_redirect: 2]],
{:ok, 200, "headers", :client}}])
expect(:hackney, :body, 1, {:ok, "response"})
assert HTTPoison.post!("localhost", "body", [], max_redirect: 2) ==
%HTTPoison.Response{ status_code: 200,
headers: "headers",
body: "response" }
assert validate :hackney
end
end