Permalink
Browse files

test stderr and stdout of exceptions in routes

  • Loading branch information...
1 parent 2f9065f commit 5fd78f21e64d256d8ce1d78c252641a53a70c122 @ufobat ufobat committed May 27, 2017
Showing with 93 additions and 33 deletions.
  1. +51 −15 lib/Bailador/Test.pm
  2. +13 −4 t/07-OO-errors.t
  3. +14 −4 t/07-errors.t
  4. +15 −10 t/11-error-templates.t
View
@@ -5,34 +5,70 @@ use URI;
unit module Bailador::Test;
-# It would be nice to have IO::String here instead
+my class ErrorBuffer does Stringy {
+ has @.buf;
+ method Str() { @.buf.join: '' }
+ method gist() { self.Str }
+ method wipe() { @.buf.splice(0, @.buf.elems).join: '' }
+ method add(Str:D $data) { @.buf.push($data) }
+}
+
my class IO::Null is IO::Handle {
- multi method print(Str:D \string ) { diag string }
- multi method print(**@args is raw) { diag join '', @args.map: *.Str }
+ has ErrorBuffer $.error-buf;
- method print-nl { diag $.nl-out }
+ multi method print(Str:D \string ) { self!add-string( string ) }
+ multi method print(**@args is raw) { self!add-string( join '', @args.map: *.Str ) }
- multi method put(Str:D \string) { diag string ~ $.nl-out }
+ method print-nl { self.add-string( $.nl-out ) }
+
+ multi method put(Str:D \string) { self!add-string( string ~ $.nl-out ) }
multi method put(**@args is raw) {
- diag join '', @args.map(*.Str), $.nl-out
+ self!add-string( join '', @args.map(*.Str), $.nl-out )
}
- multi method say(Str:D \string) { diag string ~ $.nl-out }
+ multi method say(Str:D \string) { self!add-string( string ~ $.nl-out ) }
multi method say(**@args is raw) {
- diag join '', @args.map(*.gist), $.nl-out
+ self!add-string( join '', @args.map(*.gist), $.nl-out )
}
+
+ method !add-string(Str:D $str) {
+ if $.error-buf {
+ $.error-buf.add($str);
+ } else {
+ diag $str;
+ }
+ }
+}
+
+multi sub run-psgi-request(Bailador::App $app, $meth, $url, $data = '', :$http_cookie = '' ) is export {
+ my $error-buf = ErrorBuffer.new;
+ my $response = get-psgi-response($app, $meth, $url, $data, :$http_cookie, :$error-buf),
+
+ return {
+ err => $error-buf.Str,
+ response => $response;
+ };
+}
+
+multi sub run-psgi-request($meth, $url, $data = '', :$http_cookie = '' ) is export {
+ my $error-buf = ErrorBuffer.new;
+ my $response = get-psgi-response($meth, $url, $data, :$http_cookie, :$error-buf),
+
+ return {
+ err => $error-buf.Str,
+ response => $response;
+ };
}
- # preparing a environment variale for PSGI
-multi sub get-psgi-response(Bailador::App $app, $meth, $url, $data = '', :$http_cookie = "") is export {
- my $env = get-psgi-env($meth, $url, $data, $http_cookie);
+multi sub get-psgi-response(Bailador::App $app, $meth, $url, $data = '', :$http_cookie = "", ErrorBuffer :$error-buf) is export {
+ my $env = get-psgi-env($meth, $url, $data, $http_cookie, $error-buf);
my $psgi-app = $app.get-psgi-app(),
my $promise = $psgi-app.($env);
return de-supply-response $promise.result;
}
-multi sub get-psgi-response($meth, $url, $data = '', :$http_cookie = "") is export {
- my $env = get-psgi-env($meth, $url, $data, $http_cookie);
+multi sub get-psgi-response($meth, $url, $data = '', :$http_cookie = "", ErrorBuffer :$error-buf) is export {
+ my $env = get-psgi-env($meth, $url, $data, $http_cookie, $error-buf);
my $psgi-app = get-psgi-app();
my $promise = $psgi-app.($env);
return de-supply-response $promise.result;
@@ -49,14 +85,14 @@ sub de-supply-response($response) {
die "body must be a Supply";
}
-sub get-psgi-env($meth, $url, $data, $http_cookie,) {
+sub get-psgi-env($meth, $url, $data, $http_cookie, ErrorBuffer $error-buf) {
# prefix with http://127.0.0.1:1234 because the URI module cannot handle URI that looks like /foo
my $uri = URI.new(($url.substr(0, 1) eq '/' ?? 'http://127.0.0.1:1234' !! '') ~ $url);
my $env = {
"p6w.multiprocess" => Bool::False,
"p6w.multithread" => Bool::False,
- "p6w.errors" => IO::Null.new,
+ "p6w.errors" => IO::Null.new( :$error-buf ),
"p6w.streaming" => Bool::False,
"p6w.nonblocking" => Bool::False,
"p6w.version" => [1, 0],
View
@@ -2,7 +2,7 @@ use Test;
use Bailador::App;
use Bailador::Test;
-plan 3;
+plan 6;
class MyOwnWebApp is Bailador::App {
submethod BUILD(|) {
@@ -14,7 +14,16 @@ class MyOwnWebApp is Bailador::App {
}
my $app = MyOwnWebApp.new;
+my %data;
-is-deeply get-psgi-response($app, 'GET', '/die'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles die';
-is-deeply get-psgi-response($app, 'GET', '/fail'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles fail';
-is-deeply get-psgi-response($app, 'GET', '/exception'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles thrown exception';
+%data = run-psgi-request($app, 'GET', '/die');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles die';
+like %data<err>, rx:s/oh no\!/, 'stderr';
+
+%data = run-psgi-request($app, 'GET', '/fail');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles fail';
+like %data<err>, rx:s/oh no\!/, 'stderr';
+
+%data = run-psgi-request($app, 'GET', '/exception');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles thrown exception';
+like %data<err>, rx:s/NYI not yet implemented\. Sorry\./, 'stderr';
View
@@ -2,12 +2,22 @@ use Test;
use Bailador;
use Bailador::Test;
-plan 3;
+plan 6;
get '/die' => sub { die "oh no!" }
get '/fail' => sub { fail "oh no!" }
get '/exception' => sub { X::NYI.new(feature => 'NYI').throw }
-is-deeply get-psgi-response('GET', '/die'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles die';
-is-deeply get-psgi-response('GET', '/fail'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles fail';
-is-deeply get-psgi-response('GET', '/exception'), [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles thrown exception';
+my %data;
+
+%data = run-psgi-request('GET', '/die');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles die';
+like %data<err>, rx:s/oh no\!/, 'stderr';
+
+%data = run-psgi-request('GET', '/fail');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles fail';
+like %data<err>, rx:s/oh no\!/, 'stderr';
+
+%data = run-psgi-request('GET', '/exception');
+is-deeply %data<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error'], 'route GET handles thrown exception';
+like %data<err>, rx:s/NYI not yet implemented\. Sorry\./, 'stderr';
View
@@ -3,36 +3,41 @@ use Bailador;
Bailador::import;
use Bailador::Test;
-plan 8;
+plan 12;
my $not-found-template will leave { .unlink } = $?FILE.IO.parent.child('views').child('404.xx');
my $error-template will leave { .unlink } = $?FILE.IO.parent.child('views').child('500.xx');
-my $resp;
+my %resp;
get '/die' => sub { die 'something' };
# 404
ok not $not-found-template.e , 'there is no 404 template';
-$resp = get-psgi-response('GET', '/notexisting');
-is-deeply $resp,[404, ["Content-Type" => "text/html, charset=utf-8"], 'Not found' ], '404 - without error template';
+%resp = run-psgi-request('GET', '/notexisting');
+is-deeply %resp<response>, [404, ["Content-Type" => "text/html, charset=utf-8"], 'Not found' ], '404 - without error template';
+is %resp<err>, '', 'no stderr';
$not-found-template.spurt: 'nada';
ok $not-found-template.e , 'there is a 404 template';
-$resp = get-psgi-response('GET', '/notexisting');
-is-deeply $resp, [404, ["Content-Type" => "text/html, charset=utf-8"], 'nada' ], '404 - with error template';
+%resp = run-psgi-request('GET', '/notexisting');
+is-deeply %resp<response>, [404, ["Content-Type" => "text/html, charset=utf-8"], 'nada' ], '404 - with error template';
+is %resp<err>, '', 'no stderr';
+#like %resp<err>, rx:s//, 'stderr';
# 500
ok not $error-template.e, 'there is no 500 template';
-$resp = get-psgi-response('GET', '/die');
+%resp = run-psgi-request('GET', '/die');
-is-deeply $resp, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error' ], '500 - without error template';
+is-deeply %resp<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'Internal Server Error' ], '500 - without error template';
+like %resp<err>, rx:s/something/, 'stderr';
$error-template.spurt: 'muerte';
ok $error-template.e, 'there is a 500 template';
-$resp = get-psgi-response('GET', '/die');
-is-deeply $resp, [500, ["Content-Type" => "text/html, charset=utf-8"], 'muerte' ], '500 - with error template';
+%resp = run-psgi-request('GET', '/die');
+is-deeply %resp<response>, [500, ["Content-Type" => "text/html, charset=utf-8"], 'muerte' ], '500 - with error template';
+like %resp<err>, rx:s/something/, 'stderr';

0 comments on commit 5fd78f2

Please sign in to comment.