-
Notifications
You must be signed in to change notification settings - Fork 17
/
adapter_test_helper.exs
159 lines (134 loc) · 5.14 KB
/
adapter_test_helper.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
defmodule Maxwell.Adapter.TestHelper do
defmacro __using__([adapter: adapter]) do
client = adapter |> Macro.expand(__CALLER__) |> Module.concat(TestClient)
quote location: :keep do
use ExUnit.Case, async: false
import Maxwell.Conn
defmodule unquote(client) do
use Maxwell.Builder
adapter unquote(adapter)
middleware Maxwell.Middleware.BaseUrl, "http://httpbin.org"
middleware Maxwell.Middleware.Opts, [connect_timeout: 5000]
middleware Maxwell.Middleware.Json
def get_ip_test() do
"/ip" |> new() |> get!()
end
def encode_decode_json_test(body) do
"/post"
|> new()
|> put_req_body(body)
|> post!
|> get_resp_body("json")
end
def user_agent_test(user_agent) do
new("/user-agent")
|> put_req_header("user-agent", user_agent)
|> get!
|> get_resp_body("user-agent")
end
def put_json_test(json) do
new("/put")
|> put_req_body(json)
|> put!
|> get_resp_body("data")
end
def delete_test() do
new("/delete")
|> delete!
|> get_resp_body("data")
end
def multipart_test() do
new("/post")
|> put_req_body({:multipart, [{:file, "test/maxwell/multipart_test_file.sh"}]})
|> post!
end
def multipart_with_extra_header_test() do
new("/post")
|> put_req_body({:multipart, [{:file, "test/maxwell/multipart_test_file.sh", [{"Content-Type", "image/jpeg"}]}]})
|> post!
end
def file_test(filepath) do
new("/post")
|> put_req_header("transfer-encoding", "chunked")
|> put_req_body({:file, filepath})
|> post!
end
def file_test(filepath, content_type) do
new("/post")
|> put_req_header("transfer-encoding", "Chunked")
|> put_req_body({:file, filepath})
|> put_req_header("content-type", content_type)
|> post!
end
def file_without_transfer_encoding_test(filepath, content_type) do
new("/post")
|> put_req_body({:file, filepath})
|> put_req_header("content-type", content_type)
|> post!
end
def stream_test() do
new("/post")
|> put_req_header("content-type", "application/vnd.lotus-1-2-3")
|> put_req_header("content-length", 6)
|> put_req_body(Stream.map(["1", "2", "3"], fn(x) -> List.duplicate(x, 2) end))
|> post!
end
end
if Code.ensure_loaded?(:rand) do
setup do
:rand.seed(:exs1024, {:erlang.phash2([node()]), :erlang.monotonic_time, :erlang.unique_integer})
:ok
end
else
setup do
:random.seed(:erlang.phash2([node()]), :erlang.monotonic_time, :erlang.unique_integer)
:ok
end
end
test "sync request" do
assert unquote(client).get_ip_test |> get_status == 200
end
test "encode decode json test" do
result = %{"josnkey1" => "jsonvalue1", "josnkey2" => "jsonvalue2"} |> unquote(client).encode_decode_json_test
assert result == %{"josnkey1" => "jsonvalue1", "josnkey2" => "jsonvalue2"}
end
test "mutilpart body file" do
conn = unquote(client).multipart_test
assert get_resp_body(conn, "files") == %{"file" => "#!/usr/bin/env bash\necho \"test multipart file\"\n"}
end
test "mutilpart body file extra headers" do
conn = unquote(client).multipart_with_extra_header_test
assert get_resp_body(conn, "files") == %{"file" => "#!/usr/bin/env bash\necho \"test multipart file\"\n"}
end
test "send file without content-type" do
conn = unquote(client).file_test("test/maxwell/multipart_test_file.sh")
assert get_resp_body(conn, "data") == "#!/usr/bin/env bash\necho \"test multipart file\"\n"
end
test "send file with content-type" do
conn = unquote(client).file_test("test/maxwell/multipart_test_file.sh", "application/x-sh")
assert get_resp_body(conn, "data") == "#!/usr/bin/env bash\necho \"test multipart file\"\n"
end
test "file_without_transfer_encoding" do
conn = unquote(client).file_without_transfer_encoding_test("test/maxwell/multipart_test_file.sh", "application/x-sh")
assert get_resp_body(conn, "data") == "#!/usr/bin/env bash\necho \"test multipart file\"\n"
end
test "send stream" do
conn = unquote(client).stream_test
assert get_resp_body(conn, "data") == "112233"
end
test "user-agent header test" do
assert "test" |> unquote(client).user_agent_test == "test"
end
test "/put" do
assert %{"key" => "value"} |> unquote(client).put_json_test == "{\"key\":\"value\"}"
end
test "/delete" do
assert unquote(client).delete_test == ""
end
test "Head without body(test return {:ok, status, header})" do
body = unquote(client).head! |> get_resp_body |> Kernel.to_string
assert body == ""
end
end
end
end