Debian Codegraph @

test/rfc7231_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(rfc7231_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 -import(cowboy_test, [gun_open/2]).
23 -import(cowboy_test, [raw_open/1]).
24 -import(cowboy_test, [raw_send/2]).
25 -import(cowboy_test, [raw_recv_head/1]).
26 -import(cowboy_test, [raw_recv/3]).
27
28 all() ->
29 cowboy_test:common_all().
30
31 groups() ->
32 cowboy_test:common_groups(ct_helper:all(?MODULE)).
33
34 init_per_group(Name, Config) ->
35 cowboy_test:init_common_groups(Name, Config, ?MODULE).
36
37 end_per_group(Name, _) ->
38 cowboy_test:stop_group(Name).
39
40 init_dispatch(_) ->
41 cowboy_router:compile([{"[...]", [
42 {"*", asterisk_h, []},
43 {"/", hello_h, []},
44 {"/echo/:key", echo_h, []},
45 {"/delay/echo/:key", echo_h, []},
46 {"/resp/:key[/:arg]", resp_h, []},
47 {"/ws", ws_init_h, #{}}
48 ]}]).
49
50 %% @todo The documentation should list what methods, headers and status codes
51 %% are handled automatically so users can know what befalls to them to implement.
52
53 %% Representations.
54
55 %% Cowboy has cowboy_compress_h that could be concerned with this.
56 %% However Cowboy will not attempt to compress if any content-coding
57 %% is already applied, regardless of what they are.
58 %
59 % If one or more encodings have been applied to a representation, the
60 % sender that applied the encodings MUST generate a Content-Encoding
61 % header field that lists the content codings in the order in which
62 % they were applied. Additional information about the encoding
63 % parameters can be provided by other header fields not defined by this
64 % specification. (RFC7231 3.1.2.2)
65
66 %% Methods.
67
68 method_get(Config) ->
69 doc("The GET method is accepted. (RFC7231 4.3.1)"),
70 ConnPid = gun_open(Config),
71 Ref = gun:get(ConnPid, "/", [
72 {<<"accept-encoding">>, <<"gzip">>}
73 ]),
74 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
75 {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
76 ok.
77
78 method_head(Config) ->
79 doc("The HEAD method is accepted. (RFC7231 4.3.2)"),
80 ConnPid = gun_open(Config),
81 Ref = gun:head(ConnPid, "/", [
82 {<<"accept-encoding">>, <<"gzip">>}
83 ]),
84 {response, fin, 200, _} = gun:await(ConnPid, Ref),
85 ok.
86
87 method_head_same_resp_headers_as_get(Config) ->
88 doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
89 ConnPid = gun_open(Config),
90 Ref1 = gun:get(ConnPid, "/", [
91 {<<"accept-encoding">>, <<"gzip">>}
92 ]),
93 {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
94 {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref1),
95 Ref2 = gun:head(ConnPid, "/", [
96 {<<"accept-encoding">>, <<"gzip">>}
97 ]),
98 {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
99 %% We remove the date header since the date might have changed between requests.
100 Headers = lists:keydelete(<<"date">>, 1, Headers1),
101 Headers = lists:keydelete(<<"date">>, 1, Headers2),
102 ok.
103
104 method_head_same_resp_headers_as_get_stream_reply(Config) ->
105 doc("Responses to HEAD should return the same headers as GET. (RFC7231 4.3.2)"),
106 ConnPid = gun_open(Config),
107 Ref1 = gun:get(ConnPid, "/resp/stream_reply2/200", [
108 {<<"accept-encoding">>, <<"gzip">>}
109 ]),
110 {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
111 {ok, _} = gun:await_body(ConnPid, Ref1),
112 Ref2 = gun:head(ConnPid, "/resp/stream_reply2/200", [
113 {<<"accept-encoding">>, <<"gzip">>}
114 ]),
115 {response, fin, 200, Headers2} = gun:await(ConnPid, Ref2),
116 %% We remove the date header since the date might have changed between requests.
117 Headers = lists:keydelete(<<"date">>, 1, Headers1),
118 Headers = lists:keydelete(<<"date">>, 1, Headers2),
119 ok.
120
121 method_post(Config) ->
122 doc("The POST method is accepted. (RFC7231 4.3.3)"),
123 ConnPid = gun_open(Config),
124 Ref = gun:post(ConnPid, "/echo/read_body", [
125 {<<"accept-encoding">>, <<"gzip">>},
126 {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
127 ], <<"hello=world">>),
128 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
129 {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
130 ok.
131
132 method_put(Config) ->
133 doc("The PUT method is accepted. (RFC7231 4.3.4)"),
134 ConnPid = gun_open(Config),
135 Ref = gun:put(ConnPid, "/echo/read_body", [
136 {<<"accept-encoding">>, <<"gzip">>},
137 {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
138 ], <<"hello=world">>),
139 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
140 {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
141 ok.
142
143 method_delete(Config) ->
144 doc("The DELETE method is accepted. (RFC7231 4.3.5)"),
145 ConnPid = gun_open(Config),
146 Ref = gun:delete(ConnPid, "/echo/method", [
147 {<<"accept-encoding">>, <<"gzip">>}
148 ]),
149 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
150 {ok, <<"DELETE">>} = gun:await_body(ConnPid, Ref),
151 ok.
152
153 %% @todo This test is currently broken because Gun does not
154 %% send a proper CONNECT request.
155 %method_connect(Config) ->
156 % doc("The CONNECT method is currently not implemented. (RFC7231 4.3.6)"),
157 % ConnPid = gun_open(Config),
158 % Ref = gun:request(ConnPid, <<"CONNECT">>, "localhost:8080", [
159 % {<<"accept-encoding">>, <<"gzip">>}
160 % ], <<>>),
161 % {response, fin, 501, _} = gun:await(ConnPid, Ref),
162 % ok.
163
164 % A client sending a CONNECT request MUST send the authority form of
165 % request-target (Section 5.3 of [RFC7230]); i.e., the request-target
166 % consists of only the host name and port number of the tunnel
167 % destination, separated by a colon.
168 %
169 % A server MUST NOT send any Transfer-Encoding or Content-Length header
170 % fields in a 2xx (Successful) response to CONNECT. A client MUST
171 % ignore any Content-Length or Transfer-Encoding header fields received
172 % in a successful response to CONNECT.
173 %
174 % A payload within a CONNECT request message has no defined semantics;
175 % sending a payload body on a CONNECT request might cause some existing
176 % implementations to reject the request.
177
178 method_options(Config) ->
179 doc("The OPTIONS method is accepted. (RFC7231 4.3.7)"),
180 ConnPid = gun_open(Config),
181 Ref = gun:options(ConnPid, "/echo/method", [
182 {<<"accept-encoding">>, <<"gzip">>}
183 ]),
184 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
185 {ok, <<"OPTIONS">>} = gun:await_body(ConnPid, Ref),
186 ok.
187
188 method_options_asterisk(Config) ->
189 doc("The OPTIONS method is accepted with an asterisk. (RFC7231 4.3.7)"),
190 ConnPid = gun_open(Config),
191 Ref = gun:options(ConnPid, "*", [
192 {<<"accept-encoding">>, <<"gzip">>},
193 {<<"x-echo">>, <<"method">>}
194 ]),
195 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
196 {ok, <<"OPTIONS">>} = gun:await_body(ConnPid, Ref),
197 ok.
198
199 method_options_content_length_0(Config) ->
200 doc("The OPTIONS method must set the content-length header "
201 "to 0 when no body is returned. (RFC7231 4.3.7)"),
202 ConnPid = gun_open(Config),
203 Ref = gun:options(ConnPid, "*", [
204 {<<"accept-encoding">>, <<"gzip">>}
205 ]),
206 {response, fin, 200, Headers} = gun:await(ConnPid, Ref),
207 {_, <<"0">>} = lists:keyfind(<<"content-length">>, 1, Headers),
208 ok.
209
210 method_trace(Config) ->
211 doc("The TRACE method is currently not implemented. (RFC7231 4.3.8)"),
212 ConnPid = gun_open(Config),
213 Ref = gun:request(ConnPid, <<"TRACE">>, "/", [
214 {<<"accept-encoding">>, <<"gzip">>}
215 ], <<>>),
216 {response, fin, 501, _} = gun:await(ConnPid, Ref),
217 ok.
218
219 %% Request headers.
220
221 %% @todo It could be useful to check that we can parse all request headers defined in this RFC.
222 %% @todo The same applies to any other RFC for which we have a test suite.
223
224 expect(Config) ->
225 doc("A server that receives a 100-continue expectation should honor it. (RFC7231 5.1.1)"),
226 ConnPid = gun_open(Config),
227 Ref = gun:post(ConnPid, "/echo/read_body", [
228 {<<"accept-encoding">>, <<"gzip">>},
229 {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
230 {<<"expect">>, <<"100-continue">>}
231 ]),
232 {inform, 100, _} = gun:await(ConnPid, Ref),
233 gun:close(ConnPid).
234
235 http10_expect(Config) ->
236 case config(protocol, Config) of
237 http ->
238 do_http10_expect(Config);
239 http2 ->
240 expect(Config);
241 http3 ->
242 expect(Config)
243 end.
244
245 do_http10_expect(Config) ->
246 doc("A server that receives a 100-continue expectation "
247 "in an HTTP/1.0 request must ignore it. (RFC7231 5.1.1)"),
248 Body = <<"hello=world">>,
249 ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
250 Ref = gun:post(ConnPid, "/echo/read_body", [
251 {<<"accept-encoding">>, <<"gzip">>},
252 {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
253 {<<"content-length">>, integer_to_binary(byte_size(Body))},
254 {<<"expect">>, <<"100-continue">>}
255 ]),
256 timer:sleep(500),
257 ok = gun:data(ConnPid, Ref, fin, Body),
258 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
259 {ok, Body} = gun:await_body(ConnPid, Ref),
260 ok.
261
262 %% Cowboy ignores the expect header when the value is not 100-continue.
263 %
264 % A server that receives an Expect field-value other than 100-continue
265 % MAY respond with a 417 (Expectation Failed) status code to indicate
266 % that the unexpected expectation cannot be met.
267
268 expect_receive_body_omit_100_continue(Config) ->
269 doc("A server may omit sending a 100 Continue response if it has "
270 "already started receiving the request body. (RFC7231 5.1.1)"),
271 ConnPid = gun_open(Config),
272 Ref = gun:post(ConnPid, "/delay/echo/read_body", [
273 {<<"accept-encoding">>, <<"gzip">>},
274 {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
275 {<<"expect">>, <<"100-continue">>}
276 ], <<"hello=world">>),
277 %% We receive the response directly without a 100 Continue.
278 {response, nofin, 200, _} = gun:await(ConnPid, Ref),
279 {ok, <<"hello=world">>} = gun:await_body(ConnPid, Ref),
280 ok.
281
282 expect_discard_body_skip(Config) ->
283 doc("A server that responds with a final status code before reading "
284 "the entire message body should keep the connection open and skip "
285 "the body when appropriate. (RFC7231 5.1.1)"),
286 ConnPid = gun_open(Config),
287 Ref1 = gun:post(ConnPid, "/echo/method", [
288 {<<"accept-encoding">>, <<"gzip">>},
289 {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
290 {<<"expect">>, <<"100-continue">>}
291 ], <<"hello=world">>),
292 {response, nofin, 200, _} = gun:await(ConnPid, Ref1),
293 {ok, <<"POST">>} = gun:await_body(ConnPid, Ref1),
294 Ref2 = gun:get(ConnPid, "/echo/method", [
295 {<<"accept-encoding">>, <<"gzip">>},
296 {<<"content-type">>, <<"application/x-www-form-urlencoded">>}
297 ]),
298 {response, nofin, 200, _} = gun:await(ConnPid, Ref2),
299 {ok, <<"GET">>} = gun:await_body(ConnPid, Ref2),
300 ok.
301
302 expect_discard_body_close(Config) ->
303 case config(protocol, Config) of
304 http ->
305 do_expect_discard_body_close(Config);
306 http2 ->
307 doc("There's no reason to close the connection when using HTTP/2, "
308 "even if a stream body is too large. We just cancel the stream.");
309 http3 ->
310 doc("There's no reason to close the connection when using HTTP/3, "
311 "even if a stream body is too large. We just cancel the stream.")
312 end.
313
314 do_expect_discard_body_close(Config) ->
315 doc("A server that responds with a final status code before reading "
316 "the entire message body may close the connection to avoid "
317 "reading a potentially large request body. (RFC7231 5.1.1, RFC7230 6.6)"),
318 ConnPid = gun_open(Config),
319 Ref1 = gun:post(ConnPid, "/echo/method", [
320 {<<"accept-encoding">>, <<"gzip">>},
321 {<<"content-length">>, <<"10000000">>},
322 {<<"content-type">>, <<"application/x-www-form-urlencoded">>},
323 {<<"expect">>, <<"100-continue">>}
324 ]),
325 {response, nofin, 200, _Headers} = gun:await(ConnPid, Ref1),
326 %% Ideally we would send a connection: close. Cowboy however
327 %% cannot know the intent of the application until after we
328 %% sent the response.
329 % {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
330 {ok, <<"POST">>} = gun:await_body(ConnPid, Ref1),
331 %% The connection is gone.
332 receive
333 {gun_down, ConnPid, _, closed, _} ->
334 ok
335 after 1000 ->
336 error(timeout)
337 end.
338
339 no_accept_encoding(Config) ->
340 doc("While a request with no accept-encoding header implies the "
341 "user agent has no preferences and any would be acceptable, "
342 "Cowboy will not serve content-codings by defaults to ensure "
343 "the content can safely be read. (RFC7231 5.3.4)"),
344 ConnPid = gun_open(Config),
345 Ref = gun:get(ConnPid, "/resp/stream_reply2/200"),
346 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
347 false = lists:keyfind(<<"content-encoding">>, 1, Headers),
348 ok.
349
350 %% Cowboy currently ignores any information about the identity content-coding
351 %% and instead considers it always acceptable.
352 %
353 % 2. If the representation has no content-coding, then it is
354 % acceptable by default unless specifically excluded by the
355 % Accept-Encoding field stating either "identity;q=0" or "*;q=0"
356 % without a more specific entry for "identity".
357
358 accept_encoding_gzip(Config) ->
359 doc("No qvalue means the content-coding is acceptable. (RFC7231 5.3.4)"),
360 ConnPid = gun_open(Config),
361 Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
362 {<<"accept-encoding">>, <<"gzip">>}
363 ]),
364 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
365 _ = case config(flavor, Config) of
366 compress ->
367 {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
368 _ ->
369 false = lists:keyfind(<<"content-encoding">>, 1, Headers)
370 end,
371 ok.
372
373 accept_encoding_gzip_1(Config) ->
374 doc("A qvalue different than 0 means the content-coding is acceptable. (RFC7231 5.3.4)"),
375 ConnPid = gun_open(Config),
376 Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
377 {<<"accept-encoding">>, <<"gzip;q=1.0">>}
378 ]),
379 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
380 _ = case config(flavor, Config) of
381 compress ->
382 {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
383 _ ->
384 false = lists:keyfind(<<"content-encoding">>, 1, Headers)
385 end,
386 ok.
387
388 accept_encoding_gzip_0(Config) ->
389 doc("A qvalue of 0 means the content-coding is not acceptable. (RFC7231 5.3.1, RFC7231 5.3.4)"),
390 ConnPid = gun_open(Config),
391 Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
392 {<<"accept-encoding">>, <<"gzip;q=0">>}
393 ]),
394 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
395 false = lists:keyfind(<<"content-encoding">>, 1, Headers),
396 ok.
397
398 %% Cowboy currently only supports gzip automatically via cowboy_compress_h.
399 %
400 % 4. If multiple content-codings are acceptable, then the acceptable
401 % content-coding with the highest non-zero qvalue is preferred.
402
403 accept_encoding_empty(Config) ->
404 doc("An empty content-coding means that the user agent does not "
405 "want any content-coding applied to the response. (RFC7231 5.3.4)"),
406 ConnPid = gun_open(Config),
407 Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
408 {<<"accept-encoding">>, <<>>}
409 ]),
410 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
411 false = lists:keyfind(<<"content-encoding">>, 1, Headers),
412 ok.
413
414 accept_encoding_unknown(Config) ->
415 doc("An accept-encoding header only containing unknown content-codings "
416 "should result in no content-coding being applied. (RFC7231 5.3.4)"),
417 ConnPid = gun_open(Config),
418 Ref = gun:get(ConnPid, "/resp/stream_reply2/200", [
419 {<<"accept-encoding">>, <<"deflate">>}
420 ]),
421 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
422 false = lists:keyfind(<<"content-encoding">>, 1, Headers),
423 ok.
424
425 %% Status codes.
426
427 http10_status_code_100(Config) ->
428 case config(protocol, Config) of
429 http ->
430 doc("The 100 Continue status code must not "
431 "be sent to HTTP/1.0 endpoints. (RFC7231 6.2)"),
432 do_unsupported_status_code_1xx(100, Config);
433 http2 ->
434 status_code_100(Config);
435 http3 ->
436 status_code_100(Config)
437 end.
438
439 http10_status_code_101(Config) ->
440 case config(protocol, Config) of
441 http ->
442 doc("The 101 Switching Protocols status code must not "
443 "be sent to HTTP/1.0 endpoints. (RFC7231 6.2)"),
444 do_unsupported_status_code_1xx(101, Config);
445 http2 ->
446 status_code_101(Config);
447 http3 ->
448 %% While 101 is not supported by HTTP/3, there is no
449 %% wording in RFC9114 that forbids sending it.
450 status_code_101(Config)
451 end.
452
453 do_unsupported_status_code_1xx(StatusCode, Config) ->
454 ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}),
455 Ref = gun:get(ConnPid, "/resp/inform2/" ++ integer_to_list(StatusCode), [
456 {<<"accept-encoding">>, <<"gzip">>}
457 ]),
458 {response, _, 200, _} = gun:await(ConnPid, Ref),
459 ok.
460
461 status_code_100(Config) ->
462 doc("The 100 Continue status code can be sent. (RFC7231 6.2.1)"),
463 ConnPid = gun_open(Config),
464 Ref = gun:get(ConnPid, "/resp/inform2/100", [
465 {<<"accept-encoding">>, <<"gzip">>}
466 ]),
467 {inform, 100, []} = gun:await(ConnPid, Ref),
468 ok.
469
470 status_code_101(Config) ->
471 doc("The 101 Switching Protocols status code can be sent. (RFC7231 6.2.2)"),
472 ConnPid = gun_open(Config),
473 Ref = gun:get(ConnPid, "/resp/inform2/101", [
474 {<<"accept-encoding">>, <<"gzip">>}
475 ]),
476 case gun:await(ConnPid, Ref) of
477 %% Gun HTTP/1 rejects unexpected 101 with a protocol_error connection error.
478 {error, {connection_error,
479 {connection_error, protocol_error, _}}} -> ok;
480 {inform, 101, []} -> ok
481 end.
482
483 status_code_200(Config) ->
484 doc("The 200 OK status code can be sent. (RFC7231 6.3.1)"),
485 ConnPid = gun_open(Config),
486 Ref = gun:get(ConnPid, "/resp/reply2/200", [
487 {<<"accept-encoding">>, <<"gzip">>}
488 ]),
489 {response, _, 200, _} = gun:await(ConnPid, Ref),
490 ok.
491
492 status_code_201(Config) ->
493 doc("The 201 Created status code can be sent. (RFC7231 6.3.2)"),
494 ConnPid = gun_open(Config),
495 Ref = gun:get(ConnPid, "/resp/reply2/201", [
496 {<<"accept-encoding">>, <<"gzip">>}
497 ]),
498 {response, _, 201, _} = gun:await(ConnPid, Ref),
499 ok.
500
501 status_code_202(Config) ->
502 doc("The 202 Accepted status code can be sent. (RFC7231 6.3.3)"),
503 ConnPid = gun_open(Config),
504 Ref = gun:get(ConnPid, "/resp/reply2/202", [
505 {<<"accept-encoding">>, <<"gzip">>}
506 ]),
507 {response, _, 202, _} = gun:await(ConnPid, Ref),
508 ok.
509
510 status_code_203(Config) ->
511 doc("The 203 Non-Authoritative Information status code can be sent. (RFC7231 6.3.4)"),
512 ConnPid = gun_open(Config),
513 Ref = gun:get(ConnPid, "/resp/reply2/203", [
514 {<<"accept-encoding">>, <<"gzip">>}
515 ]),
516 {response, _, 203, _} = gun:await(ConnPid, Ref),
517 ok.
518
519 status_code_204(Config) ->
520 doc("The 204 No Content status code can be sent. (RFC7231 6.3.5)"),
521 ConnPid = gun_open(Config),
522 Ref = gun:get(ConnPid, "/resp/reply2/204", [
523 {<<"accept-encoding">>, <<"gzip">>}
524 ]),
525 {response, _, 204, _} = gun:await(ConnPid, Ref),
526 ok.
527
528 status_code_205(Config) ->
529 doc("The 205 Reset Content status code can be sent. (RFC7231 6.3.6)"),
530 ConnPid = gun_open(Config),
531 Ref = gun:get(ConnPid, "/resp/reply2/205", [
532 {<<"accept-encoding">>, <<"gzip">>}
533 ]),
534 {response, _, 205, _} = gun:await(ConnPid, Ref),
535 ok.
536
537 status_code_300(Config) ->
538 doc("The 300 Multiple Choices status code can be sent. (RFC7231 6.4.1)"),
539 ConnPid = gun_open(Config),
540 Ref = gun:get(ConnPid, "/resp/reply2/300", [
541 {<<"accept-encoding">>, <<"gzip">>}
542 ]),
543 {response, _, 300, _} = gun:await(ConnPid, Ref),
544 ok.
545
546 status_code_301(Config) ->
547 doc("The 301 Moved Permanently status code can be sent. (RFC7231 6.4.2)"),
548 ConnPid = gun_open(Config),
549 Ref = gun:get(ConnPid, "/resp/reply2/301", [
550 {<<"accept-encoding">>, <<"gzip">>}
551 ]),
552 {response, _, 301, _} = gun:await(ConnPid, Ref),
553 ok.
554
555 status_code_302(Config) ->
556 doc("The 302 Found status code can be sent. (RFC7231 6.4.3)"),
557 ConnPid = gun_open(Config),
558 Ref = gun:get(ConnPid, "/resp/reply2/302", [
559 {<<"accept-encoding">>, <<"gzip">>}
560 ]),
561 {response, _, 302, _} = gun:await(ConnPid, Ref),
562 ok.
563
564 status_code_303(Config) ->
565 doc("The 303 See Other status code can be sent. (RFC7231 6.4.4)"),
566 ConnPid = gun_open(Config),
567 Ref = gun:get(ConnPid, "/resp/reply2/303", [
568 {<<"accept-encoding">>, <<"gzip">>}
569 ]),
570 {response, _, 303, _} = gun:await(ConnPid, Ref),
571 ok.
572
573 status_code_305(Config) ->
574 doc("The 305 Use Proxy status code can be sent. (RFC7231 6.4.5)"),
575 ConnPid = gun_open(Config),
576 Ref = gun:get(ConnPid, "/resp/reply2/305", [
577 {<<"accept-encoding">>, <<"gzip">>}
578 ]),
579 {response, _, 305, _} = gun:await(ConnPid, Ref),
580 ok.
581
582 %% The status code 306 is no longer used. (RFC7231 6.4.6)
583
584 status_code_307(Config) ->
585 doc("The 307 Temporary Redirect status code can be sent. (RFC7231 6.4.7)"),
586 ConnPid = gun_open(Config),
587 Ref = gun:get(ConnPid, "/resp/reply2/307", [
588 {<<"accept-encoding">>, <<"gzip">>}
589 ]),
590 {response, _, 307, _} = gun:await(ConnPid, Ref),
591 ok.
592
593 status_code_400(Config) ->
594 doc("The 400 Bad Request status code can be sent. (RFC7231 6.5.1)"),
595 ConnPid = gun_open(Config),
596 Ref = gun:get(ConnPid, "/resp/reply2/400", [
597 {<<"accept-encoding">>, <<"gzip">>}
598 ]),
599 {response, _, 400, _} = gun:await(ConnPid, Ref),
600 ok.
601
602 status_code_402(Config) ->
603 doc("The 402 Payment Required status code can be sent. (RFC7231 6.5.2)"),
604 ConnPid = gun_open(Config),
605 Ref = gun:get(ConnPid, "/resp/reply2/402", [
606 {<<"accept-encoding">>, <<"gzip">>}
607 ]),
608 {response, _, 402, _} = gun:await(ConnPid, Ref),
609 ok.
610
611 status_code_403(Config) ->
612 doc("The 403 Forbidden status code can be sent. (RFC7231 6.5.3)"),
613 ConnPid = gun_open(Config),
614 Ref = gun:get(ConnPid, "/resp/reply2/403", [
615 {<<"accept-encoding">>, <<"gzip">>}
616 ]),
617 {response, _, 403, _} = gun:await(ConnPid, Ref),
618 ok.
619
620 status_code_404(Config) ->
621 doc("The 404 Not Found status code can be sent. (RFC7231 6.5.4)"),
622 ConnPid = gun_open(Config),
623 Ref = gun:get(ConnPid, "/resp/reply2/404", [
624 {<<"accept-encoding">>, <<"gzip">>}
625 ]),
626 {response, _, 404, _} = gun:await(ConnPid, Ref),
627 ok.
628
629 status_code_404_not_found(Config) ->
630 doc("The 404 Not Found status code is sent when the target "
631 "resource does not exist. (RFC7231 6.5.4)"),
632 ConnPid = gun_open(Config),
633 Ref = gun:get(ConnPid, "/not/found", [
634 {<<"accept-encoding">>, <<"gzip">>}
635 ]),
636 {response, _, 404, _} = gun:await(ConnPid, Ref),
637 ok.
638
639 status_code_405(Config) ->
640 doc("The 405 Method Not Allowed status code can be sent. (RFC7231 6.5.5)"),
641 ConnPid = gun_open(Config),
642 Ref = gun:get(ConnPid, "/resp/reply2/405", [
643 {<<"accept-encoding">>, <<"gzip">>}
644 ]),
645 {response, _, 405, _} = gun:await(ConnPid, Ref),
646 ok.
647
648 status_code_406(Config) ->
649 doc("The 406 Not Acceptable status code can be sent. (RFC7231 6.5.6)"),
650 ConnPid = gun_open(Config),
651 Ref = gun:get(ConnPid, "/resp/reply2/406", [
652 {<<"accept-encoding">>, <<"gzip">>}
653 ]),
654 {response, _, 406, _} = gun:await(ConnPid, Ref),
655 ok.
656
657 status_code_408(Config) ->
658 doc("The 408 Request Timeout status code can be sent. (RFC7231 6.5.7)"),
659 ConnPid = gun_open(Config),
660 Ref = gun:get(ConnPid, "/resp/reply2/408", [
661 {<<"accept-encoding">>, <<"gzip">>}
662 ]),
663 {response, _, 408, _} = gun:await(ConnPid, Ref),
664 ok.
665
666 status_code_408_connection_close(Config) ->
667 case config(protocol, Config) of
668 http ->
669 do_http11_status_code_408_connection_close(Config);
670 http2 ->
671 doc("HTTP/2 connections are not closed on 408 responses.");
672 http3 ->
673 doc("HTTP/3 connections are not closed on 408 responses.")
674 end.
675
676 do_http11_status_code_408_connection_close(Config) ->
677 doc("A 408 response should result in a connection close "
678 "for HTTP/1.1 connections. (RFC7231 6.5.7)"),
679 Client = raw_open(Config),
680 ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
681 {_, 408, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
682 {Headers, <<>>} = cow_http:parse_headers(Rest),
683 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
684 {error, closed} = raw_recv(Client, 0, 1000),
685 ok.
686
687 status_code_409(Config) ->
688 doc("The 409 Conflict status code can be sent. (RFC7231 6.5.8)"),
689 ConnPid = gun_open(Config),
690 Ref = gun:get(ConnPid, "/resp/reply2/409", [
691 {<<"accept-encoding">>, <<"gzip">>}
692 ]),
693 {response, _, 409, _} = gun:await(ConnPid, Ref),
694 ok.
695
696 status_code_410(Config) ->
697 doc("The 410 Gone status code can be sent. (RFC7231 6.5.9)"),
698 ConnPid = gun_open(Config),
699 Ref = gun:get(ConnPid, "/resp/reply2/410", [
700 {<<"accept-encoding">>, <<"gzip">>}
701 ]),
702 {response, _, 410, _} = gun:await(ConnPid, Ref),
703 ok.
704
705 status_code_411(Config) ->
706 doc("The 411 Length Required status code can be sent. (RFC7231 6.5.10)"),
707 ConnPid = gun_open(Config),
708 Ref = gun:get(ConnPid, "/resp/reply2/411", [
709 {<<"accept-encoding">>, <<"gzip">>}
710 ]),
711 {response, _, 411, _} = gun:await(ConnPid, Ref),
712 ok.
713
714 status_code_413(Config) ->
715 doc("The 413 Payload Too Large status code can be sent. (RFC7231 6.5.11)"),
716 ConnPid = gun_open(Config),
717 Ref = gun:get(ConnPid, "/resp/reply2/413", [
718 {<<"accept-encoding">>, <<"gzip">>}
719 ]),
720 {response, _, 413, _} = gun:await(ConnPid, Ref),
721 ok.
722
723 status_code_414(Config) ->
724 doc("The 414 URI Too Long status code can be sent. (RFC7231 6.5.12)"),
725 ConnPid = gun_open(Config),
726 Ref = gun:get(ConnPid, "/resp/reply2/414", [
727 {<<"accept-encoding">>, <<"gzip">>}
728 ]),
729 {response, _, 414, _} = gun:await(ConnPid, Ref),
730 ok.
731
732 status_code_415(Config) ->
733 doc("The 415 Unsupported Media Type status code can be sent. (RFC7231 6.5.13)"),
734 ConnPid = gun_open(Config),
735 Ref = gun:get(ConnPid, "/resp/reply2/415", [
736 {<<"accept-encoding">>, <<"gzip">>}
737 ]),
738 {response, _, 415, _} = gun:await(ConnPid, Ref),
739 ok.
740
741 status_code_417(Config) ->
742 doc("The 417 Expectation Failed status code can be sent. (RFC7231 6.5.14)"),
743 ConnPid = gun_open(Config),
744 Ref = gun:get(ConnPid, "/resp/reply2/417", [
745 {<<"accept-encoding">>, <<"gzip">>}
746 ]),
747 {response, _, 417, _} = gun:await(ConnPid, Ref),
748 ok.
749
750 status_code_426(Config) ->
751 doc("The 426 Upgrade Required status code can be sent. (RFC7231 6.5.15)"),
752 ConnPid = gun_open(Config),
753 Ref = gun:get(ConnPid, "/resp/reply2/426", [
754 {<<"accept-encoding">>, <<"gzip">>}
755 ]),
756 {response, _, 426, _} = gun:await(ConnPid, Ref),
757 ok.
758
759 status_code_426_upgrade_header(Config) ->
760 case config(protocol, Config) of
761 http ->
762 do_status_code_426_upgrade_header(Config);
763 http2 ->
764 doc("HTTP/2 does not support the HTTP/1.1 Upgrade mechanism.");
765 http3 ->
766 doc("HTTP/3 does not support the HTTP/1.1 Upgrade mechanism.")
767 end.
768
769 do_status_code_426_upgrade_header(Config) ->
770 doc("A 426 response must include a upgrade header. (RFC7231 6.5.15)"),
771 ConnPid = gun_open(Config),
772 Ref = gun:get(ConnPid, "/ws?ok", [
773 {<<"accept-encoding">>, <<"gzip">>}
774 ]),
775 {response, _, 426, Headers} = gun:await(ConnPid, Ref),
776 {_, <<"upgrade">>} = lists:keyfind(<<"connection">>, 1, Headers),
777 {_, <<"websocket">>} = lists:keyfind(<<"upgrade">>, 1, Headers),
778 ok.
779
780 status_code_500(Config) ->
781 doc("The 500 Internal Server Error status code can be sent. (RFC7231 6.6.1)"),
782 ConnPid = gun_open(Config),
783 Ref = gun:get(ConnPid, "/resp/reply2/500", [
784 {<<"accept-encoding">>, <<"gzip">>}
785 ]),
786 {response, _, 500, _} = gun:await(ConnPid, Ref),
787 ok.
788
789 status_code_501(Config) ->
790 doc("The 501 Not Implemented status code can be sent. (RFC7231 6.6.2)"),
791 ConnPid = gun_open(Config),
792 Ref = gun:get(ConnPid, "/resp/reply2/501", [
793 {<<"accept-encoding">>, <<"gzip">>}
794 ]),
795 {response, _, 501, _} = gun:await(ConnPid, Ref),
796 ok.
797
798 status_code_502(Config) ->
799 doc("The 502 Bad Gateway status code can be sent. (RFC7231 6.6.3)"),
800 ConnPid = gun_open(Config),
801 Ref = gun:get(ConnPid, "/resp/reply2/502", [
802 {<<"accept-encoding">>, <<"gzip">>}
803 ]),
804 {response, _, 502, _} = gun:await(ConnPid, Ref),
805 ok.
806
807 status_code_503(Config) ->
808 doc("The 503 Service Unavailable status code can be sent. (RFC7231 6.6.4)"),
809 ConnPid = gun_open(Config),
810 Ref = gun:get(ConnPid, "/resp/reply2/503", [
811 {<<"accept-encoding">>, <<"gzip">>}
812 ]),
813 {response, _, 503, _} = gun:await(ConnPid, Ref),
814 ok.
815
816 status_code_504(Config) ->
817 doc("The 504 Gateway Timeout status code can be sent. (RFC7231 6.6.5)"),
818 ConnPid = gun_open(Config),
819 Ref = gun:get(ConnPid, "/resp/reply2/504", [
820 {<<"accept-encoding">>, <<"gzip">>}
821 ]),
822 {response, _, 504, _} = gun:await(ConnPid, Ref),
823 ok.
824
825 status_code_505(Config) ->
826 doc("The 505 HTTP Version Not Supported status code can be sent. (RFC7231 6.6.6)"),
827 ConnPid = gun_open(Config),
828 Ref = gun:get(ConnPid, "/resp/reply2/505", [
829 {<<"accept-encoding">>, <<"gzip">>}
830 ]),
831 {response, _, 505, _} = gun:await(ConnPid, Ref),
832 ok.
833
834 %% The 505 response code is supposed to be about the major HTTP version.
835 %% Cowboy instead rejects any version that isn't HTTP/1.0 or HTTP/1.1
836 %% when expecting an h1 request. While this is not correct in theory
837 %% it works in practice because there are no other minor versions.
838 %%
839 %% Cowboy does not do version checking for HTTP/2 since the protocol
840 %% does not include a version number in the messages.
841
842 %% Response headers.
843
844 %% @todo No such header in this suite, but some in other suites (if-(un)modified-since).
845 % A recipient that parses a timestamp value in an HTTP header field
846 % MUST accept all three HTTP-date formats. (RFC7231 7.1.1.1)
847
848 date_imf_fixdate(Config) ->
849 doc("The date header uses the IMF-fixdate format. (RFC7231 7.1.1.1, RFC7231 7.1.1.2)"),
850 ConnPid = gun_open(Config),
851 Ref = gun:get(ConnPid, "/", [
852 {<<"accept-encoding">>, <<"gzip">>}
853 ]),
854 {response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
855 {_, <<_,_,_,", ",_,_," ",_,_,_," ",_,_,_,_," ",_,_,":",_,_,":",_,_," GMT">>}
856 = lists:keyfind(<<"date">>, 1, Headers),
857 ok.
858
859 %% @todo Applies to both date and other headers (if-(un)modified-since).
860 % HTTP-date is case sensitive. A sender MUST NOT generate additional
861 % whitespace in an HTTP-date beyond that specifically included as SP in
862 % the grammar. The semantics of day-name, day, month, year, and
863 % time-of-day are the same as those defined for the Internet Message
864 % Format constructs with the corresponding name ([RFC5322], Section
865 % 3.3). (RFC7231 7.1.1.1)
866
867 %% @todo No such header in this suite, but some in other suites (if-(un)modified-since).
868 % Recipients of a timestamp value in rfc850-date format, which uses a
869 % two-digit year, MUST interpret a timestamp that appears to be more
870 % than 50 years in the future as representing the most recent year in
871 % the past that had the same last two digits. (RFC7231 7.1.1.1)
872
873 %% @todo Add an option to disable sending the date header.
874 % An origin server MUST NOT send a Date header field if it does not
875 % have a clock capable of providing a reasonable approximation of the
876 % current instance in Coordinated Universal Time. (RFC7231 7.1.1.2)
877
878 no_date_1xx(Config) ->
879 doc("The date header is optional for 1xx responses. "
880 "Cowboy does not send it with those responses. (RFC7231 7.1.1.2)"),
881 ConnPid = gun_open(Config),
882 Ref = gun:get(ConnPid, "/resp/inform2/100", [
883 {<<"accept-encoding">>, <<"gzip">>}
884 ]),
885 {inform, 100, Headers} = gun:await(ConnPid, Ref),
886 false = lists:keyfind(<<"date">>, 1, Headers),
887 ok.
888
889 date_2xx(Config) ->
890 doc("A date header must be sent for 2xx status codes. (RFC7231 7.1.1.2)"),
891 ConnPid = gun_open(Config),
892 Ref = gun:get(ConnPid, "/resp/reply2/200", [
893 {<<"accept-encoding">>, <<"gzip">>}
894 ]),
895 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
896 {_, _} = lists:keyfind(<<"date">>, 1, Headers),
897 ok.
898
899 date_3xx(Config) ->
900 doc("A date header must be sent for 3xx status codes. (RFC7231 7.1.1.2)"),
901 ConnPid = gun_open(Config),
902 Ref = gun:get(ConnPid, "/resp/reply2/300", [
903 {<<"accept-encoding">>, <<"gzip">>}
904 ]),
905 {response, _, 300, Headers} = gun:await(ConnPid, Ref),
906 {_, _} = lists:keyfind(<<"date">>, 1, Headers),
907 ok.
908
909 date_4xx(Config) ->
910 doc("A date header must be sent for 4xx status codes. (RFC7231 7.1.1.2)"),
911 ConnPid = gun_open(Config),
912 Ref = gun:get(ConnPid, "/resp/reply2/400", [
913 {<<"accept-encoding">>, <<"gzip">>}
914 ]),
915 {response, _, 400, Headers} = gun:await(ConnPid, Ref),
916 {_, _} = lists:keyfind(<<"date">>, 1, Headers),
917 ok.
918
919 date_5xx(Config) ->
920 doc("The date header is optional for 5xx status codes. "
921 "Cowboy however does send it with those responses. (RFC7231 7.1.1.2)"),
922 ConnPid = gun_open(Config),
923 Ref = gun:get(ConnPid, "/resp/reply2/500", [
924 {<<"accept-encoding">>, <<"gzip">>}
925 ]),
926 {response, _, 500, Headers} = gun:await(ConnPid, Ref),
927 {_, _} = lists:keyfind(<<"date">>, 1, Headers),
928 ok.
929
930 server_header(Config) ->
931 doc("An origin server may generate a server header field. "
932 "Cowboy generates a small one by default. (RFC7231 7.4.2)"),
933 ConnPid = gun_open(Config),
934 Ref = gun:get(ConnPid, "/"),
935 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
936 {_, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
937 ok.
938
939 server_header_override(Config) ->
940 doc("An origin server may generate a server header field. "
941 "Cowboy allows the user to override the default. (RFC7231 7.4.2)"),
942 ConnPid = gun_open(Config),
943 Ref = gun:get(ConnPid, "/resp/set_resp_header_server"),
944 {response, _, 200, Headers} = gun:await(ConnPid, Ref),
945 {_, <<"nginx">>} = lists:keyfind(<<"server">>, 1, Headers),
946 ok.
947
948 %% @todo It's worth revisiting this RFC in the context of cowboy_rest
949 %% to ensure the state machine is doing what's expected by the RFC.