0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Move many old HTTP test cases to the rest_handler test suite

A bug was fixed in cowboy_rest where when content_types_provided
returned a media type with a wildcard as first in the list, and
a request comes in without an accept header, then the media_type
value in the Req object would contain '*' instead of [] for the
parameters.
This commit is contained in:
Loïc Hoguin 2018-11-21 17:39:12 +01:00
parent 800a489009
commit 037b286aa8
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
8 changed files with 298 additions and 146 deletions

View file

@ -0,0 +1,15 @@
-module(accept_callback_missing_h).
-export([init/2]).
-export([allowed_methods/2]).
-export([content_types_accepted/2]).
init(Req, State) ->
{cowboy_rest, Req, State}.
allowed_methods(Req, State) ->
{[<<"PUT">>], Req, State}.
content_types_accepted(Req, State) ->
ct_helper_error_h:ignore(cowboy_rest, process_content_type, 3),
{[{<<"text/plain">>, accept}], Req, State}.

View file

@ -7,6 +7,7 @@
-export([allowed_methods/2]).
-export([content_types_accepted/2]).
-export([put_multipart_mixed/2]).
-export([put_text_plain/2]).
init(Req, Opts) ->
{cowboy_rest, Req, Opts}.
@ -17,7 +18,13 @@ allowed_methods(Req, State) ->
content_types_accepted(Req=#{qs := <<"multipart">>}, State) ->
{[
{{<<"multipart">>, <<"mixed">>, [{<<"v">>, <<"1">>}]}, put_multipart_mixed}
], Req, State}.
], Req, State};
content_types_accepted(Req=#{qs := <<"wildcard-param">>}, State) ->
{[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}.
put_multipart_mixed(Req, State) ->
{true, Req, State}.
put_text_plain(Req0, State) ->
{ok, _, Req} = cowboy_req:read_body(Req0),
{true, Req, State}.

View file

@ -0,0 +1,24 @@
%% This module has different content_types_provided values
%% and/or sends a different response body depending on the
%% query string.
-module(content_types_provided_h).
-export([init/2]).
-export([content_types_provided/2]).
-export([get_text_plain/2]).
init(Req, Opts) ->
{cowboy_rest, Req, Opts}.
content_types_provided(Req=#{qs := <<"wildcard-param">>}, State) ->
{[{{<<"text">>, <<"plain">>, '*'}, get_text_plain}], Req, State}.
get_text_plain(Req=#{qs := <<"wildcard-param">>}, State) ->
{_, _, Param} = maps:get(media_type, Req),
Body = if
Param =:= [] -> <<"[]">>;
Param =/= [] ->
iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param])
end,
{Body, Req, State}.

View file

@ -0,0 +1,28 @@
%% This module sends a different expires value
%% depending on the query string.
-module(expires_h).
-export([init/2]).
-export([content_types_provided/2]).
-export([get_text_plain/2]).
-export([expires/2]).
init(Req, Opts) ->
{cowboy_rest, Req, Opts}.
content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
get_text_plain(Req, State) ->
{<<"This is REST!">>, Req, State}.
expires(Req=#{qs := <<"tuple">>}, State) ->
{{{2012, 9, 21}, {22, 36, 14}}, Req, State};
expires(Req=#{qs := <<"binary">>}, State) ->
{<<"0">>, Req, State};
expires(Req=#{qs := <<"undefined">>}, State) ->
{undefined, Req, State};
%% Simulate the callback being missing in other cases.
expires(#{qs := <<"missing">>}, _) ->
no_call.

View file

@ -0,0 +1,24 @@
%% This module sends a different last-modified value
%% depending on the query string.
-module(last_modified_h).
-export([init/2]).
-export([content_types_provided/2]).
-export([get_text_plain/2]).
-export([last_modified/2]).
init(Req, Opts) ->
{cowboy_rest, Req, Opts}.
content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
get_text_plain(Req, State) ->
{<<"This is REST!">>, Req, State}.
last_modified(Req=#{qs := <<"tuple">>}, State) ->
{{{2012, 9, 21}, {22, 36, 14}}, Req, State};
%% Simulate the callback being missing in other cases.
last_modified(#{qs := <<"missing">>}, _) ->
no_call.