| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More import => ['!pass']; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
use Ref::Util qw<is_coderef>; |
| 7 |
|
| 8 |
use Dancer2; |
| 9 |
|
| 10 |
set behind_proxy => 1; |
| 11 |
|
| 12 |
get '/' => sub { |
| 13 |
'home:' . join( ',', params ); |
| 14 |
}; |
| 15 |
get '/bounce/' => sub { |
| 16 |
return forward '/'; |
| 17 |
}; |
| 18 |
get '/bounce/:withparams/' => sub { |
| 19 |
return forward '/'; |
| 20 |
}; |
| 21 |
get '/bounce2/adding_params/' => sub { |
| 22 |
return forward '/', { withparams => 'foo' }; |
| 23 |
}; |
| 24 |
post '/simple_post_route/' => sub { |
| 25 |
'post:' . join( ',', params ); |
| 26 |
}; |
| 27 |
get '/go_to_post/' => sub { |
| 28 |
return forward '/simple_post_route/', { foo => 'bar' }, |
| 29 |
{ method => 'post' }; |
| 30 |
}; |
| 31 |
get '/proxy/' => sub { |
| 32 |
return uri_for('/'); |
| 33 |
}; |
| 34 |
get '/forward_with_proxy/' => sub { |
| 35 |
forward '/proxy/'; |
| 36 |
}; |
| 37 |
|
| 38 |
# NOT SUPPORTED IN DANCER2 |
| 39 |
# In dancer2, vars are alive for only one request flow, a forward initiate a |
| 40 |
# new request flow, then the vars HashRef is destroyed. |
| 41 |
# |
| 42 |
# get '/b' => sub { vars->{test} = 1; forward '/a'; }; |
| 43 |
# get '/a' => sub { return "test is " . var('test'); }; |
| 44 |
|
| 45 |
my $app = __PACKAGE__->to_app; |
| 46 |
ok( is_coderef($app), 'Got app' ); |
| 47 |
|
| 48 |
test_psgi $app, sub { |
| 49 |
my $cb = shift; |
| 50 |
is( $cb->( GET '/' )->code, 200, '[GET /] Correct code' ); |
| 51 |
is( $cb->( GET '/' )->content, 'home:', '[GET /] Correct content' ); |
| 52 |
|
| 53 |
is( $cb->( GET '/bounce/' )->code, 200, '[GET /bounce] Correct code' ); |
| 54 |
is( |
| 55 |
$cb->( GET '/bounce/' )->content, |
| 56 |
'home:', |
| 57 |
'[GET /bounce] Correct content', |
| 58 |
); |
| 59 |
|
| 60 |
is( |
| 61 |
$cb->( GET '/bounce/thesethings/' )->code, |
| 62 |
200, |
| 63 |
'[GET /bounce/thesethings/] Correct code', |
| 64 |
); |
| 65 |
|
| 66 |
is( |
| 67 |
$cb->( GET '/bounce/thesethings/' )->content, |
| 68 |
'home:withparams,thesethings', |
| 69 |
'[GET /bounce/thesethings/] Correct content', |
| 70 |
); |
| 71 |
|
| 72 |
is( |
| 73 |
$cb->( GET '/bounce2/adding_params/' )->code, |
| 74 |
200, |
| 75 |
'[GET /bounce2/adding_params/] Correct code', |
| 76 |
); |
| 77 |
|
| 78 |
is( |
| 79 |
$cb->( GET '/bounce2/adding_params/' )->content, |
| 80 |
'home:withparams,foo', |
| 81 |
'[GET /bounce2/adding_params/] Correct content', |
| 82 |
); |
| 83 |
|
| 84 |
is( |
| 85 |
$cb->( GET '/go_to_post/' )->code, |
| 86 |
200, |
| 87 |
'[GET /go_to_post/] Correct code', |
| 88 |
); |
| 89 |
|
| 90 |
is( |
| 91 |
$cb->( GET '/go_to_post/' )->content, |
| 92 |
'post:foo,bar', |
| 93 |
'[GET /go_to_post/] Correct content', |
| 94 |
); |
| 95 |
|
| 96 |
# NOT SUPPORTED |
| 97 |
# response_status_is [ GET => '/b' ] => 200; |
| 98 |
# response_content_is [ GET => '/b' ] => 'test is 1'; |
| 99 |
|
| 100 |
{ |
| 101 |
my $res = $cb->( GET '/bounce/' ); |
| 102 |
|
| 103 |
is( |
| 104 |
$res->headers->content_length, |
| 105 |
5, |
| 106 |
'[GET /bounce/] Correct content length', |
| 107 |
); |
| 108 |
|
| 109 |
is( |
| 110 |
$res->headers->content_type, |
| 111 |
'text/html', |
| 112 |
'[GET /bounce/] Correct content type', |
| 113 |
); |
| 114 |
|
| 115 |
is( |
| 116 |
$res->headers->content_type_charset, |
| 117 |
'UTF-8', |
| 118 |
'[GET /bounce/] Default content type charset', |
| 119 |
); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
# checking post |
| 124 |
post '/' => sub {'post-home'}; |
| 125 |
post '/bounce/' => sub { forward('/') }; |
| 126 |
|
| 127 |
is( $cb->( POST '/' )->code, 200, '[POST /] Correct code' ); |
| 128 |
is( $cb->( POST '/' )->content, 'post-home', '[POST /] Correct content' ); |
| 129 |
|
| 130 |
is( |
| 131 |
$cb->( POST '/bounce/' )->code, |
| 132 |
200, |
| 133 |
'[POST /bounce/] Correct code', |
| 134 |
); |
| 135 |
|
| 136 |
is( |
| 137 |
$cb->( POST '/bounce/' )->content, |
| 138 |
'post-home', |
| 139 |
'[POST /bounce/] Correct content', |
| 140 |
); |
| 141 |
|
| 142 |
{ |
| 143 |
my $res = $cb->( POST '/bounce/' ); |
| 144 |
|
| 145 |
is( |
| 146 |
$res->headers->content_length, |
| 147 |
9, |
| 148 |
'[POST /bounce/] Correct content length', |
| 149 |
); |
| 150 |
|
| 151 |
is( |
| 152 |
$res->headers->content_type, |
| 153 |
'text/html', |
| 154 |
'[POST /bounce/] Correct content type', |
| 155 |
); |
| 156 |
|
| 157 |
is( |
| 158 |
$res->headers->content_type_charset, |
| 159 |
'UTF-8', |
| 160 |
'[POST /bounce/] Default content type charset', |
| 161 |
); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
is( |
| 166 |
$cb->( GET '/forward_with_proxy/', 'X-Forwarded-Proto' => 'https' )->content, |
| 167 |
'https://localhost/', |
| 168 |
'[GET /forward_with_proxy/] maintained is_behind_proxy', |
| 169 |
); |
| 170 |
}; |
| 171 |
|
| 172 |
done_testing; |