use Dist::Zilla::PluginBundle::Git
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / item.t
index 3b075aa..845bc1a 100644 (file)
@@ -11,9 +11,9 @@ use URI;
 use Test::More;
 use Test::WWW::Mechanize::Catalyst 'RestTest';
 use HTTP::Request::Common;
-use JSON;
+use JSON::MaybeXS;
 
-my $json = JSON->new->utf8;
+my $json = JSON::MaybeXS->new(utf8 => 1);
 
 my $mech = Test::WWW::Mechanize::Catalyst->new;
 ok( my $schema = DBICTest->init_schema(), 'got schema' );
@@ -23,14 +23,17 @@ my $artist_view_url = "$base/api/rest/artist/";
 {
     my $id = 1;
     my $req =
-        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+        GET( $artist_view_url . $id, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
     my %expected_response =
         $schema->resultset('Artist')->find($id)->get_columns;
     my $response = $json->decode( $mech->content );
+    #artist does not have use_json_boolean => 1, so true values are stringified to 'true'
     is_deeply(
         $response,
+
+        # artist doesn't set use_json_boolean
         { data => \%expected_response, success => 'true' },
         'correct data returned'
     );
@@ -39,7 +42,7 @@ my $artist_view_url = "$base/api/rest/artist/";
 {
     my $id = 5;
     my $req =
-        GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' );
+        GET( $artist_view_url . $id, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
     my $response = $json->decode( $mech->content );
@@ -57,7 +60,7 @@ my $track_view_url = "$base/api/rest/track/";
 {
     my $id = 9;
     my $req =
-        GET( $track_view_url . $id, undef, 'Accept' => 'application/json' );
+        GET( $track_view_url . $id, 'Accept' => 'application/json' );
     $mech->request($req);
     cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
     my %expected_response =
@@ -65,9 +68,26 @@ my $track_view_url = "$base/api/rest/track/";
     my $response = $json->decode( $mech->content );
     is_deeply(
         $response,
-        { data => \%expected_response, success => 'true' },
+
+        # track does set use_json_boolean
+        { data => \%expected_response, success => JSON::MaybeXS::true },
         'correct data returned for track with datetime'
     );
 }
 
+{
+    my $req =
+        GET( $artist_view_url . 'action_with_error', 'Accept' => 'application/json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 404, 'action returned error 404' );
+    my $response = $json->decode( $mech->content );
+    is_deeply(
+        $response,
+
+        # artist doesn't set use_json_boolean
+        { success => 'false' },
+        'correct data returned'
+    );
+}
+
 done_testing();