diff options
author | Loïc Hoguin <[email protected]> | 2018-11-02 12:36:51 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-11-02 13:49:54 +0100 |
commit | 399b6a16b4a571e293437dcc8f85808f83b32ff6 (patch) | |
tree | 5b1d5b0e06b1f3bf08f1a858770ff00fe3c2fe3f /test | |
parent | d4dff2105500d960479d278981f42d42beb54175 (diff) | |
download | cowboy-399b6a16b4a571e293437dcc8f85808f83b32ff6.tar.gz cowboy-399b6a16b4a571e293437dcc8f85808f83b32ff6.tar.bz2 cowboy-399b6a16b4a571e293437dcc8f85808f83b32ff6.zip |
Better handle content negotiation when accept contains charsets
Thanks to Philip Witty for help figuring this out.
Diffstat (limited to 'test')
4 files changed, 141 insertions, 0 deletions
diff --git a/test/handlers/charset_in_content_types_provided_h.erl b/test/handlers/charset_in_content_types_provided_h.erl new file mode 100644 index 0000000..4774407 --- /dev/null +++ b/test/handlers/charset_in_content_types_provided_h.erl @@ -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}. diff --git a/test/handlers/charset_in_content_types_provided_implicit_h.erl b/test/handlers/charset_in_content_types_provided_implicit_h.erl new file mode 100644 index 0000000..fcdd0c8 --- /dev/null +++ b/test/handlers/charset_in_content_types_provided_implicit_h.erl @@ -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}. + diff --git a/test/handlers/charset_in_content_types_provided_implicit_no_callback_h.erl b/test/handlers/charset_in_content_types_provided_implicit_no_callback_h.erl new file mode 100644 index 0000000..ae17af3 --- /dev/null +++ b/test/handlers/charset_in_content_types_provided_implicit_no_callback_h.erl @@ -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}. + + diff --git a/test/rest_handler_SUITE.erl b/test/rest_handler_SUITE.erl index a9884b5..491c7bb 100644 --- a/test/rest_handler_SUITE.erl +++ b/test/rest_handler_SUITE.erl @@ -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), |