PageRenderTime 60ms CodeModel.GetById 11ms app.highlight 46ms RepoModel.GetById 1ms 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
  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    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        };
 55    }
 56
 57    my $app = Dancer2->runner->server->psgi_app;
 58    is( ref $app, 'CODE', 'Got app' );
 59
 60    test_psgi $app, sub {
 61        my $cb = shift;
 62        my $r  = $cb->( GET '/error' );
 63
 64        is( $r->code, 500, 'send_error sets the status to 500' );
 65        like(
 66            $r->content,
 67            qr{This is a custom error message},
 68            'Error message looks good',
 69        );
 70
 71        is(
 72            $r->content_type,
 73            'application/json',
 74            'Response has appropriate content type after serialization',
 75        );
 76    };
 77};
 78
 79subtest "send_error with custom stuff" => sub {
 80    {
 81
 82        package App;
 83        use Dancer2;
 84
 85        get '/error/:x' => sub {
 86            my $x = param('x');
 87            send_error "Error $x", "5$x";
 88        };
 89    }
 90
 91    my $app = Dancer2->runner->server->psgi_app;
 92    is( ref $app, 'CODE', 'Got app' );
 93
 94    test_psgi $app, sub {
 95        my $cb = shift;
 96        my $r  = $cb->( GET '/error/42' );
 97
 98        is( $r->code, 542, 'send_error sets the status to 542' );
 99        like( $r->content, qr{Error 42},  'Error message looks good' );
100    };
101};
102
103subtest 'Response->error()' => sub {
104    my $resp = Dancer2::Core::Response->new;
105
106    isa_ok $resp->error( message => 'oops', status => 418 ),
107      'Dancer2::Core::Error';
108
109    is $resp->status    => 418,        'response code is 418';
110    like $resp->content => qr/oops/,   'response content overriden by error';
111    like $resp->content => qr/teapot/, 'error code title is present';
112    ok $resp->is_halted, 'response is halted';
113};
114
115subtest 'Error with show_errors: 0' => sub {
116    my $err = Dancer2::Core::Error->new(
117        context     => $c,
118        exception   => 'our exception',
119        show_errors => 0
120    )->throw;
121    unlike $err->content => qr/our exception/;
122};
123
124subtest 'Error with show_errors: 1' => sub {
125    my $err = Dancer2::Core::Error->new(
126        context     => $c,
127        exception   => 'our exception',
128        show_errors => 1
129    )->throw;
130    like $err->content => qr/our exception/;
131};
132
133done_testing;