Skip to content

Commit

Permalink
removed json_content_is method from Test::Mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 15, 2013
1 parent 0289626 commit ef7170b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 68 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -25,6 +25,7 @@
- Removed charset method from Mojo::DOM.
- Removed render_data, render_json, render_partial and render_text methods
from Mojolicious::Controller.
- Removed json_content_is method from Test::Mojo.
- Removed has_leftovers method from Mojo::Content.
- Removed is_chunked, is_dynamic, is_multipart, has_leftovers, leftovers,
write and write_chunk methods from Mojo::Message.
Expand All @@ -49,6 +50,8 @@
the PLACK_ENV environment variable.
- Improved Mojolicious to not trap exceptions if the default exception
handling has been deactivated.
- Improved json_is and json_message_is methods in Test::Mojo by making the
JSON Pointer optional.
- Improved renderer performance.
- Improved Mojo::DOM::HTML performance.
- Improved Mojo::Reactor::Poll performance.
Expand Down
39 changes: 15 additions & 24 deletions lib/Test/Mojo.pm
Expand Up @@ -146,12 +146,6 @@ sub header_unlike {
$regex, $desc || "$name is not similar");
}

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

sub json_has {
my ($self, $p, $desc) = @_;
$desc ||= qq{has value for JSON Pointer "$p"};
Expand All @@ -167,8 +161,9 @@ sub json_hasnt {
}

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

Expand All @@ -185,8 +180,9 @@ sub json_message_hasnt {
}

sub json_message_is {
my ($self, $p, $data, $desc) = @_;
$desc ||= qq{exact match for JSON Pointer "$p"};
my ($self, $p) = (shift, shift);
my $data = ref $p ? $p : shift;
my $desc = shift || qq{exact match for JSON Pointer "$p"};
return $self->_test('is_deeply', $self->_json(get => $p), $data, $desc);
}

Expand Down Expand Up @@ -638,14 +634,6 @@ Check response header for similar match.
Opposite of C<header_like>.
=head2 json_content_is
$t = $t->json_content_is([1, 2, 3]);
$t = $t->json_content_is([1, 2, 3], 'right content');
$t = $t->json_content_is({foo => 'bar', baz => 23}, 'right content');
Check response content for JSON data.
=head2 json_has
$t = $t->json_has('/foo');
Expand All @@ -663,12 +651,13 @@ Opposite of C<json_has>.
=head2 json_is
$t = $t->json_is('' => {foo => [1, 2, 3]});
$t = $t->json_is({foo => [1, 2, 3]});
$t = $t->json_is({foo => [1, 2, 3]}, 'right content');
$t = $t->json_is('/foo' => [1, 2, 3]);
$t = $t->json_is('/foo/1' => 2, 'right value');
Check the value extracted from JSON response using the given JSON Pointer with
L<Mojo::JSON::Pointer>.
L<Mojo::JSON::Pointer>, which defaults to the root value if it is omitted.
=head2 json_message_has
Expand All @@ -687,12 +676,14 @@ Opposite of C<json_message_has>.
=head2 json_message_is
$t = $t->json_message_is('' => {foo => [1, 2, 3]});
$t = $t->json_message_is({foo => [1, 2, 3]});
$t = $t->json_message_is({foo => [1, 2, 3]}, 'right content');
$t = $t->json_message_is('/foo' => [1, 2, 3]);
$t = $t->json_message_is('/foo/1' => 2, 'right value');
Check the value extracted from JSON WebSocket message using the given JSON
Pointer with L<Mojo::JSON::Pointer>.
Pointer with L<Mojo::JSON::Pointer>, which defaults to the root value if it is
omitted.
=head2 message_is
Expand Down Expand Up @@ -791,7 +782,7 @@ arguments as L<Mojo::UserAgent/"post">, except for the callback.
# Test JSON API
$t->post_json_ok('/hello.json' => json => {hello => 'world'})
->status_is(200)
->json_content_is({bye => 'world'});
->json_is({bye => 'world'});
=head2 put_ok
Expand All @@ -812,7 +803,7 @@ Perform request and check for transport errors.
# Request with custom method
my $tx = $t->ua->build_tx(FOO => '/test.json' => json => {foo => 1});
$t->request_ok($tx)->status_is(200)->json_content_is({success => 1});
$t->request_ok($tx)->status_is(200)->json_is({success => 1});
=head2 reset_session
Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/charset_lite_app.t
Expand Up @@ -98,17 +98,17 @@ $t->post_ok('/data', $yatta_sjis)->status_is(200)->content_is($yatta_sjis);

# JSON data
$t->get_ok('/json')->status_is(200)->content_type_is('application/json')
->json_content_is({test => $yatta});
->json_is({test => $yatta});

# IRI
$t->get_ok('/привет/мир')->status_is(200)
->content_type_is('application/json')->json_content_is({foo => $yatta});
->content_type_is('application/json')->json_is({foo => $yatta});

# Shift_JIS parameters
my $url = $t->ua->app_url->path('/params')->query(foo => 3, yatta => $yatta);
$url->query->charset('shift_jis');
$t->get_ok($url)->status_is(200)
->json_content_is({params => {foo => 3, yatta => $yatta}, yatta => $yatta});
->json_is({params => {foo => 3, yatta => $yatta}, yatta => $yatta});

done_testing();

Expand Down
5 changes: 2 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -595,7 +595,7 @@ $t->get_ok('/without-format')->content_is("/without-format\n");
$t->get_ok('/without-format.html')->content_is("/without-format\n");

# JSON response
$t->get_ok('/json_too')->status_is(200)->json_content_is({hello => 'world'});
$t->get_ok('/json_too')->status_is(200)->json_is({hello => 'world'});

# Static inline file
$t->get_ok('/static.txt')->status_is(200)
Expand Down Expand Up @@ -861,8 +861,7 @@ $t->post_ok('/malformed_utf8' =>

# JSON
$t->get_ok('/json')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->content_type_is('application/json')
->json_content_is({foo => [1, -2, 3, 'b☃r']})
->content_type_is('application/json')->json_is({foo => [1, -2, 3, 'b☃r']})
->json_is('/foo' => [1, -2, 3, 'b☃r'])->json_is('/foo/3', 'b☃r')
->json_has('/foo')->json_hasnt('/bar');

Expand Down
65 changes: 28 additions & 37 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -45,19 +45,19 @@ $t->post_ok('/json/echo' => json => {hello => 'world'})->status_is(204)
$t->post_ok(
'/json/echo' => {Accept => 'application/json'} => json => {hello => 'world'})
->status_is(200)->content_type_is('application/json')
->json_content_is({hello => 'world'});
->json_is({hello => 'world'});
my $tx
= $t->ua->build_tx(
PUT => '/json/echo' => {Accept => 'application/json'} => json =>
{hello => 'world'});
$t->request_ok($tx)->status_is(200)->content_type_is('application/json')
->json_content_is({hello => 'world'});
->json_is({hello => 'world'});

# Array with "json" format
$tx = $t->ua->build_tx(
PUT => '/json/echo' => {Accept => 'application/json'} => json => [1, 2, 3]);
$t->request_ok($tx, 'request succesful')->status_is(200)
->content_type_is('application/json')->json_content_is([1, 2, 3]);
->content_type_is('application/json')->json_is([1, 2, 3]);

# Nothing
$t->get_ok('/rest')->status_is(200)
Expand Down Expand Up @@ -116,45 +116,45 @@ $t->get_ok('/rest.html?format=html' => {Accept => 'text/html'})

# "json" format
$t->get_ok('/rest.json')->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});
->json_is({just => 'works'});

# Accept "json"
$t->get_ok('/rest' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# Accept "json" again
$t->get_ok('/rest' => {Accept => 'APPLICATION/JSON'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# Accept "json" with format
$t->get_ok('/rest.json' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# Accept "json" with wrong format
$t->get_ok('/rest.png' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# Accept "json" with quality
$t->get_ok('/rest' => {Accept => 'application/json;q=9'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# "json" query
$t->get_ok('/rest?format=json')->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# "json" format with query
$t->get_ok('/rest.json?format=json')->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});
->content_type_is('application/json')->json_is({just => 'works'});

# Accept "json" with query
$t->get_ok('/rest?format=json' => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});
->json_is({just => 'works'});

# Accept "json" with everything
$t->get_ok('/rest.json?format=json' => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});
->json_is({just => 'works'});

# "xml" format
$t->get_ok('/rest.xml')->status_is(200)->content_type_is('application/xml')
Expand Down Expand Up @@ -279,74 +279,65 @@ $t->post_ok(

# "json" format
$t->post_ok('/rest.json')->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
->json_is({just => 'works too'});

# Accept "json"
$t->post_ok('/rest' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" again
$t->post_ok('/rest' => {Accept => 'APPLICATION/JSON'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" with format
$t->post_ok('/rest.json' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" with wrong format
$t->post_ok('/rest.png' => {Accept => 'application/json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" with quality
$t->post_ok('/rest' => {Accept => 'application/json;q=9'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# "json" query
$t->post_ok('/rest?format=json')->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# "json" format with query
$t->post_ok('/rest.json?format=json')->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" with query
$t->post_ok('/rest?format=json' => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
->json_is({just => 'works too'});

# Accept "json" with everything
$t->post_ok('/rest.json?format=json' => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
->json_is({just => 'works too'});

# "json" form
$t->post_ok('/rest' => form => {format => 'json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# "json" format with form
$t->post_ok('/rest.json' => form => {format => 'json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});
->content_type_is('application/json')->json_is({just => 'works too'});

# Accept "json" with form
$t->post_ok(
'/rest' => {Accept => 'application/json'} => form => {format => 'json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
->json_is({just => 'works too'});

# Accept "json" with everything, form alternative
$t->post_ok(
'/rest.json' => {Accept => 'application/json'} => form => {format => 'json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
->json_is({just => 'works too'});

# "xml" format
$t->post_ok('/rest.xml')->status_is(200)->content_type_is('application/xml')
Expand Down Expand Up @@ -467,6 +458,6 @@ my $jquery = 'application/json, text/javascript, */*; q=0.01';
$t->get_ok(
'/rest' => {Accept => $jquery, 'X-Requested-With' => 'XMLHttpRequest'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});
->json_is({just => 'works'});

done_testing();
4 changes: 3 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -149,9 +149,11 @@ $t->get_ok('/echo')->status_is(200)->content_is('plain echo!');
# JSON roundtrips
$t->websocket_ok('/json')->send_ok({json => {test => 23, snowman => ''}})
->message_ok->json_message_is('' => {test => 24, snowman => ''})
->json_message_is('' => {test => 24, snowman => ''}, 'right content')
->json_message_has('/test')->json_message_hasnt('/test/2')
->send_ok({binary => j([1, 2, 3])})
->message_ok->json_message_is('' => [1, 2, 3, 4], 'right content')
->message_ok->json_message_is([1, 2, 3, 4])
->json_message_is([1, 2, 3, 4], 'right content')
->send_ok({binary => j([1, 2, 3])})
->message_ok->json_message_has('/2', 'has two elements')
->json_message_is('/2' => 3)->json_message_hasnt('/5', 'not five elements')
Expand Down

0 comments on commit ef7170b

Please sign in to comment.