Skip to content

Commit

Permalink
Added POST support and t/okay.t.
Browse files Browse the repository at this point in the history
  • Loading branch information
chromatic committed Sep 22, 2011
1 parent d0ab4cb commit 5a0bf7f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/Plack/Test/Agent.pm
Expand Up @@ -6,10 +6,10 @@ use warnings;


use Test::TCP; use Test::TCP;
use Plack::Loader; use Plack::Loader;
use HTTP::Request;
use HTTP::Response;
use LWP::UserAgent; use LWP::UserAgent;
use HTTP::Response;
use HTTP::Message::PSGI; use HTTP::Message::PSGI;
use HTTP::Request::Common;


use Plack::Util::Accessor qw( app host port server ua ); use Plack::Util::Accessor qw( app host port server ua );


Expand Down Expand Up @@ -59,13 +59,22 @@ sub execute_req
sub get sub get
{ {
my ($self, $uri) = @_; my ($self, $uri) = @_;
my $req = HTTP::Request->new( GET => $self->normalize_uri($uri) ); my $req = GET $self->normalize_uri($uri);
my $res = $self->execute_req( $req ); my $res = $self->execute_req( $req );


$res->request( $req ); $res->request( $req );
return $res; return $res;
} }


sub post
{
my ($self, $uri, @args) = @_;
my $req = POST $self->normalize_uri($uri), @args;
my $res = $self->execute_req( $req );
$res->request( $req );
return $res;
}

sub normalize_uri sub normalize_uri
{ {
my ($self, $uri) = @_; my ($self, $uri) = @_;
Expand Down
54 changes: 54 additions & 0 deletions t/okay.t
@@ -0,0 +1,54 @@
#!/usr/bin/env perl

use Modern::Perl;

use utf8;
use open ':encoding(utf8)';

use Test::More;
use Plack::Test::Agent;
use Plack::Request;

my $app = sub
{
my $res = Plack::Request->new( shift );

my $want = $res->param( 'want' );
my $have = $res->param( 'have' );
my $desc = $res->param( 'desc' );

my ($code, $output) = ( $want eq $have )
? ( 200, 'ok' )
: ( 412, 'not ok' );

$output .= ' - ' . $desc if $desc;
return [ $code, [ 'Content-Type' => 'text/plain' ], [ $output ] ];
};

my $agent = Plack::Test::Agent->new( app => $app );
my $res = $agent->get( '/?have=foo;want=foo' );
ok $res->is_success, 'Request should succeed when values match';
is $res->decoded_content, 'ok', '... with descriptive success message';

$res = $agent->get( '/?have=10;want=20' );
ok ! $res->is_success, 'Request should fail when values do not match';
is $res->decoded_content, 'not ok', '... with descriptive error';

my $uri = URI->new( '/' );
$uri->query_form( have => 'cow', want => 'cow', desc => 'Cow Comparison' );
$res = $agent->get( $uri );

ok $res->is_success, 'Request should succeed when values do';
is $res->decoded_content, 'ok - Cow Comparison',
'... including description when provided';
is $res->content_type, 'text/plain', '... with plain text content';
is $res->content_charset, 'US-ASCII', '... in ASCII';

$res = $agent->post( '/', [ have => 'cow', want => 'pig', desc => 'æ' ] );
ok ! $res->is_success, 'Request should fail given different values';
is $res->decoded_content, "not ok - \x{00E6}",
'... including description when provided';
is $res->content_type, 'text/plain', '... with plain text content';
is $res->content_charset, 'UTF-8', '... in ASCII';

done_testing;

0 comments on commit 5a0bf7f

Please sign in to comment.