PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/t/error.t

https://github.com/chinmayadas/Dancer2
Unknown | 134 lines | 110 code | 24 blank | 0 comment | 0 complexity | a542e77f18d3ba12b559d54dca843996 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. HTTP_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. return "send_error returns so this content is not processed";
  47. };
  48. }
  49. my $app = Dancer2->runner->psgi_app;
  50. is( ref $app, 'CODE', 'Got app' );
  51. test_psgi $app, sub {
  52. my $cb = shift;
  53. my $r = $cb->( GET '/error' );
  54. is( $r->code, 500, 'send_error sets the status to 500' );
  55. like(
  56. $r->content,
  57. qr{This is a custom error message},
  58. 'Error message looks good',
  59. );
  60. is(
  61. $r->content_type,
  62. 'application/json',
  63. 'Response has appropriate content type after serialization',
  64. );
  65. };
  66. };
  67. subtest "send_error with custom stuff" => sub {
  68. {
  69. package App;
  70. use Dancer2;
  71. get '/error/:x' => sub {
  72. my $x = param('x');
  73. send_error "Error $x", "5$x";
  74. };
  75. }
  76. my $app = Dancer2->runner->psgi_app;
  77. is( ref $app, 'CODE', 'Got app' );
  78. test_psgi $app, sub {
  79. my $cb = shift;
  80. my $r = $cb->( GET '/error/42' );
  81. is( $r->code, 542, 'send_error sets the status to 542' );
  82. like( $r->content, qr{Error 42}, 'Error message looks good' );
  83. };
  84. };
  85. subtest 'Response->error()' => sub {
  86. my $resp = Dancer2::Core::Response->new;
  87. isa_ok $resp->error( message => 'oops', status => 418 ),
  88. 'Dancer2::Core::Error';
  89. is $resp->status => 418, 'response code is 418';
  90. like $resp->content => qr/oops/, 'response content overriden by error';
  91. like $resp->content => qr/teapot/, 'error code title is present';
  92. ok $resp->is_halted, 'response is halted';
  93. };
  94. subtest 'Error with show_errors: 0' => sub {
  95. my $err = Dancer2::Core::Error->new(
  96. context => $c,
  97. exception => 'our exception',
  98. show_errors => 0
  99. )->throw;
  100. unlike $err->content => qr/our exception/;
  101. };
  102. subtest 'Error with show_errors: 1' => sub {
  103. my $err = Dancer2::Core::Error->new(
  104. context => $c,
  105. exception => 'our exception',
  106. show_errors => 1
  107. )->throw;
  108. like $err->content => qr/our exception/;
  109. };
  110. done_testing;