PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/t/error.t

https://github.com/mickeyn/Dancer2
Unknown | 133 lines | 109 code | 24 blank | 0 comment | 0 complexity | 09632110b7eb6852ea3c286cbd107cc5 MD5 | raw file
  1. use strict;
  2. use warnings;
  3. use Test::More import => ['!pass'];
  4. use Plack::Test;
  5. use HTTP::Request::Common;
  6. use Dancer2::Core::App;
  7. use Dancer2::Core::Context;
  8. use Dancer2::Core::Response;
  9. use Dancer2::Core::Request;
  10. use Dancer2::Core::Error;
  11. my $env = {
  12. 'psgi.url_scheme' => 'http',
  13. REQUEST_METHOD => 'GET',
  14. SCRIPT_NAME => '/foo',
  15. PATH_INFO => '/bar/baz',
  16. REQUEST_URI => '/foo/bar/baz',
  17. QUERY_STRING => 'foo=42&bar=12&bar=13&bar=14',
  18. SERVER_NAME => 'localhost',
  19. SERVER_PORT => 5000,
  20. SERVER_PROTOCOL => 'HTTP/1.1',
  21. REMOTE_ADDR => '127.0.0.1',
  22. HTTP_COOKIE =>
  23. 'dancer.session=1234; fbs_102="access_token=xxxxxxxxxx%7Cffffff"',
  24. X_FORWARDED_FOR => '127.0.0.2',
  25. REMOTE_HOST => 'localhost',
  26. HTTP_USER_AGENT => 'Mozilla',
  27. REMOTE_USER => 'sukria',
  28. };
  29. my $a = Dancer2::Core::App->new( name => 'main' );
  30. my $c = Dancer2::Core::Context->new( env => $env, app => $a );
  31. subtest 'basic defaults of Error object' => sub {
  32. my $e = Dancer2::Core::Error->new( context => $c, );
  33. is $e->status, 500, 'code';
  34. is $e->title, 'Error 500 - Internal Server Error', 'title';
  35. is $e->message, undef, 'message';
  36. like $e->content, qr!http://localhost:5000/foo/css!,
  37. "error content contains css path relative to uri_base";
  38. };
  39. subtest "send_error in route" => sub {
  40. {
  41. package App;
  42. use Dancer2;
  43. set serializer => 'JSON';
  44. get '/error' => sub {
  45. send_error "This is a custom error message";
  46. };
  47. }
  48. my $app = Dancer2->runner->server->psgi_app;
  49. is( ref $app, 'CODE', 'Got app' );
  50. test_psgi $app, sub {
  51. my $cb = shift;
  52. my $r = $cb->( GET '/error' );
  53. is( $r->code, 500, 'send_error sets the status to 500' );
  54. like(
  55. $r->content,
  56. qr{This is a custom error message},
  57. 'Error message looks good',
  58. );
  59. is(
  60. $r->content_type,
  61. 'application/json',
  62. 'Response has appropriate content type after serialization',
  63. );
  64. };
  65. };
  66. subtest "send_error with custom stuff" => sub {
  67. {
  68. package App;
  69. use Dancer2;
  70. get '/error/:x' => sub {
  71. my $x = param('x');
  72. send_error "Error $x", "5$x";
  73. };
  74. }
  75. my $app = Dancer2->runner->server->psgi_app;
  76. is( ref $app, 'CODE', 'Got app' );
  77. test_psgi $app, sub {
  78. my $cb = shift;
  79. my $r = $cb->( GET '/error/42' );
  80. is( $r->code, 542, 'send_error sets the status to 542' );
  81. like( $r->content, qr{Error 42}, 'Error message looks good' );
  82. };
  83. };
  84. subtest 'Response->error()' => sub {
  85. my $resp = Dancer2::Core::Response->new;
  86. isa_ok $resp->error( message => 'oops', status => 418 ),
  87. 'Dancer2::Core::Error';
  88. is $resp->status => 418, 'response code is 418';
  89. like $resp->content => qr/oops/, 'response content overriden by error';
  90. like $resp->content => qr/teapot/, 'error code title is present';
  91. ok $resp->is_halted, 'response is halted';
  92. };
  93. subtest 'Error with show_errors: 0' => sub {
  94. my $err = Dancer2::Core::Error->new(
  95. context => $c,
  96. exception => 'our exception',
  97. show_errors => 0
  98. )->throw;
  99. unlike $err->content => qr/our exception/;
  100. };
  101. subtest 'Error with show_errors: 1' => sub {
  102. my $err = Dancer2::Core::Error->new(
  103. context => $c,
  104. exception => 'our exception',
  105. show_errors => 1
  106. )->throw;
  107. like $err->content => qr/our exception/;
  108. };
  109. done_testing;