From 60faca51cb5ecdd89daa61a6c897dacac813aaa8 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Thu, 1 Apr 2010 17:02:50 +0200 Subject: add tests --- t/02_daily.t | 35 +++++++++++++++++++++++++++++++++++ t/03_interval.t | 38 ++++++++++++++++++++++++++++++++++++++ t/04_combine.t | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 t/02_daily.t create mode 100644 t/03_interval.t create mode 100644 t/04_combine.t diff --git a/t/02_daily.t b/t/02_daily.t new file mode 100644 index 0000000..5353206 --- /dev/null +++ b/t/02_daily.t @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More; + +use Plack::Test; +use Plack::Builder; +use HTTP::Request::Common; +use Plack::Middleware::Throttle::Backend::Hash; + +my $handler = builder { + enable "Throttle::Daily", + max => 2, + backend => Plack::Middleware::Throttle::Backend::Hash->new(); + sub { [ '200', [ 'Content-Type' => 'text/html' ], ['hello world'] ] }; +}; + +test_psgi + app => $handler, + client => sub { + my $cb = shift; + { + for ( 1 .. 2 ) { + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 200, 'http response is 200'; + ok $res->headers('X-RateLimit-Limit'), 'header ratelimit'; + } + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 503, 'http response is 503'; + ok $res->headers('X-RateLimit-Reset'), 'header reset'; + } + }; + +done_testing; diff --git a/t/03_interval.t b/t/03_interval.t new file mode 100644 index 0000000..db0aafe --- /dev/null +++ b/t/03_interval.t @@ -0,0 +1,38 @@ +use strict; +use warnings; +use Test::More; + +use Plack::Test; +use Plack::Builder; +use HTTP::Request::Common; +use Plack::Middleware::Throttle::Backend::Hash; + +my $handler = builder { + enable "Throttle::Interval", + min => 2, + backend => Plack::Middleware::Throttle::Backend::Hash->new(); + sub { [ '200', [ 'Content-Type' => 'text/html' ], ['hello world'] ] }; +}; + +test_psgi + app => $handler, + client => sub { + my $cb = shift; + { + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 200, 'http response is 200'; + for ( 1 .. 2 ) { + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 503, 'http response is 503'; + ok $res->headers('X-RateLimit-Reset'), 'header reset'; + } + sleep(3); + $req = GET "http://localhost/"; + $res = $cb->($req); + is $res->code, 200, 'http response is 200'; + } + }; + +done_testing; diff --git a/t/04_combine.t b/t/04_combine.t new file mode 100644 index 0000000..31a4e98 --- /dev/null +++ b/t/04_combine.t @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More; + +use Plack::Test; +use Plack::Builder; +use HTTP::Request::Common; +use Plack::Middleware::Throttle::Backend::Hash; + +my $handler = builder { + enable "Throttle::Hourly", + max => 2, + backend => Plack::Middleware::Throttle::Backend::Hash->new(); + sub { [ '200', [ 'Content-Type' => 'text/html' ], ['hello world'] ] }; +}; + +test_psgi + app => $handler, + client => sub { + my $cb = shift; + { + for ( 1 .. 2 ) { + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 200, 'http response is 200'; + ok $res->headers('X-RateLimit-Limit'), 'header ratelimit'; + } + my $req = GET "http://localhost/"; + my $res = $cb->($req); + is $res->code, 503, 'http response is 503'; + ok $res->headers('X-RateLimit-Reset'), 'header reset'; + } + }; + +done_testing; -- cgit v1.2.3