summary refs log tree commit diff
path: root/t/01_basic.t
blob: 336a6e2fd3a576b43b9076459d2e334b87dcf0b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use strict;
use warnings;
use Test::More;

use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;
use Plack::Middleware::APIRateLimit::Backend::Hash;

my $handler = builder {
    enable "APIRateLimit",
        requests_per_hour => 2,
        backend => Plack::Middleware::APIRateLimit::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;