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
|
#!/usr/bin/env escript
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
-record(req, {method=get, path="", headers=[], body="", opts=[]}).
server() ->
lists:concat([
"http://127.0.0.1:",
mochiweb_socket_server:get(couch_httpd, port),
"/_test/"
]).
proxy() ->
"http://127.0.0.1:" ++ integer_to_list(test_web:get_port()) ++ "/".
external() -> "https://www.google.com/".
main(_) ->
test_util:init_code_path(),
etap:plan(61),
case (catch test()) of
ok ->
etap:end_tests();
Other ->
etap:diag("Test died abnormally: ~p", [Other]),
etap:bail("Bad return value.")
end,
ok.
check_request(Name, Req, Remote, Local) ->
case Remote of
no_remote -> ok;
_ -> test_web:set_assert(Remote)
end,
Url = case proplists:lookup(url, Req#req.opts) of
none -> server() ++ Req#req.path;
{url, DestUrl} -> DestUrl
end,
Opts = [{headers_as_is, true} | Req#req.opts],
Resp =ibrowse:send_req(
Url, Req#req.headers, Req#req.method, Req#req.body, Opts
),
%etap:diag("ibrowse response: ~p", [Resp]),
case Local of
no_local -> ok;
_ -> etap:fun_is(Local, Resp, Name)
end,
case {Remote, Local} of
{no_remote, _} ->
ok;
{_, no_local} ->
ok;
_ ->
etap:is(test_web:check_last(), was_ok, Name ++ " - request handled")
end,
Resp.
test() ->
couch_server_sup:start_link([
test_util:build_file("test/etap/180-http-proxy.ini") |
test_util:config_files()
]),
ibrowse:start(),
crypto:start(),
% start the test_web server on a random port
test_web:start_link(),
Url = lists:concat([
"{couch_httpd_proxy, handle_proxy_req, <<\"http://127.0.0.1:",
test_web:get_port(),
"/\">>}"
]),
couch_config:set("httpd_global_handlers", "_test", Url, false),
% let couch_httpd restart
timer:sleep(100),
test_basic(),
test_alternate_status(),
test_trailing_slash(),
test_passes_header(),
test_passes_host_header(),
test_passes_header_back(),
test_rewrites_location_headers(),
test_doesnt_rewrite_external_locations(),
test_rewrites_relative_location(),
test_uses_same_version(),
test_passes_body(),
test_passes_eof_body_back(),
test_passes_chunked_body(),
test_passes_chunked_body_back(),
test_connect_error(),
ok.
test_basic() ->
Remote = fun(Req) ->
'GET' = Req:get(method),
"/" = Req:get(path),
0 = Req:get(body_length),
<<>> = Req:recv_body(),
{ok, {200, [{"Content-Type", "text/plain"}], "ok"}}
end,
Local = fun({ok, "200", _, "ok"}) -> true; (_) -> false end,
check_request("Basic proxy test", #req{}, Remote, Local).
test_alternate_status() ->
Remote = fun(Req) ->
"/alternate_status" = Req:get(path),
{ok, {201, [], "ok"}}
end,
Local = fun({ok, "201", _, "ok"}) -> true; (_) -> false end,
Req = #req{path="alternate_status"},
check_request("Alternate status", Req, Remote, Local).
test_trailing_slash() ->
Remote = fun(Req) ->
"/trailing_slash/" = Req:get(path),
{ok, {200, [], "ok"}}
end,
Local = fun({ok, "200", _, "ok"}) -> true; (_) -> false end,
Req = #req{path="trailing_slash/"},
check_request("Trailing slash", Req, Remote, Local).
test_passes_header() ->
Remote = fun(Req) ->
"/passes_header" = Req:get(path),
"plankton" = Req:get_header_value("X-CouchDB-Ralph"),
{ok, {200, [], "ok"}}
end,
Local = fun({ok, "200", _, "ok"}) -> true; (_) -> false end,
Req = #req{
path="passes_header",
headers=[{"X-CouchDB-Ralph", "plankton"}]
},
check_request("Passes header", Req, Remote, Local).
test_passes_host_header() ->
Remote = fun(Req) ->
"/passes_host_header" = Req:get(path),
"www.google.com" = Req:get_header_value("Host"),
{ok, {200, [], "ok"}}
end,
Local = fun({ok, "200", _, "ok"}) -> true; (_) -> false end,
Req = #req{
path="passes_host_header",
headers=[{"Host", "www.google.com"}]
},
check_request("Passes host header", Req, Remote, Local).
test_passes_header_back() ->
Remote = fun(Req) ->
"/passes_header_back" = Req:get(path),
{ok, {200, [{"X-CouchDB-Plankton", "ralph"}], "ok"}}
end,
Local = fun
({ok, "200", Headers, "ok"}) ->
lists:member({"X-CouchDB-Plankton", "ralph"}, Headers);
(_) ->
false
end,
Req = #req{path="passes_header_back"},
check_request("Passes header back", Req, Remote, Local).
test_rewrites_location_headers() ->
etap:diag("Testing location header rewrites."),
do_rewrite_tests([
{"Location", proxy() ++ "foo/bar", server() ++ "foo/bar"},
{"Content-Location", proxy() ++ "bing?q=2", server() ++ "bing?q=2"},
{"Uri", proxy() ++ "zip#frag", server() ++ "zip#frag"},
{"Destination", proxy(), server()}
]).
test_doesnt_rewrite_external_locations() ->
etap:diag("Testing no rewrite of external locations."),
do_rewrite_tests([
{"Location", external() ++ "search", external() ++ "search"},
{"Content-Location", external() ++ "s?q=2", external() ++ "s?q=2"},
{"Uri", external() ++ "f#f", external() ++ "f#f"},
{"Destination", external() ++ "f?q=2#f", external() ++ "f?q=2#f"}
]).
test_rewrites_relative_location() ->
etap:diag("Testing relative rewrites."),
do_rewrite_tests([
{"Location", "/foo", server() ++ "foo"},
{"Content-Location", "bar", server() ++ "bar"},
{"Uri", "/zing?q=3", server() ++ "zing?q=3"},
{"Destination", "bing?q=stuff#yay", server() ++ "bing?q=stuff#yay"}
]).
do_rewrite_tests(Tests) ->
lists:foreach(fun({Header, Location, Url}) ->
do_rewrite_test(Header, Location, Url)
end, Tests).
do_rewrite_test(Header, Location, Url) ->
Remote = fun(Req) ->
"/rewrite_test" = Req:get(path),
{ok, {302, [{Header, Location}], "ok"}}
end,
Local = fun
({ok, "302", Headers, "ok"}) ->
etap:is(
couch_util:get_value(Header, Headers),
Url,
"Header rewritten correctly."
),
true;
(_) ->
false
end,
Req = #req{path="rewrite_test"},
Label = "Rewrite test for ",
check_request(Label ++ Header, Req, Remote, Local).
test_uses_same_version() ->
Remote = fun(Req) ->
"/uses_same_version" = Req:get(path),
{1, 0} = Req:get(version),
{ok, {200, [], "ok"}}
end,
Local = fun({ok, "200", _, "ok"}) -> true; (_) -> false end,
Req = #req{
path="uses_same_version",
opts=[{http_vsn, {1, 0}}]
},
check_request("Uses same version", Req, Remote, Local).
test_passes_body() ->
Remote = fun(Req) ->
'PUT' = Req:get(method),
"/passes_body" = Req:get(path),
<<"Hooray!">> = Req:recv_body(),
{ok, {201, [], "ok"}}
end,
Local = fun({ok, "201", _, "ok"}) -> true; (_) -> false end,
Req = #req{
method=put,
path="passes_body",
body="Hooray!"
},
check_request("Passes body", Req, Remote, Local).
test_passes_eof_body_back() ->
BodyChunks = [<<"foo">>, <<"bar">>, <<"bazinga">>],
Remote = fun(Req) ->
'GET' = Req:get(method),
"/passes_eof_body" = Req:get(path),
{raw, {200, [{"Connection", "close"}], BodyChunks}}
end,
Local = fun({ok, "200", _, "foobarbazinga"}) -> true; (_) -> false end,
Req = #req{path="passes_eof_body"},
check_request("Passes eof body", Req, Remote, Local).
test_passes_chunked_body() ->
BodyChunks = [<<"foo">>, <<"bar">>, <<"bazinga">>],
Remote = fun(Req) ->
'POST' = Req:get(method),
"/passes_chunked_body" = Req:get(path),
RecvBody = fun
({Length, Chunk}, [Chunk | Rest]) ->
Length = size(Chunk),
Rest;
({0, []}, []) ->
ok
end,
ok = Req:stream_body(1024*1024, RecvBody, BodyChunks),
{ok, {201, [], "ok"}}
end,
Local = fun({ok, "201", _, "ok"}) -> true; (_) -> false end,
Req = #req{
method=post,
path="passes_chunked_body",
headers=[{"Transfer-Encoding", "chunked"}],
body=mk_chunked_body(BodyChunks)
},
check_request("Passes chunked body", Req, Remote, Local).
test_passes_chunked_body_back() ->
Name = "Passes chunked body back",
Remote = fun(Req) ->
'GET' = Req:get(method),
"/passes_chunked_body_back" = Req:get(path),
BodyChunks = [<<"foo">>, <<"bar">>, <<"bazinga">>],
{chunked, {200, [{"Transfer-Encoding", "chunked"}], BodyChunks}}
end,
Req = #req{
path="passes_chunked_body_back",
opts=[{stream_to, self()}]
},
Resp = check_request(Name, Req, Remote, no_local),
etap:fun_is(
fun({ibrowse_req_id, _}) -> true; (_) -> false end,
Resp,
"Received an ibrowse request id."
),
{_, ReqId} = Resp,
% Grab headers from response
receive
{ibrowse_async_headers, ReqId, "200", Headers} ->
etap:is(
proplists:get_value("Transfer-Encoding", Headers),
"chunked",
"Response included the Transfer-Encoding: chunked header"
),
ibrowse:stream_next(ReqId)
after 1000 ->
throw({error, timeout})
end,
% Check body received
% TODO: When we upgrade to ibrowse >= 2.0.0 this check needs to
% check that the chunks returned are what we sent from the
% Remote test.
etap:diag("TODO: UPGRADE IBROWSE"),
etap:is(recv_body(ReqId, []), <<"foobarbazinga">>, "Decoded chunked body."),
% Check test_web server.
etap:is(test_web:check_last(), was_ok, Name ++ " - request handled").
test_connect_error() ->
Local = fun({ok, "500", _Headers, _Body}) -> true; (_) -> false end,
Url = lists:concat([
"http://127.0.0.1:",
mochiweb_socket_server:get(couch_httpd, port),
"/_error"
]),
Req = #req{opts=[{url, Url}]},
check_request("Connect error", Req, no_remote, Local).
mk_chunked_body(Chunks) ->
mk_chunked_body(Chunks, []).
mk_chunked_body([], Acc) ->
iolist_to_binary(lists:reverse(Acc, "0\r\n\r\n"));
mk_chunked_body([Chunk | Rest], Acc) ->
Size = to_hex(size(Chunk)),
mk_chunked_body(Rest, ["\r\n", Chunk, "\r\n", Size | Acc]).
to_hex(Val) ->
to_hex(Val, []).
to_hex(0, Acc) ->
Acc;
to_hex(Val, Acc) ->
to_hex(Val div 16, [hex_char(Val rem 16) | Acc]).
hex_char(V) when V < 10 -> $0 + V;
hex_char(V) -> $A + V - 10.
recv_body(ReqId, Acc) ->
receive
{ibrowse_async_response, ReqId, Data} ->
recv_body(ReqId, [Data | Acc]);
{ibrowse_async_response_end, ReqId} ->
iolist_to_binary(lists:reverse(Acc));
Else ->
throw({error, unexpected_mesg, Else})
after 5000 ->
throw({error, timeout})
end.
|