File: access_log.t

package info (click to toggle)
libplack-perl 1.0048-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,480 kB
  • sloc: perl: 5,288; python: 7; makefile: 4; javascript: 1
file content (98 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download | duplicates (5)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use strict;
use warnings;
use Test::More;
use HTTP::Request::Common;
use Plack::Test;
use Plack::Builder;
use POSIX;

my $log;

my $test = sub {
    my $format = shift;
    return sub {
        my $req = shift;
        my $app = builder {
            enable "Plack::Middleware::AccessLog",
                char_handlers => {
                    z => sub { shift->{HTTP_X_FORWARDED_FOR}, }
                },
                block_handlers => +{
                    Z => sub {
                        my ($block,$env) = @_;

                        $env->{$block} || '-'
                    }
                },
                logger => sub { $log = "@_" }, format => $format;
            sub { [ 200, [ 'Content-Type' => 'text/plain', 'Content-Length', 2 ], [ 'OK' ] ] };
        };
        test_psgi $app, sub { $_[0]->($req) };
    };
};

{
    my $req = GET "http://example.com/";
    $req->header("Host" => "example.com", "X-Forwarded-For" => "192.0.2.1");

    my $fmt = "%P %{Host}i %p %{X-Forwarded-For}i %{Content-Type}o %{%m %y}t %v";
    $test->($fmt)->($req);
    chomp $log;
    my $month_year = POSIX::strftime('%m %y', localtime);
    is $log, "$$ example.com 80 192.0.2.1 text/plain [$month_year] example.com";
}

{
    $test->("%D")->(GET "/");
    chomp $log;
    is $log, '-';
}

{
    my $req = GET "http://example.com/";
    my $fmt = "%r == %m %U%q %H";
    $test->($fmt)->($req);
    chomp $log;
    my ($r, $rs) = split / == /, $log;
    is $r, $rs;
}

{
    my $req = GET "http://example.com/foo?bar=baz";
    my $fmt = "%r == %m %U%q %H";
    $test->($fmt)->($req);
    chomp $log;
    my ($r, $rs) = split / == /, $log;
    is $r, $rs;
}

{
    my $req = GET "http://example.com/foo?bar=baz",
        x_forwarded_for => 'herp derp';
    my $fmt = "%m %z";
    $test->($fmt)->($req);
    chomp $log;
    is $log, 'GET herp derp';
}

{
    my $req = GET "http://example.com/foo?bar=baz",
        x_rand_r => 'station';
    my $fmt = "%m %{HTTP_X_RAND_R}Z";
    $test->($fmt)->($req);
    chomp $log;
    is $log, 'GET station';
}

{
    my $req = POST "http://example.com/foo", [ "bar", "baz" ];
    my $fmt = "cti=%{Content-Type}i cli=%{Content-Length}i cto=%{Content-Type}o clo=%{Content-Length}o";
    $test->($fmt)->($req);
    chomp $log;

    my %vals = split /[= ]/, $log;
    is_deeply \%vals, { cti => "application/x-www-form-urlencoded", cli => 7,
                        cto => 'text/plain', clo => 2 };
}

done_testing;