summaryrefslogtreecommitdiffstats
path: root/t/02-inputs.t
blob: faa5e5af8c78c6e10de0c844f619b5d76cfef49d (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib';
use Test::More;
use Plack::Client::Test;

use HTTP::Message::PSGI;

my $app = <<'APP';
sub {
    my $env = shift;
    return [
        200,
        ['Content-Type' => 'text/plain'],
        [
            (map { ($env->{$_} || '') . "\n" }
                 qw(
                    REQUEST_METHOD
                    REQUEST_URI
                    CONTENT_LENGTH
                 )),
            (map { ucfirst(lc) . ': ' . $env->{"HTTP_X_$_"} . "\n" }
                 grep { $_ ne 'FORWARDED_FOR' } grep { s/^HTTP_X_// }
                      keys %$env),
            do {
                my $fh = $env->{'psgi.input'};
                $fh->read(my $body, $env->{CONTENT_LENGTH});
                $body;
            },
        ]
    ];
}
APP

test_tcp_plackup(
    $app,
    sub {
        my $base_uri = shift;

        test_responses($base_uri, Plack::Client->new);
    },
);

{
    my $apps = {
        foo => eval $app,
    };
    my $base_uri = 'psgi-local://foo';

    test_responses($base_uri, Plack::Client->new(apps => $apps));
}

sub test_responses {
    my ($base_uri, $client) = @_;
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    response_is(
        $client->get($base_uri),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '7'],
        "GET\n/\n\n"
    );

    response_is(
        $client->get($base_uri . '/'),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '7'],
        "GET\n/\n\n"
    );

    response_is(
        $client->get($base_uri . '/foo'),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '10'],
        "GET\n/foo\n\n"
    );

    response_is(
        $client->get($base_uri . '/foo', ['X-Foo' => 'bar']),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '19'],
        "GET\n/foo\n\nFoo: bar\n"
    );

    response_is(
        $client->get($base_uri . '/foo', HTTP::Headers->new('X-Foo' => 'bar')),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '19'],
        "GET\n/foo\n\nFoo: bar\n"
    );

    response_is(
        $client->post($base_uri, [], "foo"),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '12'],
        "POST\n/\n3\nfoo",
    );

    response_is(
        $client->put($base_uri, [], "foo"),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '11'],
        "PUT\n/\n3\nfoo",
    );

    response_is(
        $client->delete($base_uri),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '10'],
        "DELETE\n/\n\n",
    );

    response_is(
        $client->head($base_uri),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '8'],
        "", # length("HEAD\n/\n\n") == 8
    );

    response_is(
        $client->request(HTTP::Request->new(GET => $base_uri)),
        200,
        ['Content-Type' => 'text/plain', 'Content-Length' => '7'],
        "GET\n/\n\n"
    );

    {
        my $base = URI->new($base_uri);
        my $uri = $base->clone;
        $uri->scheme('http');
        $uri->path('/') unless $uri->path;
        my $env = HTTP::Request->new(GET => $uri)->to_psgi;
        $env->{'plack.client.url_scheme'} = $base->scheme;
        $env->{'plack.client.app_name'} = $base->authority
            if $base->scheme eq 'psgi-local';
        response_is(
            $client->request($env),
            200,
            ['Content-Type' => 'text/plain', 'Content-Length' => '7'],
            "GET\n/\n\n"
        );
    }
}

done_testing;