Use JSON::MaybeXS for improved performance
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / item.t
CommitLineData
609916e5 1use strict;
2use warnings;
3
4use lib 't/lib';
5
6my $base = 'http://localhost';
7
8use RestTest;
9use DBICTest;
10use URI;
11use Test::More;
12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
a5949bfd 14use JSON::MaybeXS;
0b0bf911 15
a5949bfd 16my $json = JSON::MaybeXS->new(utf8 => 1);
609916e5 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 19ok( my $schema = DBICTest->init_schema(), 'got schema' );
609916e5 20
21my $artist_view_url = "$base/api/rpc/artist/id/";
22
23{
24 my $id = 1;
7dccc5de 25 my $req = GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 26 $mech->request($req);
27 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
0b0bf911 28 my %expected_response =
29 $schema->resultset('Artist')->find($id)->get_columns;
30 my $response = $json->decode( $mech->content );
31 is_deeply(
32 $response,
33 { data => \%expected_response, success => 'true' },
34 'correct data returned'
35 );
609916e5 36}
37
38{
39 my $id = 5;
7dccc5de 40 my $req = GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 41 $mech->request($req);
42 cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
0b0bf911 43 my $response = $json->decode( $mech->content );
44 is( $response->{success}, 'false',
45 'not existing object fetch failed ok' );
46 like(
47 $response->{messages}->[0],
48 qr/^No object found for id/,
49 'error message for not existing object fetch ok'
50 );
609916e5 51}
52
53done_testing();