Debian Codegraph @

test/rest_handler_SUITE.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(rest_handler_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20 -import(ct_helper, [doc/1]).
21 -import(cowboy_test, [gun_open/1]).
22
23 %% ct.
24
25 all() ->
26 cowboy_test:common_all().
27
28 groups() ->
29 cowboy_test:common_groups(ct_helper:all(?MODULE)).
30
31 init_per_group(Name, Config) ->
32 cowboy_test:init_common_groups(Name, Config, ?MODULE).
33
34 end_per_group(Name, _) ->
35 cowboy_test:stop_group(Name).
36
37 %% Dispatch configuration.
38
39 init_dispatch(_) ->
40 cowboy_router:compile([{'_', [
41 {"/", rest_hello_h, []},
42 {"/accept_callback", accept_callback_h, []},
43 {"/accept_callback_missing", accept_callback_missing_h, []},
44 {"/charsets_provided", charsets_provided_h, []},
45 {"/charsets_provided_empty", charsets_provided_empty_h, []},
46 {"/charset_in_content_types_provided",
47 charset_in_content_types_provided_h, []},
48 {"/charset_in_content_types_provided_implicit",
49 charset_in_content_types_provided_implicit_h, []},
50 {"/charset_in_content_types_provided_implicit_no_callback",
51 charset_in_content_types_provided_implicit_no_callback_h, []},
52 {"/content_types_accepted", content_types_accepted_h, []},
53 {"/content_types_provided", content_types_provided_h, []},
54 {"/delete_resource", delete_resource_h, []},
55 {"/create_resource", create_resource_h, []},
56 {"/expires", expires_h, []},
57 {"/generate_etag", generate_etag_h, []},
58 {"/if_range", if_range_h, []},
59 {"/last_modified", last_modified_h, []},
60 {"/provide_callback_missing", provide_callback_missing_h, []},
61 {"/provide_range_callback", provide_range_callback_h, []},
62 {"/range_satisfiable", range_satisfiable_h, []},
63 {"/ranges_provided", ranges_provided_h, []},
64 {"/ranges_provided_auto", ranges_provided_auto_h, []},
65 {"/rate_limited", rate_limited_h, []},
66 {"/stop_handler", stop_handler_h, []},
67 {"/switch_handler", switch_handler_h, run},
68 {"/switch_handler_opts", switch_handler_h, hibernate}
69 ]}]).
70
71 %% Internal.
72
73 do_decode(Headers, Body) ->
74 case lists:keyfind(<<"content-encoding">>, 1, Headers) of
75 {_, <<"gzip">>} -> zlib:gunzip(Body);
76 _ -> Body
77 end.
78
79 %% Tests.
80
81 accept_callback_missing(Config) ->
82 doc("A 500 response must be sent when the AcceptCallback can't be called."),
83 ConnPid = gun_open(Config),
84 Ref = gun:put(ConnPid, "/accept_callback_missing", [
85 {<<"accept-encoding">>, <<"gzip">>},
86 {<<"content-type">>, <<"text/plain">>}
87 ], <<"Missing!">>),
88 {response, fin, 500, _} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
89 ok.
90
91 accept_callback_patch_false(Config) ->
92 do_accept_callback_false(Config, patch).
93
94 accept_callback_patch_true(Config) ->
95 do_accept_callback_true(Config, patch).
96
97 accept_callback_post_false(Config) ->
98 do_accept_callback_false(Config, post).
99
100 accept_callback_post_true(Config) ->
101 do_accept_callback_true(Config, post).
102
103 accept_callback_put_false(Config) ->
104 do_accept_callback_false(Config, put).
105
106 accept_callback_put_true(Config) ->
107 do_accept_callback_true(Config, put).
108
109 do_accept_callback_false(Config, Fun) ->
110 doc("When AcceptCallback returns false a 400 response must be returned."),
111 ConnPid = gun_open(Config),
112 Ref = gun:Fun(ConnPid, "/accept_callback?false", [
113 {<<"accept-encoding">>, <<"gzip">>},
114 {<<"content-type">>, <<"text/plain">>}
115 ], <<"Request body.">>),
116 {response, _, 400, _} = gun:await(ConnPid, Ref),
117 ok.
118
119 do_accept_callback_true(Config, Fun) ->
120 doc("When AcceptCallback returns true a 204 response must be returned."),
121 ConnPid = gun_open(Config),
122 Ref = gun:Fun(ConnPid, "/accept_callback?true", [
123 {<<"accept-encoding">>, <<"gzip">>},
124 {<<"content-type">>, <<"text/plain">>}
125 ], <<"Request body.">>),
126 {response, _, 204, _} = gun:await(ConnPid, Ref),
127 ok.
128
129 charset_in_content_types_provided(Config) ->
130 doc("When a charset is matched explicitly in content_types_provided, "
131 "that charset is used and the charsets_provided callback is ignored."),
132 ConnPid = gun_open(Config),
133 Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
134 {<<"accept">>, <<"text/plain;charset=utf-8">>},
135 {<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
136 {<<"accept-encoding">>, <<"gzip">>}
137 ]),
138 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
139 {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
140 ok.
141
142 charset_in_content_types_provided_implicit_match(Config) ->
143 doc("When a charset is matched implicitly in content_types_provided, "
144 "the charsets_provided callback is used to determine if the media "
145 "type will match."),
146 ConnPid = gun_open(Config),
147 Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
148 {<<"accept">>, <<"text/plain;charset=utf-16">>},
149 {<<"accept-encoding">>, <<"gzip">>}
150 ]),
151 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
152 {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
153 ok.
154
155 charset_in_content_types_provided_implicit_nomatch(Config) ->
156 doc("When a charset is matched implicitly in content_types_provided, "
157 "the charsets_provided callback is used to determine if the media "
158 "type will match. If it doesn't, try the next media type."),
159 ConnPid = gun_open(Config),
160 Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
161 {<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
162 {<<"accept-encoding">>, <<"gzip">>}
163 ]),
164 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
165 %% We end up with the first charset listed in charsets_provided.
166 {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
167 ok.
168
169 charset_in_content_types_provided_implicit_nomatch_error(Config) ->
170 doc("When a charset is matched implicitly in content_types_provided, "
171 "the charsets_provided callback is used to determine if the media "
172 "type will match. If it doesn't, and there's no other media type, "
173 "a 406 is returned."),
174 ConnPid = gun_open(Config),
175 Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
176 {<<"accept">>, <<"text/plain;charset=utf-32">>},
177 {<<"accept-encoding">>, <<"gzip">>}
178 ]),
179 {response, _, 406, _} = gun:await(ConnPid, Ref),
180 ok.
181
182 charset_in_content_types_provided_implicit_no_callback(Config) ->
183 doc("When a charset is matched implicitly in content_types_provided, "
184 "and the charsets_provided callback is not exported, the media "
185 "type will match but the charset will be ignored like all other "
186 "parameters."),
187 ConnPid = gun_open(Config),
188 Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
189 {<<"accept">>, <<"text/plain;charset=utf-32">>},
190 {<<"accept-encoding">>, <<"gzip">>}
191 ]),
192 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
193 %% The charset is ignored as if it was any other parameter.
194 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
195 ok.
196
197 charsets_provided_match_text(Config) ->
198 doc("When the media type is text and the charsets_provided callback exists "
199 "and the accept-charset header was sent, the selected charset is sent "
200 "back in the content-type of the response."),
201 ConnPid = gun_open(Config),
202 Ref = gun:get(ConnPid, "/charsets_provided", [
203 {<<"accept">>, <<"text/plain">>},
204 {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
205 {<<"accept-encoding">>, <<"gzip">>}
206 ]),
207 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
208 {_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
209 ok.
210
211 charsets_provided_match_other(Config) ->
212 doc("When the media type is not text and the charsets_provided callback exists "
213 "and the accept-charset header was sent, the selected charset is not sent "
214 "back in the content-type of the response."),
215 ConnPid = gun_open(Config),
216 Ref = gun:get(ConnPid, "/charsets_provided", [
217 {<<"accept">>, <<"application/json">>},
218 {<<"accept-charset">>, <<"utf-8;q=0.5, utf-16">>},
219 {<<"accept-encoding">>, <<"gzip">>}
220 ]),
221 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
222 {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
223 ok.
224
225 charsets_provided_wildcard_text(Config) ->
226 doc("When the media type is text and the charsets_provided callback exists "
227 "and a wildcard accept-charset header was sent, the selected charset is sent "
228 "back in the content-type of the response."),
229 ConnPid = gun_open(Config),
230 Ref = gun:get(ConnPid, "/charsets_provided", [
231 {<<"accept">>, <<"text/plain">>},
232 {<<"accept-charset">>, <<"*">>},
233 {<<"accept-encoding">>, <<"gzip">>}
234 ]),
235 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
236 {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
237 ok.
238
239 charsets_provided_wildcard_other(Config) ->
240 doc("When the media type is not text and the charsets_provided callback exists "
241 "and a wildcard accept-charset header was sent, the selected charset is not sent "
242 "back in the content-type of the response."),
243 ConnPid = gun_open(Config),
244 Ref = gun:get(ConnPid, "/charsets_provided", [
245 {<<"accept">>, <<"application/json">>},
246 {<<"accept-charset">>, <<"*">>},
247 {<<"accept-encoding">>, <<"gzip">>}
248 ]),
249 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
250 {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
251 ok.
252
253 charsets_provided_nomatch(Config) ->
254 doc("Regardless of the media type negotiated, if no charset is found in the "
255 "accept-charset header match a charset configured in charsets_provided, "
256 "then a 406 not acceptable response is sent back."),
257 ConnPid = gun_open(Config),
258 Ref = gun:get(ConnPid, "/charsets_provided", [
259 {<<"accept">>, <<"text/plain">>},
260 {<<"accept-charset">>, <<"utf-8;q=0, iso-8859-1">>},
261 {<<"accept-encoding">>, <<"gzip">>}
262 ]),
263 {response, _, 406, _} = gun:await(ConnPid, Ref),
264 ok.
265
266 charsets_provided_noheader_text(Config) ->
267 doc("When the media type is text and the charsets_provided callback exists "
268 "but the accept-charset header was not sent, the first charset in the "
269 "list is selected and sent back in the content-type of the response."),
270 ConnPid = gun_open(Config),
271 Ref = gun:get(ConnPid, "/charsets_provided", [
272 {<<"accept">>, <<"text/plain">>},
273 {<<"accept-encoding">>, <<"gzip">>}
274 ]),
275 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
276 {_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
277 ok.
278
279 charsets_provided_noheader_other(Config) ->
280 doc("When the media type is not text and the charsets_provided callback exists "
281 "but the accept-charset header was not sent, the first charset in the "
282 "list is selected but is not sent back in the content-type of the response."),
283 ConnPid = gun_open(Config),
284 Ref = gun:get(ConnPid, "/charsets_provided", [
285 {<<"accept">>, <<"application/json">>},
286 {<<"accept-encoding">>, <<"gzip">>}
287 ]),
288 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
289 {_, <<"application/json">>} = lists:keyfind(<<"content-type">>, 1, Headers),
290 ok.
291
292 charsets_provided_empty(Config) ->
293 doc("Regardless of the media type negotiated, if the charsets_provided "
294 "callback returns an empty list a 406 not acceptable response is sent back."),
295 ConnPid = gun_open(Config),
296 Ref = gun:get(ConnPid, "/charsets_provided_empty", [
297 {<<"accept">>, <<"text/plain">>},
298 {<<"accept-charset">>, <<"utf-8q=0.5, utf-16">>},
299 {<<"accept-encoding">>, <<"gzip">>}
300 ]),
301 {response, _, 406, _} = gun:await(ConnPid, Ref),
302 ok.
303
304 charsets_provided_empty_wildcard(Config) ->
305 doc("Regardless of the media type negotiated, if the charsets_provided "
306 "callback returns an empty list a 406 not acceptable response is sent back."),
307 ConnPid = gun_open(Config),
308 Ref = gun:get(ConnPid, "/charsets_provided_empty", [
309 {<<"accept">>, <<"text/plain">>},
310 {<<"accept-charset">>, <<"*">>},
311 {<<"accept-encoding">>, <<"gzip">>}
312 ]),
313 {response, _, 406, _} = gun:await(ConnPid, Ref),
314 ok.
315
316 charsets_provided_empty_noheader(Config) ->
317 doc("Regardless of the media type negotiated, if the charsets_provided "
318 "callback returns an empty list a 406 not acceptable response is sent back."),
319 ConnPid = gun_open(Config),
320 Ref = gun:get(ConnPid, "/charsets_provided_empty", [
321 {<<"accept">>, <<"text/plain">>},
322 {<<"accept-encoding">>, <<"gzip">>}
323 ]),
324 {response, _, 406, _} = gun:await(ConnPid, Ref),
325 ok.
326
327 content_type_invalid(Config) ->
328 doc("An invalid content-type in a POST/PATCH/PUT request "
329 "must be rejected with a 415 unsupported media type response. (RFC7231 6.5.13)"),
330 ConnPid = gun_open(Config),
331 Ref = gun:put(ConnPid, "/content_types_accepted?wildcard-param", [
332 {<<"accept-encoding">>, <<"gzip">>},
333 {<<"content-type">>, <<"text/plain, text/html">>}
334 ]),
335 {response, fin, 415, _} = gun:await(ConnPid, Ref),
336 ok.
337
338 content_types_accepted_ignore_multipart_boundary(Config) ->
339 doc("When a multipart content-type is provided for the request "
340 "body, the boundary parameter is not expected to be returned "
341 "from the content_types_accepted callback and will be "
342 "automatically ignored."),
343 ConnPid = gun_open(Config),
344 Ref = gun:put(ConnPid, "/content_types_accepted?multipart", [
345 {<<"accept-encoding">>, <<"gzip">>},
346 {<<"content-type">>, <<"multipart/mixed; boundary=abcdef; v=1">>}
347 ], <<"Not really multipart!">>),
348 {response, _, 204, _} = gun:await(ConnPid, Ref),
349 ok.
350
351 content_types_accepted_param(Config) ->
352 doc("When a parameter is returned from the content_types_accepted "
353 "callback, and the same parameter is found in the content-type "
354 "header, the negotiation succeeds and the request is processed."),
355 ConnPid = gun_open(Config),
356 Ref = gun:put(ConnPid, "/content_types_accepted?param", [
357 {<<"accept-encoding">>, <<"gzip">>},
358 {<<"content-type">>, <<"text/plain;charset=UTF-8">>}
359 ], "12345"),
360 {response, fin, 204, _} = gun:await(ConnPid, Ref),
361 ok.
362
363 content_types_accepted_wildcard(Config) ->
364 doc("When a wildcard is returned from the content_types_accepted "
365 "callback, any content-type must be accepted."),
366 ConnPid = gun_open(Config),
367 Ref1 = gun:put(ConnPid, "/content_types_accepted?wildcard", [
368 {<<"accept-encoding">>, <<"gzip">>},
369 {<<"content-type">>, <<"text/plain">>}
370 ]),
371 gun:data(ConnPid, Ref1, fin, "Hello world!"),
372 {response, fin, 204, _} = gun:await(ConnPid, Ref1),
373 Ref2 = gun:put(ConnPid, "/content_types_accepted?wildcard", [
374 {<<"accept-encoding">>, <<"gzip">>},
375 {<<"content-type">>, <<"application/vnd.plain;charset=UTF-8">>}
376 ]),
377 gun:data(ConnPid, Ref2, fin, "Hello world!"),
378 {response, fin, 204, _} = gun:await(ConnPid, Ref2),
379 ok.
380
381 content_types_accepted_wildcard_param_no_content_type_param(Config) ->
382 doc("When a wildcard is returned for parameters from the "
383 "content_types_accepted callback, a content-type header "
384 "with no parameters must be accepted."),
385 ConnPid = gun_open(Config),
386 Ref = gun:put(ConnPid, "/content_types_accepted?wildcard-param", [
387 {<<"accept-encoding">>, <<"gzip">>},
388 {<<"content-type">>, <<"text/plain">>}
389 ]),
390 gun:data(ConnPid, Ref, fin, "Hello world!"),
391 {response, fin, 204, _} = gun:await(ConnPid, Ref),
392 ok.
393
394 content_types_accepted_wildcard_param_content_type_with_param(Config) ->
395 doc("When a wildcard is returned for parameters from the "
396 "content_types_accepted callback, a content-type header "
397 "with a parameter must be accepted."),
398 ConnPid = gun_open(Config),
399 Ref = gun:put(ConnPid, "/content_types_accepted?wildcard-param", [
400 {<<"accept-encoding">>, <<"gzip">>},
401 {<<"content-type">>, <<"text/plain; charset=utf-8">>}
402 ]),
403 gun:data(ConnPid, Ref, fin, "Hello world!"),
404 {response, fin, 204, _} = gun:await(ConnPid, Ref),
405 ok.
406
407 content_types_provided_invalid_type(Config) ->
408 doc("When an invalid type is returned from the "
409 "content_types_provided callback, the "
410 "resource is incorrect and a 500 response is expected."),
411 ConnPid = gun_open(Config),
412 Ref = gun:get(ConnPid, "/content_types_provided?invalid-type", [
413 {<<"accept">>, <<"*/*">>},
414 {<<"accept-encoding">>, <<"gzip">>}
415 ]),
416 {response, _, 500, _} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
417 ok.
418
419 content_types_provided_wildcard_param_no_accept_param(Config) ->
420 doc("When a wildcard is returned for parameters from the "
421 "content_types_provided callback, an accept header "
422 "with no parameters must be accepted."),
423 ConnPid = gun_open(Config),
424 Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
425 {<<"accept">>, <<"text/plain">>},
426 {<<"accept-encoding">>, <<"gzip">>}
427 ]),
428 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
429 {ok, <<"[]">>} = gun:await_body(ConnPid, Ref),
430 ok.
431
432 content_types_provided_wildcard_param_accept_with_param(Config) ->
433 doc("When a wildcard is returned for parameters from the "
434 "content_types_provided callback, an accept header "
435 "with a parameter must be accepted."),
436 ConnPid = gun_open(Config),
437 Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
438 {<<"accept">>, <<"text/plain;level=1">>},
439 {<<"accept-encoding">>, <<"gzip">>}
440 ]),
441 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
442 {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref),
443 ok.
444
445 content_types_provided_wildcard_param_accept_with_param_and_qvalue(Config) ->
446 doc("When a wildcard is returned for parameters from the "
447 "content_types_provided callback, an accept header "
448 "with two media types containing parameters including a "
449 "q-value must be accepted. The q-value determines which."),
450 ConnPid = gun_open(Config),
451 Ref1 = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
452 {<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>},
453 {<<"accept-encoding">>, <<"gzip">>}
454 ]),
455 {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
456 {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref1),
457 Ref2 = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
458 {<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>},
459 {<<"accept-encoding">>, <<"gzip">>}
460 ]),
461 {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
462 {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref2),
463 ok.
464
465 content_types_provided_wildcard_param_no_accept_header(Config) ->
466 doc("When a wildcard is returned for parameters from the "
467 "content_types_provided callback, the lack of accept header "
468 "results in the first media type returned being accepted. "
469 "The wildcard must however not be present in the media_type "
470 "value added to the Req object."),
471 ConnPid = gun_open(Config),
472 Ref = gun:get(ConnPid, "/content_types_provided?wildcard-param", [
473 {<<"accept-encoding">>, <<"gzip">>}
474 ]),
475 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
476 {ok, <<"[]">>} = gun:await_body(ConnPid, Ref),
477 ok.
478
479 delete_resource_missing(Config) ->
480 doc("When a resource accepts the DELETE method and the "
481 "delete_resource callback is not exported, the "
482 "resource is incorrect and a 500 response is expected."),
483 ConnPid = gun_open(Config),
484 Ref = gun:delete(ConnPid, "/delete_resource?missing", [
485 {<<"accept-encoding">>, <<"gzip">>}
486 ]),
487 {response, _, 500, _} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
488 ok.
489
490 create_resource_created(Config) ->
491 doc("POST to an existing resource to create a new resource. "
492 "When the accept callback returns {created, NewURI}, "
493 "the expected reply is 201 Created."),
494 ConnPid = gun_open(Config),
495 Ref = gun:post(ConnPid, "/create_resource?created", [
496 {<<"content-type">>, <<"application/text">>}
497 ], <<"hello">>, #{}),
498 {response, _, 201, _} = gun:await(ConnPid, Ref),
499 ok.
500
501 create_resource_see_other(Config) ->
502 doc("POST to an existing resource to create a new resource. "
503 "When the accept callback returns {see_other, NewURI}, "
504 "the expected reply is 303 See Other with a location header set."),
505 ConnPid = gun_open(Config),
506 Ref = gun:post(ConnPid, "/create_resource?see_other", [
507 {<<"content-type">>, <<"application/text">>}
508 ], <<"hello">>, #{}),
509 {response, _, 303, RespHeaders} = gun:await(ConnPid, Ref),
510 {_, _} = lists:keyfind(<<"location">>, 1, RespHeaders),
511 ok.
512
513 error_on_malformed_accept(Config) ->
514 doc("A malformed Accept header must result in a 400 response."),
515 do_error_on_malformed_header(Config, <<"accept">>).
516
517 error_on_malformed_if_match(Config) ->
518 doc("A malformed If-Match header must result in a 400 response."),
519 do_error_on_malformed_header(Config, <<"if-match">>).
520
521 error_on_malformed_if_none_match(Config) ->
522 doc("A malformed If-None-Match header must result in a 400 response."),
523 do_error_on_malformed_header(Config, <<"if-none-match">>).
524
525 do_error_on_malformed_header(Config, Name) ->
526 ConnPid = gun_open(Config),
527 Ref = gun:get(ConnPid, "/", [
528 {<<"accept-encoding">>, <<"gzip">>},
529 {Name, <<"bad">>}
530 ]),
531 {response, _, 400, _} = gun:await(ConnPid, Ref),
532 ok.
533
534 expires_binary(Config) ->
535 doc("The expires header can also be given as a binary "
536 "to indicate a date in the past. (RFC7234 5.3)"),
537 ConnPid = gun_open(Config),
538 Ref = gun:get(ConnPid, "/expires?binary", [
539 {<<"accept-encoding">>, <<"gzip">>}
540 ]),
541 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
542 {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers),
543 ok.
544
545 expires_missing(Config) ->
546 doc("The expires header must not be sent when the callback is not exported."),
547 ConnPid = gun_open(Config),
548 Ref = gun:get(ConnPid, "/expires?missing", [
549 {<<"accept-encoding">>, <<"gzip">>}
550 ]),
551 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
552 false = lists:keyfind(<<"expires">>, 1, Headers),
553 ok.
554
555 expires_tuple(Config) ->
556 doc("The expires header can be given as a date tuple. (RFC7234 5.3)"),
557 ConnPid = gun_open(Config),
558 Ref = gun:get(ConnPid, "/expires?tuple", [
559 {<<"accept-encoding">>, <<"gzip">>}
560 ]),
561 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
562 {_, <<"Fri, 21 Sep 2012 22:36:14 GMT">>} = lists:keyfind(<<"expires">>, 1, Headers),
563 ok.
564
565 expires_undefined(Config) ->
566 doc("The expires header must not be sent when undefined is returned."),
567 ConnPid = gun_open(Config),
568 Ref = gun:get(ConnPid, "/expires?undefined", [
569 {<<"accept-encoding">>, <<"gzip">>}
570 ]),
571 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
572 false = lists:keyfind(<<"expires">>, 1, Headers),
573 ok.
574
575 generate_etag_missing(Config) ->
576 doc("The etag header must not be sent when "
577 "the generate_etag callback is not exported."),
578 ConnPid = gun_open(Config),
579 Ref = gun:get(ConnPid, "/generate_etag?missing", [
580 {<<"accept-encoding">>, <<"gzip">>}
581 ]),
582 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
583 false = lists:keyfind(<<"etag">>, 1, Headers),
584 ok.
585
586 generate_etag_undefined(Config) ->
587 doc("The etag header must not be sent when "
588 "the generate_etag callback returns undefined."),
589 ConnPid = gun_open(Config),
590 Ref = gun:get(ConnPid, "/generate_etag?undefined", [
591 {<<"accept-encoding">>, <<"gzip">>}
592 ]),
593 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
594 false = lists:keyfind(<<"etag">>, 1, Headers),
595 ok.
596
597 generate_etag_binary_strong(Config) ->
598 doc("The etag header must be sent when the generate_etag "
599 "callback returns a strong binary. (RFC7232 2.3)"),
600 do_generate_etag(Config, "binary-strong-quoted",
601 [], 200, {<<"etag">>, <<"\"etag-header-value\"">>}).
602
603 generate_etag_binary_weak(Config) ->
604 doc("The etag header must be sent when the generate_etag "
605 "callback returns a weak binary. (RFC7232 2.3)"),
606 do_generate_etag(Config, "binary-weak-quoted",
607 [], 200, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
608
609 generate_etag_invalid_binary_strong_unquoted(Config) ->
610 doc("When Cowboy cannot parse the generate_etag callback's "
611 "return value, a 500 response is returned without the etag header."),
612 do_generate_etag(Config, "binary-strong-unquoted", [], 500, false).
613
614 generate_etag_invalid_binary_weak_unquoted(Config) ->
615 doc("When Cowboy cannot parse the generate_etag callback's "
616 "return value, a 500 response is returned without the etag header."),
617 do_generate_etag(Config, "binary-weak-unquoted", [], 500, false).
618
619 generate_etag_tuple_strong(Config) ->
620 doc("The etag header must be sent when the generate_etag "
621 "callback returns a strong tuple. (RFC7232 2.3)"),
622 do_generate_etag(Config, "tuple-strong",
623 [], 200, {<<"etag">>, <<"\"etag-header-value\"">>}).
624
625 generate_etag_tuple_weak(Config) ->
626 doc("The etag header must be sent when the generate_etag "
627 "callback returns a weak tuple. (RFC7232 2.3)"),
628 do_generate_etag(Config, "tuple-weak",
629 [], 200, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
630
631 if_none_match_binary_strong(Config) ->
632 doc("When the if-none-match header matches a strong etag, "
633 "a 304 not modified response is returned. (RFC7232 3.2)"),
634 do_generate_etag(Config, "binary-strong-quoted",
635 [{<<"if-none-match">>, <<"\"etag-header-value\"">>}],
636 304, {<<"etag">>, <<"\"etag-header-value\"">>}).
637
638 if_none_match_binary_weak(Config) ->
639 doc("When the if-none-match header matches a weak etag, "
640 "a 304 not modified response is returned. (RFC7232 3.2)"),
641 do_generate_etag(Config, "binary-weak-quoted",
642 [{<<"if-none-match">>, <<"W/\"etag-header-value\"">>}],
643 304, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
644
645 if_none_match_tuple_strong(Config) ->
646 doc("When the if-none-match header matches a strong etag, "
647 "a 304 not modified response is returned. (RFC7232 3.2)"),
648 do_generate_etag(Config, "tuple-strong",
649 [{<<"if-none-match">>, <<"\"etag-header-value\"">>}],
650 304, {<<"etag">>, <<"\"etag-header-value\"">>}).
651
652 if_none_match_tuple_weak(Config) ->
653 doc("When the if-none-match header matches a weak etag, "
654 "a 304 not modified response is returned. (RFC7232 3.2)"),
655 do_generate_etag(Config, "tuple-weak",
656 [{<<"if-none-match">>, <<"W/\"etag-header-value\"">>}],
657 304, {<<"etag">>, <<"W/\"etag-header-value\"">>}).
658
659 do_generate_etag(Config, Qs, ReqHeaders, Status, Etag) ->
660 ConnPid = gun_open(Config),
661 Ref = gun:get(ConnPid, "/generate_etag?" ++ Qs, [
662 {<<"accept-encoding">>, <<"gzip">>}
663 |ReqHeaders
664 ]),
665 {response, _, Status, RespHeaders} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
666 Etag = lists:keyfind(<<"etag">>, 1, RespHeaders),
667 ok.
668
669 %% See do_maybe_h3_error2 comment.
670 do_maybe_h3_error({error, {stream_error, {stream_error, h3_internal_error, _}}}) ->
671 {response, fin, 500, []};
672 do_maybe_h3_error(Result) ->
673 Result.
674
675 if_range_etag_equal(Config) ->
676 doc("When the if-range header matches, a 206 partial content "
677 "response is expected for an otherwise valid range request. (RFC7233 3.2)"),
678 ConnPid = gun_open(Config),
679 Ref = gun:get(ConnPid, "/if_range", [
680 {<<"accept-encoding">>, <<"gzip">>},
681 {<<"range">>, <<"bytes=0-">>},
682 {<<"if-range">>, <<"\"strong-and-match\"">>}
683 ]),
684 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
685 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
686 {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
687 ok.
688
689 if_range_etag_not_equal(Config) ->
690 doc("When the if-range header does not match, the range header "
691 "must be ignored and a 200 OK response is expected for "
692 "an otherwise valid range request. (RFC7233 3.2)"),
693 ConnPid = gun_open(Config),
694 Ref = gun:get(ConnPid, "/if_range", [
695 {<<"accept-encoding">>, <<"gzip">>},
696 {<<"range">>, <<"bytes=0-">>},
697 {<<"if-range">>, <<"\"strong-but-no-match\"">>}
698 ]),
699 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
700 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
701 false = lists:keyfind(<<"content-range">>, 1, Headers),
702 ok.
703
704 if_range_ignored_when_no_range_header(Config) ->
705 doc("When there is no range header the if-range header is ignored "
706 "and a 200 OK response is expected (RFC7233 3.2)"),
707 ConnPid = gun_open(Config),
708 Ref = gun:get(ConnPid, "/if_range", [
709 {<<"accept-encoding">>, <<"gzip">>},
710 {<<"if-range">>, <<"\"strong-and-match\"">>}
711 ]),
712 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
713 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
714 false = lists:keyfind(<<"content-range">>, 1, Headers),
715 ok.
716
717 if_range_ignored_when_ranges_provided_missing(Config) ->
718 doc("When the resource does not support range requests "
719 "the range and if-range headers must be ignored"
720 "and a 200 OK response is expected (RFC7233 3.2)"),
721 ConnPid = gun_open(Config),
722 Ref = gun:get(ConnPid, "/if_range?missing-ranges_provided", [
723 {<<"accept-encoding">>, <<"gzip">>},
724 {<<"range">>, <<"bytes=0-">>},
725 {<<"if-range">>, <<"\"strong-and-match\"">>}
726 ]),
727 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
728 false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
729 false = lists:keyfind(<<"content-range">>, 1, Headers),
730 ok.
731
732 if_range_ignored_when_ranges_provided_empty(Config) ->
733 doc("When the resource does not support range requests "
734 "the range and if-range headers must be ignored"
735 "and a 200 OK response is expected (RFC7233 3.2)"),
736 ConnPid = gun_open(Config),
737 Ref = gun:get(ConnPid, "/if_range?empty-ranges_provided", [
738 {<<"accept-encoding">>, <<"gzip">>},
739 {<<"range">>, <<"bytes=0-">>},
740 {<<"if-range">>, <<"\"strong-and-match\"">>}
741 ]),
742 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
743 {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
744 false = lists:keyfind(<<"content-range">>, 1, Headers),
745 ok.
746
747 if_range_weak_etag_not_equal(Config) ->
748 doc("The if-range header must not match weak etags; the range header "
749 "must be ignored and a 200 OK response is expected for "
750 "an otherwise valid range request. (RFC7233 3.2)"),
751 ConnPid = gun_open(Config),
752 Ref = gun:get(ConnPid, "/if_range?weak-etag", [
753 {<<"accept-encoding">>, <<"gzip">>},
754 {<<"range">>, <<"bytes=0-">>},
755 {<<"if-range">>, <<"W/\"weak-no-match\"">>}
756 ]),
757 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
758 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
759 false = lists:keyfind(<<"content-range">>, 1, Headers),
760 ok.
761
762 if_range_date_not_equal(Config) ->
763 doc("The if-range header must not match weak dates. Cowboy "
764 "currently has no way of knowing whether a resource was "
765 "updated twice within the same second. The range header "
766 "must be ignored and a 200 OK response is expected for "
767 "an otherwise valid range request. (RFC7233 3.2)"),
768 ConnPid = gun_open(Config),
769 Ref = gun:get(ConnPid, "/if_range", [
770 {<<"accept-encoding">>, <<"gzip">>},
771 {<<"range">>, <<"bytes=0-">>},
772 {<<"if-range">>, <<"Fri, 22 Feb 2222 11:11:11 GMT">>}
773 ]),
774 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
775 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
776 false = lists:keyfind(<<"content-range">>, 1, Headers),
777 ok.
778
779 last_modified(Config) ->
780 doc("The last-modified header can be given as a date tuple. (RFC7232 2.2)"),
781 ConnPid = gun_open(Config),
782 Ref = gun:get(ConnPid, "/last_modified?tuple", [
783 {<<"accept-encoding">>, <<"gzip">>}
784 ]),
785 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
786 {_, <<"Fri, 21 Sep 2012 22:36:14 GMT">>} = lists:keyfind(<<"last-modified">>, 1, Headers),
787 ok.
788
789 last_modified_missing(Config) ->
790 doc("The last-modified header must not be sent when the callback is not exported."),
791 ConnPid = gun_open(Config),
792 Ref = gun:get(ConnPid, "/last_modified?missing", [
793 {<<"accept-encoding">>, <<"gzip">>}
794 ]),
795 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
796 false = lists:keyfind(<<"last-modified">>, 1, Headers),
797 ok.
798
799 last_modified_undefined(Config) ->
800 doc("The last-modified header must not be sent when the callback returns undefined."),
801 ConnPid = gun_open(Config),
802 Ref = gun:get(ConnPid, "/last_modified?undefined", [
803 {<<"accept-encoding">>, <<"gzip">>}
804 ]),
805 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
806 false = lists:keyfind(<<"last-modified">>, 1, Headers),
807 ok.
808
809 options_missing(Config) ->
810 doc("A successful OPTIONS request to a simple handler results in "
811 "a 200 OK response with the allow header set. (RFC7231 4.3.7)"),
812 ConnPid = gun_open(Config),
813 Ref = gun:options(ConnPid, "/", [
814 {<<"accept-encoding">>, <<"gzip">>}
815 ]),
816 {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
817 {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
818 ok.
819
820 provide_callback(Config) ->
821 doc("A successful GET request to a simple handler results in "
822 "a 200 OK response with the content-type set. (RFC7231 4.3.1, RFC7231 6.3.1)"),
823 ConnPid = gun_open(Config),
824 Ref = gun:get(ConnPid, "/", [
825 {<<"accept">>, <<"*/*">>},
826 {<<"accept-encoding">>, <<"gzip">>}
827 ]),
828 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
829 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
830 {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
831 {ok, <<"This is REST!">>} = gun:await_body(ConnPid, Ref),
832 ok.
833
834 provide_callback_missing(Config) ->
835 doc("A 500 response must be sent when the ProvideCallback can't be called."),
836 ConnPid = gun_open(Config),
837 Ref = gun:get(ConnPid, "/provide_callback_missing", [{<<"accept-encoding">>, <<"gzip">>}]),
838 {response, fin, 500, _} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
839 ok.
840
841 provide_range_callback(Config) ->
842 doc("A successful request for a single range results in a "
843 "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
844 ConnPid = gun_open(Config),
845 Ref = gun:get(ConnPid, "/provide_range_callback", [
846 {<<"accept-encoding">>, <<"gzip">>},
847 {<<"range">>, <<"bytes=0-">>}
848 ]),
849 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
850 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
851 {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
852 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
853 {ok, <<"This is ranged REST!">>} = gun:await_body(ConnPid, Ref),
854 ok.
855
856 provide_range_callback_sendfile(Config) ->
857 doc("A successful request for a single range results in a "
858 "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
859 ConnPid = gun_open(Config),
860 Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
861 {<<"accept-encoding">>, <<"gzip">>},
862 {<<"range">>, <<"bytes=0-">>}
863 ]),
864 Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
865 Size = filelib:file_size(Path),
866 {ok, Body} = file:read_file(Path),
867 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
868 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
869 {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
870 ContentRange = iolist_to_binary([
871 <<"bytes 0-">>,
872 integer_to_binary(Size - 1),
873 <<"/">>,
874 integer_to_binary(Size)
875 ]),
876 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
877 {ok, Body} = gun:await_body(ConnPid, Ref),
878 ok.
879
880 provide_range_callback_multipart(Config) ->
881 doc("A successful request for multiple ranges results in a "
882 "206 partial content response using the multipart/byteranges "
883 "content-type and the content-range not being set. The real "
884 "content-type and content-range of the parts can be found in "
885 "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
886 ConnPid = gun_open(Config),
887 Ref = gun:get(ConnPid, "/provide_range_callback", [
888 {<<"accept-encoding">>, <<"gzip">>},
889 %% This range selects everything except the space characters.
890 {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
891 ]),
892 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
893 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
894 false = lists:keyfind(<<"content-range">>, 1, Headers),
895 {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
896 = lists:keyfind(<<"content-type">>, 1, Headers),
897 {ok, Body0} = gun:await_body(ConnPid, Ref),
898 Body = do_decode(Headers, Body0),
899 {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
900 [
901 {bytes, 0, 3, 20},
902 {bytes, 5, 6, 20},
903 {bytes, 8, 13, 20},
904 {bytes, 15, 19, 20}
905 ] = ContentRanges,
906 <<"ThisisrangedREST!">> = BodyAcc,
907 ok.
908
909 provide_range_callback_multipart_sendfile(Config) ->
910 doc("A successful request for multiple ranges results in a "
911 "206 partial content response using the multipart/byteranges "
912 "content-type and the content-range not being set. The real "
913 "content-type and content-range of the parts can be found in "
914 "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
915 ConnPid = gun_open(Config),
916 Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
917 {<<"accept-encoding">>, <<"gzip">>},
918 %% This range selects a few random chunks of the file.
919 {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
920 ]),
921 Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
922 Size = filelib:file_size(Path),
923 Skip = Size - 399,
924 {ok, <<
925 _:50/binary, Body1:50/binary,
926 _:50/binary, Body2:50/binary,
927 _:50/binary, Body3:50/binary,
928 _:Skip/binary, Body4/bits>>} = file:read_file(Path),
929 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
930 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
931 false = lists:keyfind(<<"content-range">>, 1, Headers),
932 {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
933 = lists:keyfind(<<"content-type">>, 1, Headers),
934 {ok, Body0} = gun:await_body(ConnPid, Ref),
935 Body = do_decode(Headers, Body0),
936 %% We will receive the ranges in the same order as requested.
937 {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
938 LastFrom = 300 + Skip,
939 LastTo = Size - 1,
940 [
941 {bytes, 50, 99, Size},
942 {bytes, 150, 199, Size},
943 {bytes, 250, 299, Size},
944 {bytes, LastFrom, LastTo, Size}
945 ] = ContentRanges,
946 BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
947 ok.
948
949 do_provide_range_callback_multipart_body(Rest, Boundary, ContentRangesAcc, BodyAcc) ->
950 case cow_multipart:parse_headers(Rest, Boundary) of
951 {ok, Headers, Rest1} ->
952 {_, ContentRange0} = lists:keyfind(<<"content-range">>, 1, Headers),
953 ContentRange = cow_http_hd:parse_content_range(ContentRange0),
954 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
955 case cow_multipart:parse_body(Rest1, Boundary) of
956 {done, Body} ->
957 do_provide_range_callback_multipart_body(<<>>, Boundary,
958 [ContentRange|ContentRangesAcc],
959 <<BodyAcc/binary, Body/binary>>);
960 {done, Body, Rest2} ->
961 do_provide_range_callback_multipart_body(Rest2, Boundary,
962 [ContentRange|ContentRangesAcc],
963 <<BodyAcc/binary, Body/binary>>)
964 end;
965 {done, <<>>} ->
966 {lists:reverse(ContentRangesAcc), BodyAcc}
967 end.
968
969 provide_range_callback_metadata(Config) ->
970 doc("A successful request for a single range results in a "
971 "206 partial content response with the same headers that "
972 "a normal 200 OK response would, like vary or etag. (RFC7233 4.1)"),
973 ConnPid = gun_open(Config),
974 Ref = gun:get(ConnPid, "/provide_range_callback", [
975 {<<"accept-encoding">>, <<"gzip">>},
976 {<<"range">>, <<"bytes=0-">>}
977 ]),
978 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
979 {_, _} = lists:keyfind(<<"date">>, 1, Headers),
980 {_, _} = lists:keyfind(<<"etag">>, 1, Headers),
981 {_, _} = lists:keyfind(<<"expires">>, 1, Headers),
982 {_, _} = lists:keyfind(<<"last-modified">>, 1, Headers),
983 {_, _} = lists:keyfind(<<"vary">>, 1, Headers),
984 %% Also cache-control and content-location but we don't send those.
985 ok.
986
987 provide_range_callback_missing(Config) ->
988 doc("A 500 response must be sent when the ProvideRangeCallback can't be called."),
989 ConnPid = gun_open(Config),
990 Ref = gun:get(ConnPid, "/provide_range_callback?missing", [
991 {<<"accept-encoding">>, <<"gzip">>},
992 {<<"range">>, <<"bytes=0-">>}
993 ]),
994 {response, fin, 500, _} = do_maybe_h3_error(gun:await(ConnPid, Ref)),
995 ok.
996
997 range_ignore_unknown_unit(Config) ->
998 doc("The range header must be ignored when the range unit "
999 "is not found in ranges_provided. (RFC7233 3.1)"),
1000 ConnPid = gun_open(Config),
1001 Ref = gun:get(ConnPid, "/if_range", [
1002 {<<"accept-encoding">>, <<"gzip">>},
1003 {<<"range">>, <<"chapters=1-">>}
1004 ]),
1005 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
1006 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1007 false = lists:keyfind(<<"content-range">>, 1, Headers),
1008 ok.
1009
1010 range_ignore_when_not_modified(Config) ->
1011 doc("The range header must be ignored when a conditional "
1012 "GET results in a 304 not modified response. (RFC7233 3.1)"),
1013 ConnPid = gun_open(Config),
1014 Ref = gun:get(ConnPid, "/if_range", [
1015 {<<"accept-encoding">>, <<"gzip">>},
1016 {<<"range">>, <<"bytes=0-">>},
1017 {<<"if-none-match">>, <<"\"strong-and-match\"">>}
1018 ]),
1019 {response, fin, 304, Headers} = gun:await(ConnPid, Ref),
1020 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1021 false = lists:keyfind(<<"content-range">>, 1, Headers),
1022 ok.
1023
1024 range_satisfiable(Config) ->
1025 doc("When the range_satisfiable callback returns true "
1026 "a 206 partial content response is expected for "
1027 "an otherwise valid range request. (RFC7233 4.1)"),
1028 ConnPid = gun_open(Config),
1029 Ref = gun:get(ConnPid, "/range_satisfiable?true", [
1030 {<<"accept-encoding">>, <<"gzip">>},
1031 {<<"range">>, <<"bytes=0-">>}
1032 ]),
1033 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
1034 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1035 {_, <<"bytes 0-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
1036 ok.
1037
1038 range_not_satisfiable(Config) ->
1039 doc("When the range_satisfiable callback returns false "
1040 "a 416 range not satisfiable response is expected for "
1041 "an otherwise valid range request. (RFC7233 4.4)"),
1042 ConnPid = gun_open(Config),
1043 Ref = gun:get(ConnPid, "/range_satisfiable?false", [
1044 {<<"accept-encoding">>, <<"gzip">>},
1045 {<<"range">>, <<"bytes=0-">>}
1046 ]),
1047 {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
1048 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1049 false = lists:keyfind(<<"content-range">>, 1, Headers),
1050 ok.
1051
1052 range_not_satisfiable_int(Config) ->
1053 doc("When the range_satisfiable callback returns false "
1054 "a 416 range not satisfiable response is expected for "
1055 "an otherwise valid range request. If an integer is "
1056 "provided it is used to construct the content-range "
1057 "header. (RFC7233 4.2, RFC7233 4.4)"),
1058 ConnPid = gun_open(Config),
1059 Ref = gun:get(ConnPid, "/range_satisfiable?false-int", [
1060 {<<"accept-encoding">>, <<"gzip">>},
1061 {<<"range">>, <<"bytes=0-">>}
1062 ]),
1063 {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
1064 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1065 {_, <<"bytes */123">>} = lists:keyfind(<<"content-range">>, 1, Headers),
1066 ok.
1067
1068 range_not_satisfiable_bin(Config) ->
1069 doc("When the range_satisfiable callback returns false "
1070 "a 416 range not satisfiable response is expected for "
1071 "an otherwise valid range request. If a binary is "
1072 "provided it is used to construct the content-range "
1073 "header. (RFC7233 4.2, RFC7233 4.4)"),
1074 ConnPid = gun_open(Config),
1075 Ref = gun:get(ConnPid, "/range_satisfiable?false-bin", [
1076 {<<"accept-encoding">>, <<"gzip">>},
1077 {<<"range">>, <<"bytes=0-">>}
1078 ]),
1079 {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
1080 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1081 {_, <<"bytes */456">>} = lists:keyfind(<<"content-range">>, 1, Headers),
1082 ok.
1083
1084 range_satisfiable_missing(Config) ->
1085 doc("When the range_satisfiable callback is missing "
1086 "a 206 partial content response is expected for "
1087 "an otherwise valid range request. (RFC7233 4.1)"),
1088 ConnPid = gun_open(Config),
1089 Ref = gun:get(ConnPid, "/range_satisfiable?missing", [
1090 {<<"accept-encoding">>, <<"gzip">>},
1091 {<<"range">>, <<"bytes=0-">>}
1092 ]),
1093 {response, _, 206, Headers} = gun:await(ConnPid, Ref),
1094 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1095 {_, <<"bytes ", _/bits>>} = lists:keyfind(<<"content-range">>, 1, Headers),
1096 ok.
1097
1098 ranges_provided_accept_ranges(Config) ->
1099 doc("When the ranges_provided callback exists the accept-ranges header "
1100 "is sent in the response. (RFC7233 2.3)"),
1101 ConnPid = gun_open(Config),
1102 Ref = gun:get(ConnPid, "/ranges_provided?list", [{<<"accept-encoding">>, <<"gzip">>}]),
1103 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
1104 {_, <<"bytes, pages, chapters">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1105 ok.
1106
1107 %% @todo Probably should have options to do this automatically for auto at least.
1108 %%
1109 %% A server that supports range requests MAY ignore or reject a Range
1110 %% header field that consists of more than two overlapping ranges, or a
1111 %% set of many small ranges that are not listed in ascending order,
1112 %% since both are indications of either a broken client or a deliberate
1113 %% denial-of-service attack (Section 6.1).
1114
1115 %% @todo Probably should have options for auto as well to join ranges that
1116 %% are very close from each other.
1117
1118 ranges_provided_auto_data(Config) ->
1119 doc("When the unit range is bytes and the callback is 'auto' "
1120 "Cowboy will call the normal ProvideCallback and perform "
1121 "the range calculations automatically."),
1122 ConnPid = gun_open(Config),
1123 Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
1124 {<<"accept-encoding">>, <<"gzip">>},
1125 {<<"range">>, <<"bytes=8-">>}
1126 ]),
1127 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
1128 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1129 {_, <<"bytes 8-19/20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
1130 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
1131 {ok, <<"ranged REST!">>} = gun:await_body(ConnPid, Ref),
1132 ok.
1133
1134 ranges_provided_auto_sendfile(Config) ->
1135 doc("When the unit range is bytes and the callback is 'auto' "
1136 "Cowboy will call the normal ProvideCallback and perform "
1137 "the range calculations automatically."),
1138 ConnPid = gun_open(Config),
1139 Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
1140 {<<"accept-encoding">>, <<"gzip">>},
1141 {<<"range">>, <<"bytes=8-">>}
1142 ]),
1143 Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
1144 Size = filelib:file_size(Path),
1145 {ok, <<_:8/binary, Body/bits>>} = file:read_file(Path),
1146 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
1147 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1148 {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
1149 ContentRange = iolist_to_binary([
1150 <<"bytes 8-">>,
1151 integer_to_binary(Size - 1),
1152 <<"/">>,
1153 integer_to_binary(Size)
1154 ]),
1155 {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
1156 {ok, Body} = gun:await_body(ConnPid, Ref),
1157 ok.
1158
1159 ranges_provided_auto_multipart_data(Config) ->
1160 doc("When the unit range is bytes and the callback is 'auto' "
1161 "Cowboy will call the normal ProvideCallback and perform "
1162 "the range calculations automatically."),
1163 ConnPid = gun_open(Config),
1164 Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
1165 {<<"accept-encoding">>, <<"gzip">>},
1166 %% This range selects everything except the space characters.
1167 {<<"range">>, <<"bytes=0-3, 5-6, 8-13, 15-">>}
1168 ]),
1169 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
1170 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1171 false = lists:keyfind(<<"content-range">>, 1, Headers),
1172 {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
1173 = lists:keyfind(<<"content-type">>, 1, Headers),
1174 {ok, Body0} = gun:await_body(ConnPid, Ref),
1175 Body = do_decode(Headers, Body0),
1176 %% We will receive the ranges in the same order as requested.
1177 {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
1178 [
1179 {bytes, 0, 3, 20},
1180 {bytes, 5, 6, 20},
1181 {bytes, 8, 13, 20},
1182 {bytes, 15, 19, 20}
1183 ] = ContentRanges,
1184 <<"ThisisrangedREST!">> = BodyAcc,
1185 ok.
1186
1187 ranges_provided_auto_multipart_sendfile(Config) ->
1188 doc("When the unit range is bytes and the callback is 'auto' "
1189 "Cowboy will call the normal ProvideCallback and perform "
1190 "the range calculations automatically."),
1191 ConnPid = gun_open(Config),
1192 Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
1193 {<<"accept-encoding">>, <<"gzip">>},
1194 %% This range selects a few random chunks of the file.
1195 {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
1196 ]),
1197 Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
1198 Size = filelib:file_size(Path),
1199 Skip = Size - 399,
1200 {ok, <<
1201 _:50/binary, Body1:50/binary,
1202 _:50/binary, Body2:50/binary,
1203 _:50/binary, Body3:50/binary,
1204 _:Skip/binary, Body4/bits>>} = file:read_file(Path),
1205 {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
1206 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1207 false = lists:keyfind(<<"content-range">>, 1, Headers),
1208 {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
1209 = lists:keyfind(<<"content-type">>, 1, Headers),
1210 {ok, Body0} = gun:await_body(ConnPid, Ref),
1211 Body = do_decode(Headers, Body0),
1212 %% We will receive the ranges in the same order as requested.
1213 {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
1214 LastFrom = 300 + Skip,
1215 LastTo = Size - 1,
1216 [
1217 {bytes, 50, 99, Size},
1218 {bytes, 150, 199, Size},
1219 {bytes, 250, 299, Size},
1220 {bytes, LastFrom, LastTo, Size}
1221 ] = ContentRanges,
1222 BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
1223 ok.
1224
1225 ranges_provided_auto_not_satisfiable_data(Config) ->
1226 doc("When the unit range is bytes and the callback is 'auto' "
1227 "Cowboy will call the normal ProvideCallback and perform "
1228 "the range calculations automatically. When the requested "
1229 "range is not satisfiable a 416 range not satisfiable response "
1230 "is expected. The content-range header will be set. (RFC7233 4.4)"),
1231 ConnPid = gun_open(Config),
1232 Ref = gun:get(ConnPid, "/ranges_provided_auto?data", [
1233 {<<"accept-encoding">>, <<"gzip">>},
1234 {<<"range">>, <<"bytes=1000-">>}
1235 ]),
1236 {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
1237 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1238 {_, <<"bytes */20">>} = lists:keyfind(<<"content-range">>, 1, Headers),
1239 ok.
1240
1241 ranges_provided_auto_not_satisfiable_sendfile(Config) ->
1242 doc("When the unit range is bytes and the callback is 'auto' "
1243 "Cowboy will call the normal ProvideCallback and perform "
1244 "the range calculations automatically. When the requested "
1245 "range is not satisfiable a 416 range not satisfiable response "
1246 "is expected. The content-range header will be set. (RFC7233 4.4)"),
1247 ConnPid = gun_open(Config),
1248 Ref = gun:get(ConnPid, "/ranges_provided_auto?sendfile", [
1249 {<<"accept-encoding">>, <<"gzip">>},
1250 {<<"range">>, <<"bytes=1000-">>}
1251 ]),
1252 {response, fin, 416, Headers} = gun:await(ConnPid, Ref),
1253 {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1254 Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
1255 Size = filelib:file_size(Path),
1256 ContentRange = iolist_to_binary([<<"bytes */">>, integer_to_binary(Size)]),
1257 {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
1258 ok.
1259
1260 ranges_provided_empty_accept_ranges_none(Config) ->
1261 doc("When the ranges_provided callback exists but returns an empty list "
1262 "the accept-ranges header is sent in the response with the value none. (RFC7233 2.3)"),
1263 ConnPid = gun_open(Config),
1264 Ref = gun:get(ConnPid, "/ranges_provided?none", [{<<"accept-encoding">>, <<"gzip">>}]),
1265 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
1266 {_, <<"none">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1267 ok.
1268
1269 ranges_provided_missing_no_accept_ranges(Config) ->
1270 doc("When the ranges_provided callback does not exist "
1271 "the accept-ranges header is not sent in the response."),
1272 ConnPid = gun_open(Config),
1273 Ref = gun:get(ConnPid, "/ranges_provided?missing", [{<<"accept-encoding">>, <<"gzip">>}]),
1274 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
1275 false = lists:keyfind(<<"accept-ranges">>, 1, Headers),
1276 ok.
1277
1278 rate_limited(Config) ->
1279 doc("A 429 response must be sent when the rate_limited callback returns true. "
1280 "The retry-after header is specified as an integer. (RFC6585 4, RFC7231 7.1.3)"),
1281 ConnPid = gun_open(Config),
1282 Ref = gun:get(ConnPid, "/rate_limited?true", [{<<"accept-encoding">>, <<"gzip">>}]),
1283 {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
1284 {_, <<"3600">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
1285 ok.
1286
1287 rate_limited_datetime(Config) ->
1288 doc("A 429 response must be sent when the rate_limited callback returns true. "
1289 "The retry-after header is specified as a date/time tuple. (RFC6585 4, RFC7231 7.1.3)"),
1290 ConnPid = gun_open(Config),
1291 Ref = gun:get(ConnPid, "/rate_limited?true-date", [{<<"accept-encoding">>, <<"gzip">>}]),
1292 {response, fin, 429, Headers} = gun:await(ConnPid, Ref),
1293 {_, <<"Fri, 22 Feb 2222 11:11:11 GMT">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
1294 ok.
1295
1296 rate_not_limited(Config) ->
1297 doc("A success response must be sent when the rate_limited callback returns false."),
1298 ConnPid = gun_open(Config),
1299 Ref = gun:get(ConnPid, "/rate_limited?false", [{<<"accept-encoding">>, <<"gzip">>}]),
1300 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
1301 ok.
1302
1303 stop_handler_allowed_methods(Config) ->
1304 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1305
1306 stop_handler_allow_missing_post(Config) ->
1307 do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
1308
1309 stop_handler_charsets_provided(Config) ->
1310 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1311
1312 stop_handler_content_types_accepted(Config) ->
1313 do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
1314
1315 stop_handler_content_types_provided(Config) ->
1316 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1317
1318 stop_handler_delete_completed(Config) ->
1319 do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
1320
1321 stop_handler_delete_resource(Config) ->
1322 do_no_body_stop_handler(Config, delete, ?FUNCTION_NAME).
1323
1324 stop_handler_forbidden(Config) ->
1325 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1326
1327 stop_handler_is_authorized(Config) ->
1328 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1329
1330 stop_handler_is_conflict(Config) ->
1331 do_req_body_stop_handler(Config, put, ?FUNCTION_NAME).
1332
1333 stop_handler_known_methods(Config) ->
1334 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1335
1336 stop_handler_languages_provided(Config) ->
1337 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1338
1339 stop_handler_malformed_request(Config) ->
1340 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1341
1342 stop_handler_moved_permanently(Config) ->
1343 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1344
1345 stop_handler_moved_temporarily(Config) ->
1346 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1347
1348 stop_handler_multiple_choices(Config) ->
1349 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1350
1351 stop_handler_options(Config) ->
1352 do_no_body_stop_handler(Config, options, ?FUNCTION_NAME).
1353
1354 stop_handler_previously_existed(Config) ->
1355 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1356
1357 stop_handler_range_satisfiable(Config) ->
1358 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1359
1360 stop_handler_ranges_provided(Config) ->
1361 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1362
1363 stop_handler_rate_limited(Config) ->
1364 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1365
1366 stop_handler_resource_exists(Config) ->
1367 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1368
1369 stop_handler_service_available(Config) ->
1370 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1371
1372 stop_handler_uri_too_long(Config) ->
1373 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1374
1375 stop_handler_valid_content_headers(Config) ->
1376 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1377
1378 stop_handler_valid_entity_length(Config) ->
1379 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1380
1381 stop_handler_accept(Config) ->
1382 do_req_body_stop_handler(Config, post, ?FUNCTION_NAME).
1383
1384 stop_handler_provide(Config) ->
1385 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1386
1387 stop_handler_provide_range(Config) ->
1388 do_no_body_stop_handler(Config, get, ?FUNCTION_NAME).
1389
1390 do_no_body_stop_handler(Config, Method, StateName0) ->
1391 doc("Send a response manually and stop the REST handler."),
1392 ConnPid = gun_open(Config),
1393 "stop_handler_" ++ StateName = atom_to_list(StateName0),
1394 Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
1395 {<<"accept-encoding">>, <<"gzip">>},
1396 {<<"range">>, <<"bytes=0-">>}
1397 ]),
1398 {response, fin, 248, _} = gun:await(ConnPid, Ref),
1399 ok.
1400
1401 do_req_body_stop_handler(Config, Method, StateName0) ->
1402 doc("Send a response manually and stop the REST handler."),
1403 ConnPid = gun_open(Config),
1404 "stop_handler_" ++ StateName = atom_to_list(StateName0),
1405 Ref = gun:Method(ConnPid, "/stop_handler?" ++ StateName, [
1406 {<<"accept-encoding">>, <<"gzip">>},
1407 {<<"content-type">>, <<"text/plain">>}
1408 ], <<"Hocus PocuSwitch!">>),
1409 {response, fin, 248, _} = gun:await(ConnPid, Ref),
1410 ok.
1411
1412 switch_handler_allowed_methods(Config) ->
1413 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1414
1415 switch_handler_allow_missing_post(Config) ->
1416 do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
1417
1418 switch_handler_charsets_provided(Config) ->
1419 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1420
1421 switch_handler_content_types_accepted(Config) ->
1422 do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
1423
1424 switch_handler_content_types_provided(Config) ->
1425 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1426
1427 switch_handler_delete_completed(Config) ->
1428 do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
1429
1430 switch_handler_delete_resource(Config) ->
1431 do_no_body_switch_handler(Config, delete, ?FUNCTION_NAME).
1432
1433 switch_handler_forbidden(Config) ->
1434 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1435
1436 switch_handler_is_authorized(Config) ->
1437 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1438
1439 switch_handler_is_conflict(Config) ->
1440 do_req_body_switch_handler(Config, put, ?FUNCTION_NAME).
1441
1442 switch_handler_known_methods(Config) ->
1443 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1444
1445 switch_handler_languages_provided(Config) ->
1446 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1447
1448 switch_handler_malformed_request(Config) ->
1449 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1450
1451 switch_handler_moved_permanently(Config) ->
1452 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1453
1454 switch_handler_moved_temporarily(Config) ->
1455 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1456
1457 switch_handler_multiple_choices(Config) ->
1458 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1459
1460 switch_handler_options(Config) ->
1461 do_no_body_switch_handler(Config, options, ?FUNCTION_NAME).
1462
1463 switch_handler_previously_existed(Config) ->
1464 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1465
1466 switch_handler_range_satisfiable(Config) ->
1467 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1468
1469 switch_handler_ranges_provided(Config) ->
1470 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1471
1472 switch_handler_rate_limited(Config) ->
1473 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1474
1475 switch_handler_resource_exists(Config) ->
1476 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1477
1478 switch_handler_service_available(Config) ->
1479 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1480
1481 switch_handler_uri_too_long(Config) ->
1482 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1483
1484 switch_handler_valid_content_headers(Config) ->
1485 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1486
1487 switch_handler_valid_entity_length(Config) ->
1488 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1489
1490 switch_handler_accept(Config) ->
1491 do_req_body_switch_handler(Config, post, ?FUNCTION_NAME).
1492
1493 switch_handler_provide(Config) ->
1494 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1495
1496 switch_handler_provide_range(Config) ->
1497 do_no_body_switch_handler(Config, get, ?FUNCTION_NAME).
1498
1499 do_no_body_switch_handler(Config, Method, StateName0) ->
1500 doc("Switch REST to loop handler for streaming the response body, "
1501 "with and without options."),
1502 "switch_handler_" ++ StateName = atom_to_list(StateName0),
1503 do_no_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
1504 do_no_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
1505
1506 do_no_body_switch_handler1(Config, Method, Path) ->
1507 ConnPid = gun_open(Config),
1508 Ref = gun:Method(ConnPid, Path, [
1509 {<<"accept-encoding">>, <<"gzip">>},
1510 {<<"range">>, <<"bytes=0-">>}
1511 ]),
1512 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
1513 {ok, Body} = gun:await_body(ConnPid, Ref),
1514 <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
1515 ok.
1516
1517 do_req_body_switch_handler(Config, Method, StateName0) ->
1518 doc("Switch REST to loop handler for streaming the response body, "
1519 "with and without options."),
1520 "switch_handler_" ++ StateName = atom_to_list(StateName0),
1521 do_req_body_switch_handler1(Config, Method, "/switch_handler?" ++ StateName),
1522 do_req_body_switch_handler1(Config, Method, "/switch_handler_opts?" ++ StateName).
1523
1524 do_req_body_switch_handler1(Config, Method, Path) ->
1525 ConnPid = gun_open(Config),
1526 Ref = gun:Method(ConnPid, Path, [
1527 {<<"accept-encoding">>, <<"gzip">>},
1528 {<<"content-type">>, <<"text/plain">>}
1529 ], <<"Hocus PocuSwitch!">>),
1530 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
1531 {ok, Body} = gun:await_body(ConnPid, Ref),
1532 <<"Hello\nstreamed\nworld!\n">> = do_decode(Headers, Body),
1533 ok.