Permalink
Browse files

Added POST support and t/okay.t.

  • Loading branch information...
1 parent d0ab4cb commit 5a0bf7fc84afc500b9815fe23e97369d572c860f @chromatic committed Sep 22, 2011
Showing with 66 additions and 3 deletions.
  1. +12 −3 lib/Plack/Test/Agent.pm
  2. +54 −0 t/okay.t
@@ -6,10 +6,10 @@ use warnings;
use Test::TCP;
use Plack::Loader;
-use HTTP::Request;
-use HTTP::Response;
use LWP::UserAgent;
+use HTTP::Response;
use HTTP::Message::PSGI;
+use HTTP::Request::Common;
use Plack::Util::Accessor qw( app host port server ua );
@@ -59,13 +59,22 @@ sub execute_req
sub get
{
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 );
$res->request( $req );
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
{
my ($self, $uri) = @_;
View
@@ -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.