Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote branch 'hiratara/rewritelocation_mapped'
- Loading branch information
Showing
4 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use strict; | ||
use warnings; | ||
use HTTP::Request::Common qw(GET); | ||
use Plack::App::Proxy; | ||
use Plack::App::Proxy::Test; | ||
use Plack::Builder; | ||
use Test::More; | ||
|
||
sub test_rewriting_path($$$) { | ||
my ($from, $to, $redirect_to) = @_; | ||
|
||
s!/$!! for $from, $to; | ||
|
||
test_proxy( | ||
proxy => sub { | ||
my ($host, $port) = @_; | ||
return builder { | ||
enable 'Proxy::RewriteLocation'; | ||
mount "$from/" => Plack::App::Proxy->new( | ||
remote => "http://$host:$port$to" | ||
); | ||
}; | ||
}, | ||
app => sub { | ||
my $env = shift; | ||
|
||
if ($env->{PATH_INFO} eq "$to/redirect") { | ||
return [ | ||
301, | ||
[Location => "http://$env->{HTTP_HOST}$to$redirect_to"], | ||
['Redirected'] | ||
]; | ||
} | ||
|
||
return [ | ||
200, | ||
[ | ||
"Content-Type" => "text/plain", | ||
"X-Request-URI" => $env->{REQUEST_URI} | ||
], | ||
["OK\n"], | ||
]; | ||
}, | ||
client => sub { | ||
my $cb = shift; | ||
|
||
my $res = $cb->(GET "http://localhost$from/redirect"); | ||
is $res->code, 301, 'got right status to redirect'; | ||
like $res->header('Location'), | ||
qr!^http://[^/]+\Q$from$redirect_to\E$!, | ||
'got right proxied redirect URL'; | ||
|
||
$res = $cb->(GET $res->header('Location')); | ||
like $res->header('X-Request-URI'), qr!^\Q$to$redirect_to\E$!, | ||
'arrived in the target http server' | ||
}, | ||
); | ||
} | ||
|
||
test_rewriting_path "/" => "/", "/goal"; | ||
test_rewriting_path "/" => "/foo", "/goal"; | ||
test_rewriting_path "/foo" => "/", "/goal"; | ||
test_rewriting_path "/foo" => "/bar", "/goal"; | ||
test_rewriting_path "/bar" => "/foo/bar", "/goal"; | ||
test_rewriting_path "/foo/bar" => "/bar", "/goal"; | ||
test_rewriting_path "/foo/goal" => "/foo", "/goal"; | ||
test_rewriting_path "/foo" => "/foo/goal", "/goal"; | ||
test_rewriting_path "/foo" => "/bar", "/goal?param=999"; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use strict; | ||
use warnings; | ||
use HTTP::Request::Common qw(GET); | ||
use Plack::App::Proxy; | ||
use Plack::App::Proxy::Test; | ||
use Plack::Builder; | ||
use Test::More; | ||
|
||
test_proxy( | ||
proxy => sub { | ||
my ($host, $port) = @_; | ||
return builder { | ||
# We must specify url_map to avoid auto-guess failure. | ||
# Otherwise, ":$port" will be changed even if the redirect URL | ||
# isn't proxied. ("/goal" isn't mapped in this case.) | ||
enable 'Proxy::RewriteLocation', | ||
url_map => ['/foo' => "http://$host:$port/foo"]; | ||
mount "/foo" => Plack::App::Proxy->new( | ||
remote => "http://$host:$port/foo" | ||
); | ||
}; | ||
}, | ||
app => sub { | ||
my $env = shift; | ||
my $no_proxied_url = "http://$env->{HTTP_HOST}/goal"; | ||
return [ | ||
301, | ||
[ | ||
Location => $no_proxied_url, | ||
'X-Original-Location' => $no_proxied_url, | ||
], | ||
['Redirected'] | ||
]; | ||
}, | ||
client => sub { | ||
my $cb = shift; | ||
|
||
my $res = $cb->(GET "http://localhost/foo/"); | ||
is $res->code, 301, 'got right status to redirect'; | ||
is $res->header('Location'), $res->header('X-Original-Location'), | ||
"Don't rewrite outer paths."; | ||
}, | ||
); | ||
|
||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters