mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Better handle content negotiation when accept contains charsets
Thanks to Philip Witty for help figuring this out.
This commit is contained in:
parent
d4dff21055
commit
399b6a16b4
6 changed files with 231 additions and 19 deletions
22
test/handlers/charset_in_content_types_provided_h.erl
Normal file
22
test/handlers/charset_in_content_types_provided_h.erl
Normal file
|
@ -0,0 +1,22 @@
|
|||
%% This module has a media type provided with an explicit charset.
|
||||
|
||||
-module(charset_in_content_types_provided_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([charsets_provided/2]).
|
||||
-export([get_text_plain/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, [{<<"charset">>, <<"utf-8">>}]}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
charsets_provided(Req, State) ->
|
||||
{[<<"utf-16">>, <<"iso-8861-1">>], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
|
@ -0,0 +1,24 @@
|
|||
%% This module has a media type provided with a wildcard
|
||||
%% and a list of charsets that is limited.
|
||||
|
||||
-module(charset_in_content_types_provided_implicit_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([content_types_provided/2]).
|
||||
-export([charsets_provided/2]).
|
||||
-export([get_text_plain/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
{cowboy_rest, Req, Opts}.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, '*'}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
charsets_provided(Req, State) ->
|
||||
{[<<"utf-8">>, <<"utf-16">>], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
%% This module has a media type provided with a wildcard
|
||||
%% and lacks a charsets_provided callback.
|
||||
|
||||
-module(charset_in_content_types_provided_implicit_no_callback_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, State) ->
|
||||
{[
|
||||
{{<<"text">>, <<"plain">>, '*'}, get_text_plain}
|
||||
], Req, State}.
|
||||
|
||||
get_text_plain(Req, State) ->
|
||||
{<<"This is REST!">>, Req, State}.
|
||||
|
||||
|
|
@ -39,6 +39,12 @@ end_per_group(Name, _) ->
|
|||
init_dispatch(_) ->
|
||||
cowboy_router:compile([{'_', [
|
||||
{"/", rest_hello_h, []},
|
||||
{"/charset_in_content_types_provided",
|
||||
charset_in_content_types_provided_h, []},
|
||||
{"/charset_in_content_types_provided_implicit",
|
||||
charset_in_content_types_provided_implicit_h, []},
|
||||
{"/charset_in_content_types_provided_implicit_no_callback",
|
||||
charset_in_content_types_provided_implicit_no_callback_h, []},
|
||||
{"/provide_callback_missing", provide_callback_missing_h, []},
|
||||
{"/switch_handler", switch_handler_h, run},
|
||||
{"/switch_handler_opts", switch_handler_h, hibernate}
|
||||
|
@ -74,6 +80,74 @@ error_on_malformed_if_none_match(Config) ->
|
|||
{response, _, 400, _} = gun:await(ConnPid, Ref),
|
||||
ok.
|
||||
|
||||
charset_in_content_types_provided(Config) ->
|
||||
doc("When a charset is matched explictly in content_types_provided, "
|
||||
"that charset is used and the charsets_provided callback is ignored."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/charset_in_content_types_provided", [
|
||||
{<<"accept">>, <<"text/plain;charset=utf-8">>},
|
||||
{<<"accept-charset">>, <<"utf-16, utf-8;q=0.5">>},
|
||||
{<<"accept-encoding">>, <<"gzip">>}
|
||||
]),
|
||||
{response, _, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
{_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||
ok.
|
||||
|
||||
charset_in_content_types_provided_implicit_match(Config) ->
|
||||
doc("When a charset is matched implicitly in content_types_provided, "
|
||||
"the charsets_provided callback is used to determine if the media "
|
||||
"type will match."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
|
||||
{<<"accept">>, <<"text/plain;charset=utf-16">>},
|
||||
{<<"accept-encoding">>, <<"gzip">>}
|
||||
]),
|
||||
{response, _, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
{_, <<"text/plain; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||
ok.
|
||||
|
||||
charset_in_content_types_provided_implicit_nomatch(Config) ->
|
||||
doc("When a charset is matched implicitly in content_types_provided, "
|
||||
"the charsets_provided callback is used to determine if the media "
|
||||
"type will match. If it doesn't, try the next media type."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
|
||||
{<<"accept">>, <<"text/plain;charset=utf-32, text/plain">>},
|
||||
{<<"accept-encoding">>, <<"gzip">>}
|
||||
]),
|
||||
{response, _, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
%% We end up with the first charset listed in charsets_provided.
|
||||
{_, <<"text/plain; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||
ok.
|
||||
|
||||
charset_in_content_types_provided_implicit_nomatch_error(Config) ->
|
||||
doc("When a charset is matched implicitly in content_types_provided, "
|
||||
"the charsets_provided callback is used to determine if the media "
|
||||
"type will match. If it doesn't, and there's no other media type, "
|
||||
"a 406 is returned."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit", [
|
||||
{<<"accept">>, <<"text/plain;charset=utf-32">>},
|
||||
{<<"accept-encoding">>, <<"gzip">>}
|
||||
]),
|
||||
{response, _, 406, _} = gun:await(ConnPid, Ref),
|
||||
ok.
|
||||
|
||||
charset_in_content_types_provided_implicit_no_callback(Config) ->
|
||||
doc("When a charset is matched implicitly in content_types_provided, "
|
||||
"and the charsets_provided callback is not exported, the media "
|
||||
"type will match but the charset will be ignored like all other "
|
||||
"parameters."),
|
||||
ConnPid = gun_open(Config),
|
||||
Ref = gun:get(ConnPid, "/charset_in_content_types_provided_implicit_no_callback", [
|
||||
{<<"accept">>, <<"text/plain;charset=utf-32">>},
|
||||
{<<"accept-encoding">>, <<"gzip">>}
|
||||
]),
|
||||
{response, _, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
%% The charset is ignored as if it was any other parameter.
|
||||
{_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||
ok.
|
||||
|
||||
provide_callback_missing(Config) ->
|
||||
doc("A 500 response must be sent when the ProvideCallback can't be called."),
|
||||
ConnPid = gun_open(Config),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue