mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
AcceptCallback may now return created/see_other tuples for POST
They replace and deprecate the {true,URI} return value.
This commit is contained in:
parent
63a6b86fba
commit
8795233c57
4 changed files with 73 additions and 4 deletions
28
test/handlers/create_resource_h.erl
Normal file
28
test/handlers/create_resource_h.erl
Normal file
|
@ -0,0 +1,28 @@
|
|||
-module(create_resource_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([allowed_methods/2]).
|
||||
-export([resource_exists/2]).
|
||||
-export([content_types_accepted/2]).
|
||||
-export([from_text/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
allowed_methods(Req, State) ->
|
||||
{[<<"POST">>], Req, State}.
|
||||
|
||||
resource_exists(Req, State) ->
|
||||
{true, Req, State}.
|
||||
|
||||
content_types_accepted(Req, State) ->
|
||||
{[{{<<"application">>, <<"text">>, []}, from_text}], Req, State}.
|
||||
|
||||
from_text(Req=#{qs := Qs}, State) ->
|
||||
NewURI = [cowboy_req:uri(Req), "/foo"],
|
||||
case Qs of
|
||||
<<"created">> ->
|
||||
{{created, NewURI}, Req, State};
|
||||
<<"see_other">> ->
|
||||
{{see_other, NewURI}, Req, State}
|
||||
end.
|
|
@ -52,6 +52,7 @@ init_dispatch(_) ->
|
|||
{"/content_types_accepted", content_types_accepted_h, []},
|
||||
{"/content_types_provided", content_types_provided_h, []},
|
||||
{"/delete_resource", delete_resource_h, []},
|
||||
{"/create_resource", create_resource_h, []},
|
||||
{"/expires", expires_h, []},
|
||||
{"/generate_etag", generate_etag_h, []},
|
||||
{"/if_range", if_range_h, []},
|
||||
|
@ -474,6 +475,29 @@ delete_resource_missing(Config) ->
|
|||
{response, _, 500, _} = gun:await(ConnPid, Ref),
|
||||
ok.
|
||||
|
||||
create_resource_created(Config) ->
|
||||
doc("POST to an existing resource to create a new resource. "
|
||||
"When the accept callback returns {created, NewURI}, "
|
||||
"the expected reply is 201 Created."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/create_resource?created", [
|
||||
{<<"content-type">>, <<"application/text">>}
|
||||
], <<"hello">>, #{}),
|
||||
{response, _, 201, _} = gun:await(ConnPid, Ref),
|
||||
ok.
|
||||
|
||||
create_resource_see_other(Config) ->
|
||||
doc("POST to an existing resource to create a new resource. "
|
||||
"When the accept callback returns {see_other, NewURI}, "
|
||||
"the expected reply is 303 See Other with a location header set."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:post(ConnPid, "/create_resource?see_other", [
|
||||
{<<"content-type">>, <<"application/text">>}
|
||||
], <<"hello">>, #{}),
|
||||
{response, _, 303, RespHeaders} = gun:await(ConnPid, Ref),
|
||||
{_, _} = lists:keyfind(<<"location">>, 1, RespHeaders),
|
||||
ok.
|
||||
|
||||
error_on_malformed_accept(Config) ->
|
||||
doc("A malformed Accept header must result in a 400 response."),
|
||||
do_error_on_malformed_header(Config, <<"accept">>).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue