| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use lib 't/lib'; |
| 5 |
|
| 6 |
use Test::More import => ['!pass']; |
| 7 |
use Plack::Test; |
| 8 |
use HTTP::Request::Common; |
| 9 |
use Ref::Util qw<is_coderef>; |
| 10 |
use List::Util qw<all>; |
| 11 |
use Module::Runtime qw/ require_module /; |
| 12 |
|
| 13 |
use Dancer2::Core::App; |
| 14 |
use Dancer2::Core::Response; |
| 15 |
use Dancer2::Core::Request; |
| 16 |
use Dancer2::Core::Error; |
| 17 |
|
| 18 |
use JSON::MaybeXS qw/JSON/; # Error serialization |
| 19 |
|
| 20 |
my $env = { |
| 21 |
'psgi.url_scheme' => 'http', |
| 22 |
REQUEST_METHOD => 'GET', |
| 23 |
SCRIPT_NAME => '/foo', |
| 24 |
PATH_INFO => '/bar/baz', |
| 25 |
REQUEST_URI => '/foo/bar/baz', |
| 26 |
QUERY_STRING => 'foo=42&bar=12&bar=13&bar=14', |
| 27 |
SERVER_NAME => 'localhost', |
| 28 |
SERVER_PORT => 5000, |
| 29 |
SERVER_PROTOCOL => 'HTTP/1.1', |
| 30 |
REMOTE_ADDR => '127.0.0.1', |
| 31 |
HTTP_COOKIE => |
| 32 |
'dancer.session=1234; fbs_102="access_token=xxxxxxxxxx%7Cffffff"', |
| 33 |
HTTP_X_FORWARDED_FOR => '127.0.0.2', |
| 34 |
REMOTE_HOST => 'localhost', |
| 35 |
HTTP_USER_AGENT => 'Mozilla', |
| 36 |
REMOTE_USER => 'sukria', |
| 37 |
}; |
| 38 |
|
| 39 |
my $app = Dancer2::Core::App->new( name => 'main' ); |
| 40 |
my $request = $app->build_request($env); |
| 41 |
|
| 42 |
$app->set_request($request); |
| 43 |
|
| 44 |
|
| 45 |
subtest 'basic defaults of Error object' => sub { |
| 46 |
my $err = Dancer2::Core::Error->new( app => $app ); |
| 47 |
is $err->status, 500, 'code'; |
| 48 |
is $err->title, 'Error 500 - Internal Server Error', 'title'; |
| 49 |
is $err->message, '', 'message'; |
| 50 |
like $err->content, qr!http://localhost:5000/foo/css!, |
| 51 |
"error content contains css path relative to uri_base"; |
| 52 |
}; |
| 53 |
|
| 54 |
subtest "send_error in route" => sub { |
| 55 |
{ |
| 56 |
|
| 57 |
package App; |
| 58 |
use Dancer2; |
| 59 |
|
| 60 |
set serializer => 'JSON'; |
| 61 |
|
| 62 |
get '/error' => sub { |
| 63 |
send_error "This is a custom error message"; |
| 64 |
return "send_error returns so this content is not processed"; |
| 65 |
}; |
| 66 |
} |
| 67 |
|
| 68 |
my $app = App->to_app; |
| 69 |
ok( is_coderef($app), 'Got app' ); |
| 70 |
|
| 71 |
test_psgi $app, sub { |
| 72 |
my $cb = shift; |
| 73 |
my $r = $cb->( GET '/error' ); |
| 74 |
|
| 75 |
is( $r->code, 500, 'send_error sets the status to 500' ); |
| 76 |
like( |
| 77 |
$r->content, |
| 78 |
qr{This is a custom error message}, |
| 79 |
'Error message looks good', |
| 80 |
); |
| 81 |
|
| 82 |
is( |
| 83 |
$r->content_type, |
| 84 |
'application/json', |
| 85 |
'Response has appropriate content type after serialization', |
| 86 |
); |
| 87 |
}; |
| 88 |
}; |
| 89 |
|
| 90 |
subtest "send_error with custom stuff" => sub { |
| 91 |
{ |
| 92 |
|
| 93 |
package App; |
| 94 |
use Dancer2; |
| 95 |
|
| 96 |
get '/error/:x' => sub { |
| 97 |
my $x = param('x'); |
| 98 |
send_error "Error $x", "5$x"; |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
my $app = App->to_app; |
| 103 |
ok( is_coderef($app), 'Got app' ); |
| 104 |
|
| 105 |
test_psgi $app, sub { |
| 106 |
my $cb = shift; |
| 107 |
my $r = $cb->( GET '/error/42' ); |
| 108 |
|
| 109 |
is( $r->code, 542, 'send_error sets the status to 542' ); |
| 110 |
like( $r->content, qr{Error 42}, 'Error message looks good' ); |
| 111 |
}; |
| 112 |
}; |
| 113 |
|
| 114 |
subtest 'Response->error()' => sub { |
| 115 |
my $resp = Dancer2::Core::Response->new; |
| 116 |
|
| 117 |
isa_ok $resp->error( message => 'oops', status => 418 ), |
| 118 |
'Dancer2::Core::Error'; |
| 119 |
|
| 120 |
is $resp->status => 418, 'response code is 418'; |
| 121 |
like $resp->content => qr/oops/, 'response content overriden by error'; |
| 122 |
like $resp->content => qr/teapot/, 'error code title is present'; |
| 123 |
ok $resp->is_halted, 'response is halted'; |
| 124 |
}; |
| 125 |
|
| 126 |
subtest 'Throwing an error with a response' => sub { |
| 127 |
my $resp = Dancer2::Core::Response->new; |
| 128 |
|
| 129 |
my $err = eval { Dancer2::Core::Error->new( |
| 130 |
exception => 'our exception', |
| 131 |
show_stacktrace => 1 |
| 132 |
)->throw($resp) }; |
| 133 |
|
| 134 |
isa_ok($err, 'Dancer2::Core::Response', "Error->throw() accepts a response"); |
| 135 |
}; |
| 136 |
|
| 137 |
subtest 'Error with show_stacktrace: 0' => sub { |
| 138 |
my $err = Dancer2::Core::Error->new( |
| 139 |
exception => 'our exception', |
| 140 |
show_stacktrace => 0 |
| 141 |
)->throw; |
| 142 |
unlike $err->content => qr/our exception/; |
| 143 |
}; |
| 144 |
|
| 145 |
subtest 'Error with show_stacktrace: 1' => sub { |
| 146 |
my $err = Dancer2::Core::Error->new( |
| 147 |
exception => 'our exception', |
| 148 |
show_stacktrace => 1 |
| 149 |
)->throw; |
| 150 |
like $err->content => qr/our exception/; |
| 151 |
}; |
| 152 |
|
| 153 |
subtest 'App dies with serialized error' => sub { |
| 154 |
{ |
| 155 |
package AppDies; |
| 156 |
use Dancer2; |
| 157 |
set serializer => 'JSON'; |
| 158 |
|
| 159 |
get '/die' => sub { |
| 160 |
die "oh no\n"; # I should serialize |
| 161 |
}; |
| 162 |
} |
| 163 |
|
| 164 |
my $app = AppDies->to_app; |
| 165 |
isa_ok( $app, 'CODE', 'Got app' ); |
| 166 |
|
| 167 |
test_psgi $app, sub { |
| 168 |
my $cb = shift; |
| 169 |
my $r = $cb->( GET '/die' ); |
| 170 |
|
| 171 |
is( $r->code, 500, '/die returns 500' ); |
| 172 |
|
| 173 |
my $out = eval { JSON->new->utf8(0)->decode($r->decoded_content) }; |
| 174 |
ok(!$@, 'JSON decoding serializer error produces no errors'); |
| 175 |
isa_ok($out, 'HASH', 'Error deserializes to a hash'); |
| 176 |
like($out->{exception}, qr/^oh no/, 'Get expected error message'); |
| 177 |
}; |
| 178 |
}; |
| 179 |
|
| 180 |
subtest 'Error with exception object' => sub { |
| 181 |
local $@; |
| 182 |
eval { MyTestException->throw('a test exception object') }; |
| 183 |
my $exception = $@; |
| 184 |
my $err = Dancer2::Core::Error->new( |
| 185 |
app => Dancer2::Core::App->new(), |
| 186 |
exception => $exception, |
| 187 |
show_stacktrace => 1, |
| 188 |
)->throw; |
| 189 |
|
| 190 |
like $err->content, qr/a test exception object/, 'Error content contains exception message'; |
| 191 |
}; |
| 192 |
|
| 193 |
subtest 'Errors without server tokens' => sub { |
| 194 |
{ |
| 195 |
package AppNoServerTokens; |
| 196 |
use Dancer2; |
| 197 |
set serializer => 'JSON'; |
| 198 |
set no_server_tokens => 1; |
| 199 |
|
| 200 |
get '/ohno' => sub { |
| 201 |
die "oh no"; |
| 202 |
}; |
| 203 |
} |
| 204 |
|
| 205 |
my $test = Plack::Test->create( AppNoServerTokens->to_app ); |
| 206 |
my $r = $test->request( GET '/ohno' ); |
| 207 |
is( $r->code, 500, "/ohno returned 500 response"); |
| 208 |
is( $r->header('server'), undef, "No server header when no_server_tokens => 1" ); |
| 209 |
}; |
| 210 |
|
| 211 |
subtest 'Errors with show_stacktrace and circular references' => sub { |
| 212 |
{ |
| 213 |
package App::ShowErrorsCircRef; |
| 214 |
use Dancer2; |
| 215 |
set show_stacktrace => 1; |
| 216 |
set something_with_config => {something => config}; |
| 217 |
set password => '===VERY-UNIQUE-STRING==='; |
| 218 |
set innocent_thing => '===VERY-INNOCENT-STRING==='; |
| 219 |
set template => 'tiny'; |
| 220 |
|
| 221 |
# Trigger an error that makes Dancer2::Core::Error::_censor enter an |
| 222 |
# infinite loop |
| 223 |
get '/ohno' => sub { |
| 224 |
template q{I don't exist}; |
| 225 |
}; |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
my $test = Plack::Test->create( App::ShowErrorsCircRef->to_app ); |
| 230 |
my $r = $test->request( GET '/ohno' ); |
| 231 |
is( $r->code, 500, "/ohno returned 500 response"); |
| 232 |
like( $r->content, qr{Stack}, 'it includes a stack trace' ); |
| 233 |
|
| 234 |
my @password_values = ($r->content =~ /\bpassword\b(.+)\n/g); |
| 235 |
my $is_password_hidden = |
| 236 |
all { /Hidden \(looks potentially sensitive\)/ } @password_values; |
| 237 |
|
| 238 |
ok($is_password_hidden, "password was hidden in stacktrace"); |
| 239 |
|
| 240 |
cmp_ok(@password_values, '>', 1, |
| 241 |
'password key appears more than once in the stacktrace'); |
| 242 |
|
| 243 |
unlike($r->content, qr{===VERY-UNIQUE-STRING===}, |
| 244 |
'password value does not appear in the stacktrace'); |
| 245 |
|
| 246 |
like($r->content, qr{===VERY-INNOCENT-STRING===}, |
| 247 |
'Values for other keys (non-sensitive) appear in the stacktrace'); |
| 248 |
}; |
| 249 |
|
| 250 |
subtest censor => sub { |
| 251 |
sub MyApp::Censor::censor { $_[0]->{hush} = 'NOT TELLING'; return 1; } |
| 252 |
|
| 253 |
my $app = Dancer2::Core::App->new( name => 'main' ); |
| 254 |
|
| 255 |
$app->setting( password => 'potato' ); # oh my, we're leaking a password |
| 256 |
|
| 257 |
subtest 'core censor()' => sub { |
| 258 |
my $error = Dancer2::Core::Error->new( app => $app ); |
| 259 |
|
| 260 |
unlike $error->environment => qr/potato/, 'the password is censored'; |
| 261 |
like $error->environment => qr/^.*password.*Hidden.*$/m, 'we say it is hidden'; |
| 262 |
}; |
| 263 |
|
| 264 |
subtest 'custom censor' => sub { |
| 265 |
|
| 266 |
subtest 'via function string' => sub { |
| 267 |
my $app = Dancer2::Core::App->new( name => 'main' ); |
| 268 |
my $error = Dancer2::Core::Error->new( app => $app ); |
| 269 |
|
| 270 |
$app->setting( hush => 'potato' ); |
| 271 |
|
| 272 |
$app->setting( error_censor => 'MyApp::Censor::censor' ); |
| 273 |
|
| 274 |
unlike $error->environment => qr/potato/, 'the password is censored'; |
| 275 |
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden'; |
| 276 |
}; |
| 277 |
|
| 278 |
subtest 'via class hashref' => sub { |
| 279 |
my $app = Dancer2::Core::App->new( name => 'main' ); |
| 280 |
$app->setting( 'error_censor' => { |
| 281 |
'Data::Censor' => { |
| 282 |
sensitive_fields => ['hush'], |
| 283 |
replacement => 'NOT TELLING', |
| 284 |
} |
| 285 |
}); |
| 286 |
|
| 287 |
my $error = Dancer2::Core::Error->new( app => $app ); |
| 288 |
|
| 289 |
$app->setting( hush => 'potato' ); |
| 290 |
|
| 291 |
unlike $error->environment => qr/potato/, 'the password is censored'; |
| 292 |
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden'; |
| 293 |
}; |
| 294 |
|
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
done_testing; |
| 300 |
|
| 301 |
{ # Simple test exception class |
| 302 |
package MyTestException; |
| 303 |
|
| 304 |
use overload '""' => \&as_str; |
| 305 |
|
| 306 |
sub new { |
| 307 |
return bless {}; |
| 308 |
} |
| 309 |
|
| 310 |
sub throw { |
| 311 |
my ( $class, $error ) = @_; |
| 312 |
my $self = ref($class) ? $class : $class->new; |
| 313 |
$self->{error} = $error; |
| 314 |
|
| 315 |
die $self; |
| 316 |
} |
| 317 |
|
| 318 |
sub as_str { return $_[0]->{error} } |
| 319 |
} |