Skip to content

Commit

Permalink
added json_message_content_is method to Test::Mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2013
1 parent 1d98aeb commit f0e26bd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
3.77 2013-01-11
- Added support for advanced binary WebSocket message tests to Test::Mojo.
- Added binary and text events to Mojo::Transaction::WebSocket.
- Added json_message_content_is method to Test::Mojo.
- Added j function to Mojo::JSON.
- Improved tests.
- Fixed aliasing bug in Mojo::EventEmitter.
Expand Down
46 changes: 31 additions & 15 deletions lib/Test/Mojo.pm
Expand Up @@ -9,6 +9,7 @@ use Mojo::Base -base;
# Bender: You're better off dead, I'm telling you, dude.
# Fry: Santa Claus is gunning you down!"
use Mojo::IOLoop;
use Mojo::JSON;
use Mojo::Server;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);
Expand Down Expand Up @@ -142,12 +143,6 @@ sub json_content_is {
return $self->_test('is_deeply', $self->tx->res->json, $data, $desc);
}

sub json_is {
my ($self, $p, $data, $desc) = @_;
$desc ||= qq{exact match for JSON Pointer "$p"};
return $self->_test('is_deeply', $self->tx->res->json($p), $data, $desc);
}

sub json_has {
my ($self, $p, $desc) = @_;
$desc ||= qq{has value for JSON Pointer "$p"};
Expand All @@ -162,6 +157,19 @@ sub json_hasnt {
!Mojo::JSON::Pointer->new->contains($self->tx->res->json, $p), $desc);
}

sub json_is {
my ($self, $p, $data, $desc) = @_;
$desc ||= qq{exact match for JSON Pointer "$p"};
return $self->_test('is_deeply', $self->tx->res->json($p), $data, $desc);
}

sub json_message_content_is {
my ($self, $data, $desc) = @_;
my $structure = Mojo::JSON->new->decode(@{$self->_next || []}[1]);
return $self->_test('is_deeply', $structure, $data,
$desc || 'exact match for JSON structure');
}

sub message_is {
my ($self, $value, $desc) = @_;
return $self->_message('is', $value, $desc || 'exact match for message');
Expand Down Expand Up @@ -590,15 +598,6 @@ Opposite of C<header_like>.
Check response content for JSON data.
=head2 json_is
$t = $t->json_is('/foo' => {bar => [1, 2, 3]});
$t = $t->json_is('/foo/bar' => [1, 2, 3]);
$t = $t->json_is('/foo/bar/1' => 2, 'right value');
Check the value extracted from JSON response using the given JSON Pointer with
L<Mojo::JSON::Pointer>.
=head2 json_has
$t = $t->json_has('/foo');
Expand All @@ -614,6 +613,23 @@ JSON Pointer with L<Mojo::JSON::Pointer>.
Opposite of C<json_has>.
=head2 json_is
$t = $t->json_is('/foo' => {bar => [1, 2, 3]});
$t = $t->json_is('/foo/bar' => [1, 2, 3]);
$t = $t->json_is('/foo/bar/1' => 2, 'right value');
Check the value extracted from JSON response using the given JSON Pointer with
L<Mojo::JSON::Pointer>.
=head2 json_message_content_is
$t = $t->json_message_content_is([1, 2, 3]);
$t = $t->json_message_content_is([1, 2, 3], 'right content');
$t = $t->json_message_content_is({foo => 'bar', baz => 23}, 'right content');
Check WebSocket message content for JSON data.
=head2 message_is
$t = $t->message_is({binary => $bytes});
Expand Down
22 changes: 22 additions & 0 deletions t/mojolicious/websocket_lite_app.t
Expand Up @@ -8,6 +8,7 @@ BEGIN {

use Test::More;
use Mojo::ByteStream 'b';
use Mojo::JSON 'j';
use Mojolicious::Lite;
use Test::Mojo;

Expand All @@ -26,6 +27,20 @@ websocket '/echo' => sub {
# GET /echo
get '/echo' => {text => 'plain echo!'};

# WebSocket /json
websocket '/json' => sub {
my $self = shift;
$self->on(binary => sub { shift->send({binary => j([@{j(shift)}, 4])}) });
$self->on(
text => sub {
my ($self, $json) = @_;
my $hash = j($json);
$hash->{test} += 1;
$self->send({text => j($hash)});
}
);
};

# GET /plain
get '/plain' => {text => 'Nothing to see here!'};

Expand Down Expand Up @@ -125,6 +140,13 @@ $t->websocket_ok('/echo')->send_ok(0)->message_is('echo: 0')->send_ok(0)
# GET /echo (plain alternative)
$t->get_ok('/echo')->status_is(200)->content_is('plain echo!');

# WebSocket /json
$t->websocket_ok('/json')
->send_ok({text => j({test => 23, snowman => ''})})
->json_message_content_is({test => 24, snowman => ''})
->send_ok({binary => j([1, 2, 3])})
->json_message_content_is([1, 2, 3, 4], 'right content')->finish_ok;

# GET /plain
$t->get_ok('/plain')->status_is(200)->content_is('Nothing to see here!');

Expand Down

0 comments on commit f0e26bd

Please sign in to comment.