Switch from JSON to JSON::MaybeXS to get a better choice of JSON parsers.
[catagits/Catalyst-Action-REST.git] / t / json.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use FindBin;
5 use Test::Requires qw(JSON::MaybeXS);
6
7 use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib");
8 use Test::Rest;
9 use utf8;
10
11 use_ok 'Catalyst::Test', 'Test::Serialize';
12
13 my $json = JSON->new->utf8;
14 # The text/x-json should throw a warning
15 for ('text/x-json', 'application/json') {
16     my $t = Test::Rest->new('content_type' => $_);
17     my $monkey_template = {
18         monkey => 'likes chicken!',
19     };
20     my $mres = request($t->get(url => '/monkey_get'));
21     ok( $mres->is_success, 'GET the monkey succeeded' );
22     is_deeply($json->decode($mres->content), $monkey_template, "GET returned the right data");
23
24     my $post_data = {
25         'sushi' => 'is good for monkey',
26         'chicken' => ' 佐藤 純',
27     };
28     my $mres_post = request($t->post(url => '/monkey_put', data => $json->encode($post_data)));
29     ok( $mres_post->is_success, "POST to the monkey succeeded");
30     my $exp = "is good for monkey 佐藤 純";
31     utf8::encode($exp);
32     is_deeply($mres_post->content, $exp, "POST data matches");
33 }
34
35 {
36     my $t = Test::Rest->new('content_type' => 'application/json');
37     my $json_data = '{ "sushi":"is good for monkey", }';
38     my $mres_post = request($t->post(url => '/monkey_put', data => $json_data));
39     ok( ! $mres_post->is_success, "Got expected failed status due to invalid JSON" );
40
41     my $relaxed_post = request( $t->post(url => "/monkey_json_put", data => $json_data));
42     ok( $relaxed_post->is_success, "Got success due to setting relaxed JSON input" );
43 }
44
45 1;
46
47 done_testing;