0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Add a cowboy_rest test for malformed if-*-match headers

This commit is contained in:
Loïc Hoguin 2018-10-31 22:41:59 +01:00
parent 09bf1199aa
commit b3bfcf068c
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
2 changed files with 37 additions and 0 deletions

View file

@ -0,0 +1,16 @@
%% This module sends a hello world response via a REST handler.
-module(rest_hello_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}.

View file

@ -38,6 +38,7 @@ end_per_group(Name, _) ->
init_dispatch(_) -> init_dispatch(_) ->
cowboy_router:compile([{'_', [ cowboy_router:compile([{'_', [
{"/", rest_hello_h, []},
{"/provide_callback_missing", provide_callback_missing_h, []}, {"/provide_callback_missing", provide_callback_missing_h, []},
{"/switch_handler", switch_handler_h, run}, {"/switch_handler", switch_handler_h, run},
{"/switch_handler_opts", switch_handler_h, hibernate} {"/switch_handler_opts", switch_handler_h, hibernate}
@ -53,6 +54,26 @@ do_decode(Headers, Body) ->
%% Tests. %% Tests.
error_on_malformed_if_match(Config) ->
doc("A malformed If-Match header must result in a 400 response."),
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/", [
{<<"accept-encoding">>, <<"gzip">>},
{<<"if-match">>, <<"bad">>}
]),
{response, _, 400, _} = gun:await(ConnPid, Ref),
ok.
error_on_malformed_if_none_match(Config) ->
doc("A malformed If-None-Match header must result in a 400 response."),
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/", [
{<<"accept-encoding">>, <<"gzip">>},
{<<"if-none-match">>, <<"bad">>}
]),
{response, _, 400, _} = gun:await(ConnPid, Ref),
ok.
provide_callback_missing(Config) -> provide_callback_missing(Config) ->
doc("A 500 response must be sent when the ProvideCallback can't be called."), doc("A 500 response must be sent when the ProvideCallback can't be called."),
ConnPid = gun_open(Config), ConnPid = gun_open(Config),