From e0b036fe68579b3ffb69b450acf2d17bb7162cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 2 Nov 2018 13:54:19 +0100 Subject: Add tests for charsets_provided Fix cases where the q-value is 0 and where a wildcard was sent in the accept-charset header. Also don't send a charset in the content-type of the response if the media type is not text. Thanks to Philip Witty for help figuring this out. --- test/handlers/charsets_provided_empty_h.erl | 30 +++++++++++++++++++++++++++++ test/handlers/charsets_provided_h.erl | 28 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/handlers/charsets_provided_empty_h.erl create mode 100644 test/handlers/charsets_provided_h.erl (limited to 'test/handlers') diff --git a/test/handlers/charsets_provided_empty_h.erl b/test/handlers/charsets_provided_empty_h.erl new file mode 100644 index 0000000..0548a5e --- /dev/null +++ b/test/handlers/charsets_provided_empty_h.erl @@ -0,0 +1,30 @@ +%% This module has a text and non-text media type, +%% but provides no charset. All requests will result +%% in a 406 not acceptable. + +-module(charsets_provided_empty_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([charsets_provided/2]). +-export([get_text_plain/2]). +-export([get_application_json/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[ + {{<<"text">>, <<"plain">>, []}, get_text_plain}, + {{<<"application">>, <<"json">>, []}, get_application_json} + ], Req, State}. + +charsets_provided(Req, State) -> + {[], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + +get_application_json(Req, State) -> + {<<"{\"hello\": \"rest\"}">>, Req, State}. + diff --git a/test/handlers/charsets_provided_h.erl b/test/handlers/charsets_provided_h.erl new file mode 100644 index 0000000..4a08e78 --- /dev/null +++ b/test/handlers/charsets_provided_h.erl @@ -0,0 +1,28 @@ +%% This module has a text and non-text media type, +%% and provides two charsets. + +-module(charsets_provided_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([charsets_provided/2]). +-export([get_text_plain/2]). +-export([get_application_json/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[ + {{<<"text">>, <<"plain">>, []}, get_text_plain}, + {{<<"application">>, <<"json">>, []}, get_application_json} + ], Req, State}. + +charsets_provided(Req, State) -> + {[<<"utf-8">>, <<"utf-16">>], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + +get_application_json(Req, State) -> + {<<"{\"hello\": \"rest\"}">>, Req, State}. -- cgit v1.2.3