-
Notifications
You must be signed in to change notification settings - Fork 9
/
basic.t
294 lines (275 loc) · 8.3 KB
/
basic.t
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use strict;
use warnings;
use Test::More;
use Plack::App::Proxy;
use Plack::App::Proxy::Test;
# regular static proxy
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]") },
app => sub {
my $env = shift;
is $env->{PATH_INFO}, '/index.html', 'PATH_INFO accessed';
return [ 200, [], [ ('x') x 123, ('y') x 111 ] ];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => "http://localhost/index.html");
my $res = $cb->($req);
ok $res->is_success, "Check the status line.";
is $res->content, ('x' x 123) . ('y' x 111), "static proxy";
},
);
# Get the proxy host from the Host header
{
my ( $app_host, $app_port );
test_proxy(
proxy => sub {
# save the app's host and port for client.
( $app_host, $app_port ) = @_;
my $app = Plack::App::Proxy->new->to_app;
sub {
my $env = shift;
# Host callback returns forbidden response instead of host
return [ 403, [], [ "forbidden" ] ]
if $env->{PATH_INFO} =~ m(^/secret);
$env->{'plack.proxy.remote'} = 'http://' . $env->{HTTP_HOST};
$app->($env);
};
},
app => sub { [ 200, [], ["WORLD"] ] },
client => sub {
my $cb = shift;
my $req1 = HTTP::Request->new(
GET => "http://localhost/index.html",
[ Host => "$app_host:$app_port" ]
);
my $res1 = $cb->($req1);
is $res1->content, "WORLD", "dynamic host";
my $req2 = HTTP::Request->new(GET => "http://localhost/secret/");
my $res2 = $cb->($req2);
is $res2->code, 403, "dynamic host forbidden reponse";
},
);
}
# Don't rewrite the Host header
test_proxy(
proxy => sub { Plack::App::Proxy->new(
remote => "http://$_[0]:$_[1]", preserve_host_header => 1,
) },
app => sub {
my $env = shift;
is $env->{HTTP_HOST}, "__TEST__", "preserve host header";
[ 200, [], [ 'DUMMY' ] ];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(
GET => "http://localhost/", [Host => "__TEST__"]);
my $res = $cb->($req);
is $res->code, 200, "success the request.";
},
);
# Get the full URL from a middleware. This example is an open proxy, don't do this!
{
my ( $app_host, $app_port );
test_proxy(
proxy => sub {
# save the app's host and port for client.
( $app_host, $app_port ) = @_;
my $app = Plack::App::Proxy->new->to_app;
sub {
my $env = shift;
my ( $url ) = ( $env->{PATH_INFO} =~ m(^\/(https?://.*)) )
or return [ 403, [], [ "forbidden" ] ];
$env->{'plack.proxy.url'} = $url;
$app->($env);
};
},
app => sub { [ 200, [], ["HELLO"] ] },
client => sub {
my $cb = shift;
my $req1 = HTTP::Request->new(
GET => "http://localhost/http://$app_host:$app_port/"
);
my $res1 = $cb->($req1);
is $res1->content, "HELLO", "url callback";
my $req2 = HTTP::Request->new(GET => "http://localhost/index.html");
my $res2 = $cb->($req2);
is $res2->code, 403, "dynamic URL forbidden reponse";
},
);
}
# with QUERY_STRING
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]") },
app => sub {
my $env = shift;
is $env->{QUERY_STRING}, 'k1=v1&k2=v2';
return [ 200, [], [ "HELLO" ] ];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(
GET => "http://localhost/proxy/?k1=v1&k2=v2"
);
my $res = $cb->($req);
is $res->content, 'HELLO';
},
);
# avoid double slashes
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]/") },
app => sub {
my $env = shift;
return [ 200, [], [ $env->{PATH_INFO} ] ];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(
GET => "http://localhost/foo",
);
my $res = $cb->($req);
is $res->content, '/foo';
},
);
# redirect
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]") },
app => sub {
my $env = shift;
if( $env->{PATH_INFO} eq '/index.html' ){
return [ 302, [
Location => 'http://' . $env->{HTTP_HOST} . '/hello.html'
], [] ];
}
return [ 200, [], [ "HELLO" ] ];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new( GET => "http://localhost/index.html" );
my $res = $cb->($req);
like $res->header( 'Location' ), qr(\bhello\.html),
"pass the Location header to the client directly";
},
);
# Don't freeze on servers without psgi.nonblocking supports.
test_proxy(
proxy => sub {
my $proxy = Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]");
sub {
my $env = shift;
if( $env->{PATH_INFO} eq '/error' ){
$env->{'plack.proxy.url'} = '!! BADURL to make AE::HTTP error!!' ;
}
return $proxy->( $env );
};
},
app => sub {
my $env = shift;
if( $env->{PATH_INFO} eq '/redirect' ){
return [ 302, [ Location => 'http://d.hatena.ne.jp/hiratara' ], [] ];
}else{
return [ 200, [ 'Content-Type' => 'text/plain'], [ "HELLO" ] ];
}
},
client => sub {
my $cb = shift;
my $res;
$res = $cb->(
HTTP::Request->new( GET => "http://localhost/redirect" )
);
is $res->code, 302, 'Success the redirect request.';
$res = $cb->(
HTTP::Request->new( GET => "http://localhost/error" )
);
like $res->code, qr/^(?:400|502)$/, 'Success the error request.';
$res = $cb->(
HTTP::Request->new( GET => "http://localhost/" )
);
is $res->code, 200, 'Success all requests.';
},
);
# server tries to set one cookie
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]" ) },
app => sub {
my $env = shift;
is $env->{PATH_INFO}, '/index.html', 'PATH_INFO accessed';
return [
200,
[
'Set-Cookie',
'foo=bar; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure; HttpOnly',
],
[ ('x') x 123, ('y') x 111 ]
];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => "http://localhost/index.html");
my $res = $cb->($req);
ok $res->is_success, "Check the status line.";
is $res->content, ('x' x 123) . ('y' x 111), "static proxy";
my @cookies = $res->header( 'Set-Cookie' );
is $#cookies, 0, 'one cookies sent by server';
},
);
# server tries to set two cookies
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]" ) },
app => sub {
my $env = shift;
is $env->{PATH_INFO}, '/index.html', 'PATH_INFO accessed';
return [
200,
[
'Set-Cookie',
'foo=bar; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure; HttpOnly',
'Set-Cookie',
'bar=foo; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure; HttpOnly',
],
[ ('x') x 123, ('y') x 111 ]
];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => "http://localhost/index.html");
my $res = $cb->($req);
ok $res->is_success, "Check the status line.";
is $res->content, ('x' x 123) . ('y' x 111), "static proxy";
my @cookies = $res->header( 'Set-Cookie' );
is $#cookies, 1, 'two cookies sent by server';
},
);
# server tries to set four cookies
test_proxy(
proxy => sub { Plack::App::Proxy->new(remote => "http://$_[0]:$_[1]" ) },
app => sub {
my $env = shift;
is $env->{PATH_INFO}, '/index.html', 'PATH_INFO accessed';
return [
200,
[
'Set-Cookie',
'foo=bar; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure; HttpOnly',
'Set-Cookie',
'bar=foo',
'Set-Cookie',
'third=some value; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure; HttpOnly',
'Set-Cookie',
'fifth=some othervalue; path=/blah; expires Sun, 31-Aug-2025 11:28:00 GMT; secure',
],
[ ('x') x 123, ('y') x 111 ]
];
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => "http://localhost/index.html");
my $res = $cb->($req);
ok $res->is_success, "Check the status line.";
is $res->content, ('x' x 123) . ('y' x 111), "static proxy";
my @cookies = $res->header( 'Set-Cookie' );
is $#cookies, 3, 'four cookies sent by server';
},
);
done_testing;