summary refs log tree commit diff
path: root/t/05_filter_path.t
blob: fa866083f79f7d199ff3acce3552e08cc5ad37e1 (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
36
37
38
39
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     => 1,
        backend => Plack::Middleware::Throttle::Backend::Hash->new(),
        path    => qr{^/api};
    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/bar";
            my $res = $cb->($req);
            is $res->content, 'hello world', 'content is valid';
            ok !$res->header('X-RateLimit-Limit'), 'no header ratelimit';
        }
        my $req = GET "http://localhost/api";
        my $res = $cb->($req);
        is $res->content, 'hello world', 'content is valid';
        ok $res->header('X-RateLimit-Limit'), 'header ratelimit';
        $req = GET "http://localhost/api";
        $res = $cb->($req);
        is $res->code, 503, 'rate limit exceeded';
    }
};

done_testing;