-
Notifications
You must be signed in to change notification settings - Fork 103
/
direct_test.exs
143 lines (111 loc) · 4.82 KB
/
direct_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
defmodule DirectTest do
use ExUnit.Case
import PathHelpers
setup_all do
{:ok, pid} = HTTPotion.spawn_worker_process("http://httpbin.org")
Process.register(pid, :non_pooled_connection)
{:ok, pid} = HTTPotion.spawn_worker_process("http://httpbin.org")
Process.register(pid, :non_pooled_connection_timeout)
:ok
end
test "get" do
assert_response HTTPotion.get("httpbin.org", [direct: :non_pooled_connection]), fn(response) ->
assert match?(<<60, 33, 68, 79, _ :: binary>>, response.body)
end
end
test "head" do
assert_response HTTPotion.head("httpbin.org/get", [direct: :non_pooled_connection]), fn(response) ->
assert response.body == ""
end
end
test "post charlist body" do
assert_response HTTPotion.post("httpbin.org/post", [body: 'test', direct: :non_pooled_connection])
end
test "post binary body" do
{ :ok, file } = File.read(fixture_path("image.png"))
assert_response HTTPotion.post("httpbin.org/post", [body: file, direct: :non_pooled_connection])
end
test "put" do
assert_response HTTPotion.put("httpbin.org/put", [body: "test", direct: :non_pooled_connection])
end
test "patch" do
assert_response HTTPotion.patch("httpbin.org/patch", [body: "test", direct: :non_pooled_connection])
end
test "delete" do
assert_response HTTPotion.delete("httpbin.org/delete", [direct: :non_pooled_connection])
end
test "options" do
assert_response HTTPotion.options("httpbin.org/get", [direct: :non_pooled_connection]), fn(response) ->
assert response.headers[:"Content-Length"] == "0"
assert is_binary(response.headers[:Allow])
end
end
test "headers" do
options = [direct: :non_pooled_connection, query: %{first: "foo", second: "bar"}]
assert_response HTTPotion.head("http://httpbin.org/cookies/set", options), fn(response) ->
assert_list response.headers[:"Set-Cookie"], ["first=foo; Path=/", "second=bar; Path=/"]
end
end
test "ibrowse option" do
ibrowse = [basic_auth: {'foo', 'bar'}]
assert_response HTTPotion.get("http://httpbin.org/basic-auth/foo/bar", [ ibrowse: ibrowse, direct: :non_pooled_connection ])
end
test "explicit http scheme" do
assert_response HTTPotion.head("http://httpbin.org/get", [direct: :non_pooled_connection])
end
test "https scheme" do
assert_response HTTPotion.head("https://httpbin.org/get", [direct: :non_pooled_connection])
end
test "char list URL" do
assert_response HTTPotion.head('httpbin.org/get', [direct: :non_pooled_connection])
end
test "exception" do
assert_raise HTTPotion.HTTPError, ~r/^econnrefused|req_timedout|timeout$/, fn ->
{:ok, pid} = HTTPotion.spawn_worker_process("localhost:1")
IO.puts HTTPotion.get!("localhost:1/lolwat", [direct: pid])
end
end
test "extension" do
defmodule TestClient do
use HTTPotion.Base
def process_url(url) do
send(self(), :ok)
super(url)
end
end
TestClient.head("httpbin.org/get", [direct: :non_pooled_connection])
assert_received :ok
end
test "asynchronous request" do
ibrowse = [basic_auth: {'foo', 'bar'}]
%HTTPotion.AsyncResponse{ id: id } = HTTPotion.get "httpbin.org/basic-auth/foo/bar", [stream_to: self(), ibrowse: ibrowse, direct: :non_pooled_connection]
assert_receive %HTTPotion.AsyncHeaders{ id: ^id, status_code: 200, headers: _headers }, 1_000
assert_receive %HTTPotion.AsyncChunk{ id: ^id, chunk: _chunk }, 1_000
assert_receive %HTTPotion.AsyncEnd{ id: ^id }, 1_000
end
test "asynchronous raw request" do
ibrowse = [basic_auth: {'foo', 'bar'}, return_raw_request: true]
%HTTPotion.AsyncResponse{ id: id } = HTTPotion.get "httpbin.org/basic-auth/foo/bar", [stream_to: self(), ibrowse: ibrowse, direct: :non_pooled_connection]
assert_receive %HTTPotion.AsyncHeaders{ id: ^id, status_code: 200, headers: _headers }, 1_000
assert_receive %HTTPotion.AsyncChunk{ id: ^id, chunk: _chunk }, 1_000
assert_receive %HTTPotion.AsyncRawRequest{ raw_request: _req_resp }, 1_000
end
test "asynchronous timeout" do
%HTTPotion.AsyncResponse{ id: id } = HTTPotion.get "httpbin.org/delay/2", [stream_to: self(), timeout: 1000, direct: :non_pooled_connection_timeout]
assert_receive %HTTPotion.AsyncTimeout{ id: ^id }, 2_000
end
test "headers are case insensitive" do
rsp = HTTPotion.get("httpbin.org", [direct: :non_pooled_connection])
assert rsp.headers[:"content-type"] == rsp.headers[:"cOntent-Type"]
assert rsp.headers[:"content-type"] != nil
end
defp assert_response(response, function \\ nil) do
assert HTTPotion.Response.success?(response, :extra)
assert response.headers[:Connection] == "keep-alive"
assert is_binary(response.body)
unless function == nil, do: function.(response)
end
defp assert_list(value, expected) do
Enum.sort(value) == expected
end
end