Skip to content

Commit

Permalink
More sensible response assertions in Plack::Middleware::Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Dec 11, 2009
1 parent 4b26ec8 commit 23ff199
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/Plack/Middleware/Lint.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package Plack::Middleware::Lint;
use strict;
no warnings;
use Carp;
use Carp ();
use parent qw(Plack::Middleware);
use Scalar::Util qw(blessed);
use Plack::Util;

sub call {
my $self = shift;
Expand Down Expand Up @@ -61,11 +63,33 @@ sub validate_env {

sub validate_res {
my ($self, $res) = @_;

unless (ref($res) and ref($res) eq 'ARRAY' || ref($res) eq 'CODE') {
Carp::croak('response should be arrayref or coderef');
}
if (scalar(@$res) == 3 && !ref($res)) {
Carp::croak('third elements in response arrayref should be reference');

return unless ref $res eq 'ARRAY';

unless (@$res == 3) {
Carp::croak('response needs to be 3 element array');
}

unless ($res->[0] =~ /^\d+$/ && $res->[0] >= 100) {
Carp::croak('status code needs to be an integer greater than or equal to 100');
}

unless (ref $res->[1] eq 'ARRAY') {
Carp::croak('Headers needs to be an array ref');
}

unless (ref $res->[2] eq 'ARRAY' ||
Plack::Util::is_real_fh($res->[2]) ||
(blessed($res->[2]) && $res->[2]->can('getline'))) {
Carp::croak('body should be an array ref or filehandle');
}

if (ref $res->[2] eq 'ARRAY' && grep utf8::is_utf8($_), @{$res->[2]}) {
Carp::croak('body must be bytes and should not contain wide characters (UTF-8 strings).');
}
}

Expand Down

0 comments on commit 23ff199

Please sign in to comment.