### ----------------------------------------------------
### -- Bailador
### -- Licenses: MIT
### -- Authors: Tadeusz “tadzik” Sośnierz, Anthony Parsons, Carl Masak, Gabor Szabo, GlitchMr, Moritz Lenz, Panu Ervamaa, Paul Cochrane, Solomon Foster, Sterling Hanenkamp, Tobias Leich, ugexe, vendethiel, Martin Barth (ufobat), Emeric Fischer, Julien Simonet (jsimonet)
### -- File: t/07-errors.t
### ----------------------------------------------------
use v6.c;

use Test;

use Bailador;
use Bailador::Test;

plan 6;

get '/die' => sub { die "oh no!" }
get '/fail' => sub { fail "hell froze!" }
get '/exception' => sub { X::NYI.new(feature => 'NYI').throw }

# Call baile just once
my $p6w-app = baile('p6w');

subtest {
    plan 2;
    my %data = run-psgi-request($p6w-app, 'GET', '/die');
    is-deeply %data<response>, [500, ["Content-Type" => "text/plain;charset=UTF-8"], 'Internal Server Error'], 'route GET handles die';
    like %data<err>, rx:s/oh no\!/, 'stderr';
};

subtest {
    plan 2;
    my %data = run-psgi-request($p6w-app, 'GET', '/fail');
    is-deeply %data<response>, [500, ["Content-Type" => "text/plain;charset=UTF-8"], 'Internal Server Error'], 'route GET handles fail';
    like %data<err>, rx:s/hell froze\!/, 'stderr';
};

subtest {
    plan 2;
    my %data = run-psgi-request($p6w-app, 'GET', '/exception');
    is-deeply %data<response>, [500, ["Content-Type" => "text/plain;charset=UTF-8"], 'Internal Server Error'], 'route GET handles thrown exception';
    like %data<err>, rx:s/NYI not yet implemented\. Sorry\./, 'stderr';
};

subtest {
    plan 3;
    config.mode = 'development';
    my %data = run-psgi-request($p6w-app, 'GET', '/die');
    my $html = %data<response>[2];
    like $html, rx:s/In the future this will be a nice error page\. For now we try to make it at least informative\./;
    %data<response>[2] = '';
    is-deeply %data<response>, [500, ["Content-Type" => "text/html;charset=UTF-8"], ''], 'route GET handles die';
    like %data<err>, rx:s/oh no\!/, 'stderr';
}

subtest {
    plan 3;
    config.mode = 'development';
    my %data = run-psgi-request($p6w-app, 'GET', '/fail');
    my $html = %data<response>[2];
    like $html, rx:s/In the future this will be a nice error page\. For now we try to make it at least informative\./;
    %data<response>[2] = '';
    is-deeply %data<response>, [500, ["Content-Type" => "text/html;charset=UTF-8"], ''], 'route GET handles die';
    like %data<err>, rx:s/hell froze\!/, 'stderr';
}

subtest {
    plan 3;
    config.mode = 'development';
    my %data = run-psgi-request($p6w-app, 'GET', '/exception');
    my $html = %data<response>[2];
    like $html, rx:s/In the future this will be a nice error page\. For now we try to make it at least informative\./;
    %data<response>[2] = '';
    is-deeply %data<response>, [500, ["Content-Type" => "text/html;charset=UTF-8"], ''], 'route GET handles die';
    like %data<err>, rx:s/NYI not yet implemented\. Sorry\./, 'stderr';
}


# vim: expandtab
# vim: tabstop=4


