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

Add the rate_limited/2 REST callback

This commit is contained in:
Loïc Hoguin 2018-11-04 11:51:35 +01:00
parent bf7ccc8623
commit 8c9ad7bf07
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
8 changed files with 597 additions and 190 deletions

View file

@ -48,6 +48,7 @@ init_dispatch(_) ->
{"/charset_in_content_types_provided_implicit_no_callback",
charset_in_content_types_provided_implicit_no_callback_h, []},
{"/provide_callback_missing", provide_callback_missing_h, []},
{"/rate_limited", rate_limited_h, []},
{"/switch_handler", switch_handler_h, run},
{"/switch_handler_opts", switch_handler_h, hibernate}
]}]).
@ -287,6 +288,31 @@ provide_callback_missing(Config) ->
{response, fin, 500, _} = gun:await(ConnPid, Ref),
ok.
rate_limited(Config) ->
doc("A 429 response must be sent when the rate_limited callback returns true. "
"The retry-after header is specified as an integer."),
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/rate_limited?true", [{<<"accept-encoding">>, <<"gzip">>}]),
{response, fin, 429, Headers} = gun:await(ConnPid, Ref),
{_, <<"3600">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
ok.
rate_limited_datetime(Config) ->
doc("A 429 response must be sent when the rate_limited callback returns true. "
"The retry-after header is specified as a date/time tuple."),
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/rate_limited?true-date", [{<<"accept-encoding">>, <<"gzip">>}]),
{response, fin, 429, Headers} = gun:await(ConnPid, Ref),
{_, <<"Fri, 22 Feb 2222 11:11:11 GMT">>} = lists:keyfind(<<"retry-after">>, 1, Headers),
ok.
rate_not_limited(Config) ->
doc("A success response must be sent when the rate_limited callback returns false."),
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/rate_limited?false", [{<<"accept-encoding">>, <<"gzip">>}]),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
ok.
switch_handler(Config) ->
doc("Switch REST to loop handler for streaming the response body."),
ConnPid = gun_open(Config),