Skip to content
Browse files

#266 - Add tests

  • Loading branch information...
1 parent e94c493 commit 894a3efd4a0b5a39b72ba98b3fdf073090a21a9d Wallace Reis committed May 13, 2012
Showing with 47 additions and 2 deletions.
  1. +15 −1 t/Plack-Middleware/httpexceptions.t
  2. +32 −1 t/Plack-Middleware/httpexceptions_streaming.t
View
16 t/Plack-Middleware/httpexceptions.t
@@ -2,6 +2,7 @@ use strict;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;
+BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }
package HTTP::Error;
sub new { bless {}, shift }
@@ -24,6 +25,15 @@ use base qw(HTTP::Error);
sub code { 302 }
sub location { "http://somewhere/else" }
+package MyMiddleware;
+use base 'Plack::Middleware';
@miyagawa
miyagawa added a note May 16, 2012

use parent

@wreis
Owner
wreis added a note May 17, 2012

As the other packages are using base instead of parent.

@miyagawa
miyagawa added a note May 17, 2012

well, which packages? In Plack, we standardize to "use parent" almost (there seems some leftovers in .t files though).
Again, not that it matters much, just taking this as an opportunity to express my preference to be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+sub call {
+ my ( $self, $env ) = @_;
+ die 'Unknown error, but on test purpose'
@miyagawa
miyagawa added a note May 17, 2012

use 4 space indent, no spaces around parens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if $env->{'PATH_INFO'} eq '/unknow_error';
+ return $self->app->($env);
+}
+
package main;
my $app = sub {
@@ -38,12 +48,16 @@ my $app = sub {
};
use Plack::Middleware::HTTPExceptions;
+$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app);
test_psgi $app, sub {
my $cb = shift;
- my $res = $cb->(GET "/");
+ my $res = $cb->(GET '/unknow_error');
+ is $res->code, 500;
+ is $res->content, 'Internal Server Error';
+ $res = $cb->(GET "/");
is $res->code, 500;
is $res->content, 'Internal Server Error';
View
33 t/Plack-Middleware/httpexceptions_streaming.t
@@ -2,6 +2,7 @@ use strict;
use Plack::Test;
use HTTP::Request::Common;
use Test::More;
+BEGIN { $ENV{'PLACK_ENV'} = 'deployment' }
package HTTP::Error;
sub new { bless {}, shift }
@@ -19,6 +20,24 @@ use base qw(HTTP::Error);
sub code { 403 }
sub as_string { "blah blah blah" }
+{
+ no strict 'refs';
+ *Carp::cluck = sub {};
+}
+package MyMiddleware;
+use base 'Plack::Middleware';
+sub call {
+ my ( $self, $env ) = @_;
+ my $res = $self->app->($env);
+ $self->response_cb($res, sub {
+ return sub {
+ die 'Unknown error, but on test purpose'
+ if $env->{PATH_INFO} eq '/unknow_error';
+ return shift;
+ };
+ });
+}
+
package main;
my $app = sub {
@@ -33,17 +52,29 @@ my $app = sub {
$w->close;
};
}
+ elsif ( $env->{PATH_INFO} eq '/unknow_error' ) {
+ return sub {
+ my $res = shift;
+ my $w = $res->([ 200, [ 'Content-Type', 'text/plain' ] ]);
+ $w->write("Hello");
+ $w->close;
+ };
+ }
return sub { HTTP::Error::InternalServerError->throw };
};
use Plack::Middleware::HTTPExceptions;
+$app = MyMiddleware->wrap($app);
$app = Plack::Middleware::HTTPExceptions->wrap($app);
test_psgi $app, sub {
my $cb = shift;
- my $res = $cb->(GET "/");
+ my $res = $cb->(GET '/unknow_error');
+ is $res->code, 500;
+ is $res->content, 'Internal Server Error';
+ $res = $cb->(GET "/");
is $res->code, 500;
is $res->content, 'Internal Server Error';

0 comments on commit 894a3ef

Please sign in to comment.
Something went wrong with that request. Please try again.