summaryrefslogtreecommitdiffstats
path: root/t/01-basic.t
blob: f4cf52acda38f39170dbf22d94bf53d7ea80ff21 (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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::TCP;

use Plack::Util;

use Plack::Client;

sub full_body {
    my ($body) = @_;

    return $body unless ref($body);

    my $ret = '';
    Plack::Util::foreach($body, sub { $ret .= $_[0] });
    return $ret;
}

sub check_headers {
    my ($got, $expected) = @_;
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    isa_ok($got, 'HTTP::Headers');

    if (ref($expected) eq 'ARRAY') {
        $expected = HTTP::Headers->new(@$expected);
    }
    elsif (ref($expected) eq 'HASH') {
        $expected = HTTP::Headers->new(%$expected);
    }
    isa_ok($expected, 'HTTP::Headers');

    my @expected_keys = $expected->header_field_names;
    my @got_keys = $got->header_field_names;

    my %default_headers = map { $_ => 1 } qw(
        Date Server Content-Length Client-Date Client-Peer Client-Response-Num
    );
    my %expected_exists = map { $_ => 1 } @expected_keys;

    for my $header (@expected_keys) {
        is($got->header($header), $expected->header($header),
           "$header header is the same");
    }

    for my $header (@got_keys) {
        next if $default_headers{$header};
        next if $expected_exists{$header};
        fail("got extra header $header");
    }
}

test_tcp(
    client => sub {
        my $port = shift;

        my $client = Plack::Client->new;
        isa_ok($client, 'Plack::Client');

        my $base_url = 'http://localhost:' . $port;

        {
            my $res = $client->get($base_url . '/');
            isa_ok($res, 'Plack::Response');
            is($res->status, 200, "right status");
            check_headers($res->headers, ["Content-Type" => "text/plain"]);
            is(full_body($res->body), '/', "right body");
        }

        {
            my $res = $client->get($base_url . '/foo');
            isa_ok($res, 'Plack::Response');
            is($res->status, 200, "right status");
            check_headers($res->headers, ["Content-Type" => "text/plain"]);
            is(full_body($res->body), '/foo', "right body");
        }
    },
    server => sub {
        my $port = shift;
        exec('plackup', '--port', $port, '-e', 'sub { [ 200, ["Content-Type" => "text/plain"], [shift->{PATH_INFO}] ] }');
    },
);

{
    my $apps = {
        foo => sub {
            [
                200,
                ["Content-Type" => "text/plain"],
                [scalar reverse shift->{PATH_INFO}]
            ]
        },
    };
    my $client = Plack::Client->new(apps => $apps);
    isa_ok($client, 'Plack::Client');
    is($client->apps, $apps, "got apps back");
    is($client->app_for('foo'), $apps->{foo}, "got the right app");
    is($client->app_for('bar'), undef, "didn't get nonexistent app");

    {
        my $res = $client->get('psgi://foo/');
        isa_ok($res, 'Plack::Response');
        is($res->status, 200, "right status");
        check_headers($res->headers, ["Content-Type" => "text/plain"]);
        is(full_body($res->body), '/', "right body");
    }

    {
        my $res = $client->get('psgi://foo/foo');
        isa_ok($res, 'Plack::Response');
        is($res->status, 200, "right status");
        check_headers($res->headers, ["Content-Type" => "text/plain"]);
        is(full_body($res->body), 'oof/', "right body");
    }
}

done_testing;