Commit | Line | Data |
7ad87df9 |
1 | use strict; |
2 | use warnings; |
2f7533ed |
3 | use Test::More; |
7ad87df9 |
4 | use FindBin; |
5 | |
6 | use lib ("$FindBin::Bin/lib", "$FindBin::Bin/../lib"); |
7 | use Test::Rest; |
c6c4ff28 |
8 | use utf8; |
7ad87df9 |
9 | |
8b010556 |
10 | eval 'use JSON 2.12'; |
11 | plan skip_all => 'Install JSON 2.12 or later to run this test' if ($@); |
7ad87df9 |
12 | |
d6fb033c |
13 | |
2f7533ed |
14 | use_ok 'Catalyst::Test', 'Test::Serialize'; |
17d910bd |
15 | |
c6c4ff28 |
16 | my $json = JSON->new->utf8; |
2f7533ed |
17 | # The text/x-json should throw a warning |
18 | for ('text/x-json', 'application/json') { |
19 | my $t = Test::Rest->new('content_type' => $_); |
20 | my $monkey_template = { |
21 | monkey => 'likes chicken!', |
22 | }; |
23 | my $mres = request($t->get(url => '/monkey_get')); |
24 | ok( $mres->is_success, 'GET the monkey succeeded' ); |
25 | is_deeply($json->decode($mres->content), $monkey_template, "GET returned the right data"); |
17d910bd |
26 | |
2f7533ed |
27 | my $post_data = { |
28 | 'sushi' => 'is good for monkey', |
c6c4ff28 |
29 | 'chicken' => ' 佐藤 純', |
2f7533ed |
30 | }; |
31 | my $mres_post = request($t->post(url => '/monkey_put', data => $json->encode($post_data))); |
32 | ok( $mres_post->is_success, "POST to the monkey succeeded"); |
c6c4ff28 |
33 | my $exp = "is good for monkey 佐藤 純"; |
34 | utf8::encode($exp); |
35 | is_deeply($mres_post->content, $exp, "POST data matches"); |
2f7533ed |
36 | } |
7ad87df9 |
37 | |
838f49dc |
38 | { |
39 | my $t = Test::Rest->new('content_type' => 'application/json'); |
40 | my $json_data = '{ "sushi":"is good for monkey", }'; |
41 | my $mres_post = request($t->post(url => '/monkey_put', data => $json_data)); |
42 | ok( ! $mres_post->is_success, "Got expected failed status due to invalid JSON" ); |
43 | |
44 | my $relaxed_post = request( $t->post(url => "/monkey_json_put", data => $json_data)); |
45 | ok( $relaxed_post->is_success, "Got success due to setting relaxed JSON input" ); |
46 | } |
47 | |
7ad87df9 |
48 | 1; |
10018fb4 |
49 | |
50 | done_testing; |