Skip to content

Commit

Permalink
Improve handling of utf8 in request and response bodies:
Browse files Browse the repository at this point in the history
* if the request contnet string is utf8 flagged, encode it before sending
* in body_is and body_like compare with the decoded_content of the response
  not the content


git-svn-id: https://repo.socialtext.net:8999/svn/test-http/trunk@15 3a6af3b1-8918-0410-b6f8-df6293f0f14b
  • Loading branch information
Chris Dent committed Nov 29, 2006
1 parent 012471a commit 3390256
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/Test/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ status codes, headers, and message bodies.
use base 'Exporter';
use Carp 'croak';
use Class::Field 'field';
use Encode qw(encode_utf8);
use Encode qw(encode_utf8 is_utf8);
use Filter::Util::Call;
use HTTP::Request;
use Test::Builder;
Expand Down Expand Up @@ -209,6 +209,14 @@ the resulting L<HTTP::Response>.
sub run_request {
my ( $self, @request_args ) = @_;
$self->new_request(@request_args) if @request_args;
if ($self->request->method ne 'GET') {
if (is_utf8($self->request->content)) {
my $content = $self->request->content;
$content = encode_utf8($content);
$self->request->content($content);
}
}

$self->response( $self->ua->simple_request( $self->request ) );
croak( $self->request->uri . ': ' . $self->response->status_line )
if $self->response->status_line =~ /500 Can't connect to /;
Expand Down Expand Up @@ -301,7 +309,7 @@ sub body_is {

$description ||= $self->name . " body is '$expected_body'.";

$Builder->is_eq( $self->response->content, $expected_body, $description );
$Builder->is_eq( $self->response->decoded_content, $expected_body, $description );
}

=head2 $test->body_like($regex [, $description]);
Expand All @@ -316,7 +324,7 @@ sub body_like {

$description ||= $self->name . " body matches $regex.";

$Builder->like( $self->response->content, $regex, $description );
$Builder->like( $self->response->decoded_content, $regex, $description );
}

=head1 USER AGENT GENERATION
Expand Down

0 comments on commit 3390256

Please sign in to comment.