aboutsummaryrefslogtreecommitdiffstats
path: root/test/rest_handler_SUITE.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-02 12:36:51 +0100
committerLoïc Hoguin <[email protected]>2018-11-02 13:49:54 +0100
commit399b6a16b4a571e293437dcc8f85808f83b32ff6 (patch)
tree5b1d5b0e06b1f3bf08f1a858770ff00fe3c2fe3f /test/rest_handler_SUITE.erl
parentd4dff2105500d960479d278981f42d42beb54175 (diff)
downloadcowboy-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/rest_handler_SUITE.erl')
-rw-r--r--test/rest_handler_SUITE.erl74
1 files changed, 74 insertions, 0 deletions
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),