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