Skip to content

Commit

Permalink
added decoded_body method to Mojo::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 20, 2013
1 parent b89e101 commit 3297522
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

4.40 2013-09-20
4.40 2013-09-21
- Added decoded_body method to Mojo::Message.
- Added siblings method to Mojo::DOM.
- Added flatten method to Mojo::Collection.
- Improved documentation browser with source links.
Expand Down
20 changes: 14 additions & 6 deletions lib/Mojo/Message.pm
Expand Up @@ -65,15 +65,17 @@ sub cookie { shift->_cache(cookies => @_) }

sub cookies { croak 'Method "cookies" not implemented by subclass' }

sub decoded_body {
my $self = shift;
my $content = $self->body;
my $charset = $self->content->charset;
return $charset ? decode($charset, $content) // $content : $content;
}

sub dom {
my $self = shift;

return undef if $self->content->is_multipart;
my $html = $self->body;
my $charset = $self->content->charset;
$html = decode($charset, $html) // $html if $charset;
my $dom = $self->{dom} ||= Mojo::DOM->new($html);

my $dom = $self->{dom} ||= Mojo::DOM->new($self->decoded_body);
return @_ ? $dom->find(@_) : $dom;
}

Expand Down Expand Up @@ -471,6 +473,12 @@ it should not be called before all headers have been received.
Access message cookies. Meant to be overloaded in a subclass.
=head2 decoded_body
my $str = $msg->decoded_body;
Slurp C<content> and try to decode it if a charset could be detected.
=head2 dom
my $dom = $msg->dom;
Expand Down
21 changes: 8 additions & 13 deletions lib/Test/Mojo.pm
Expand Up @@ -38,25 +38,25 @@ sub app {
sub content_is {
my ($self, $value, $desc) = @_;
$desc ||= 'exact match for content';
return $self->_test('is', $self->_get_content($self->tx), $value, $desc);
return $self->_test('is', $self->tx->res->decoded_body, $value, $desc);
}

sub content_isnt {
my ($self, $value, $desc) = @_;
$desc ||= 'no match for content';
return $self->_test('isnt', $self->_get_content($self->tx), $value, $desc);
return $self->_test('isnt', $self->tx->res->decoded_body, $value, $desc);
}

sub content_like {
my ($self, $regex, $desc) = @_;
$desc ||= 'content is similar';
return $self->_test('like', $self->_get_content($self->tx), $regex, $desc);
return $self->_test('like', $self->tx->res->decoded_body, $regex, $desc);
}

sub content_unlike {
my ($self, $regex, $desc) = @_;
$desc ||= 'content is not similar';
return $self->_test('unlike', $self->_get_content($self->tx), $regex, $desc);
return $self->_test('unlike', $self->tx->res->decoded_body, $regex, $desc);
}

sub content_type_is {
Expand Down Expand Up @@ -301,13 +301,6 @@ sub websocket_ok {
return $self->_test('ok', $self->tx->res->code eq 101, $desc);
}

sub _get_content {
my ($self, $tx) = @_;
my $content = $tx->res->body;
my $charset = $tx->res->content->charset;
return $charset ? decode($charset, $content) : $content;
}

sub _json {
my ($self, $method, $p) = @_;
return Mojo::JSON::Pointer->new->$method(
Expand Down Expand Up @@ -496,7 +489,8 @@ Access application with L<Mojo::UserAgent/"app">.
$t = $t->content_is('working!');
$t = $t->content_is('working!', 'right content');
Check response content for exact match.
Check response content for exact match after retrieving it from
L<Mojo::Message/"decoded_body">.
=head2 content_isnt
Expand All @@ -510,7 +504,8 @@ Opposite of C<content_is>.
$t = $t->content_like(qr/working!/);
$t = $t->content_like(qr/working!/, 'right content');
Check response content for similar match.
Check response content for similar match after retrieving it from
L<Mojo::Message/"decoded_body">.
=head2 content_unlike
Expand Down

0 comments on commit 3297522

Please sign in to comment.