diff options
author | franck cuny <franck@lumberjaph.net> | 2010-03-01 17:56:34 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-03-01 17:56:34 +0100 |
commit | 46fc47df0eace23987c3a4fa0518f4f87e7897ca (patch) | |
tree | e2c24bfda57972b0f6f44da8314c4c8d5bce894d /t | |
parent | initial commit (diff) | |
download | plack-middleware-apiratelimit-46fc47df0eace23987c3a4fa0518f4f87e7897ca.tar.gz |
a middleware to throttle request on an API
Diffstat (limited to 't')
-rw-r--r-- | t/01_basic.t | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/t/01_basic.t b/t/01_basic.t new file mode 100644 index 0000000..5fb3212 --- /dev/null +++ b/t/01_basic.t @@ -0,0 +1,43 @@ +use strict; +use warnings; +use Test::More; + +use Plack::Test; +use Plack::Builder; +use HTTP::Request::Common; + +#use AnyEvent::Redis; +#my $redis = AnyEvent::Redis->new(port => 6379, server => '127.0.0.1'); +#$redis->flushall; + +my $handler = builder { + enable "APIRateLimit"; + #enable "APIRateLimit", requests_per_hour => 2, backend => "Hash"; + #enable "APIRateLimit", + #requests_per_hour => 2, + #backend => [ "Redis", { port => 6379, server => '127.0.0.1' } ]; + sub { [ '200', [ 'Content-Type' => 'text/html' ], ['hello world'] ] }; +}; + +test_psgi + app => $handler, + client => sub { + my $cb = shift; + use YAML::Syck; + { + for ( 1 .. 2 ) { + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 200; + ok $res->headers('X-RateLimit-Limit'); +# warn Dump $res; + } + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 503; + ok $res->headers('X-RateLimit-Reset'); +# warn Dump $res; + } +}; + +done_testing; |