Version 2.008001
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / 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/rest/artist/";
22
23{
24 my $id = 1;
0b0bf911 25 my $req =
7dccc5de 26 GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 27 $mech->request($req);
28 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
0b0bf911 29 my %expected_response =
30 $schema->resultset('Artist')->find($id)->get_columns;
31 my $response = $json->decode( $mech->content );
c2a3a0b3 32 #artist does not have use_json_boolean => 1, so true values are stringified to 'true'
0b0bf911 33 is_deeply(
34 $response,
88d300e4 35
36 # artist doesn't set use_json_boolean
0b0bf911 37 { data => \%expected_response, success => 'true' },
38 'correct data returned'
39 );
609916e5 40}
41
42{
43 my $id = 5;
0b0bf911 44 my $req =
7dccc5de 45 GET( $artist_view_url . $id, 'Accept' => 'application/json' );
609916e5 46 $mech->request($req);
47 cmp_ok( $mech->status, '==', 400, 'open attempt not ok' );
0b0bf911 48 my $response = $json->decode( $mech->content );
49 is( $response->{success}, 'false',
50 'not existing object fetch failed ok' );
51 like(
52 $response->{messages}->[0],
53 qr/^No object found for id/,
54 'error message for not existing object fetch ok'
55 );
609916e5 56}
57
15754afc 58my $track_view_url = "$base/api/rest/track/";
59
60{
61 my $id = 9;
0b0bf911 62 my $req =
7dccc5de 63 GET( $track_view_url . $id, 'Accept' => 'application/json' );
15754afc 64 $mech->request($req);
65 cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' );
0b0bf911 66 my %expected_response =
67 $schema->resultset('Track')->find($id)->get_columns;
68 my $response = $json->decode( $mech->content );
69 is_deeply(
70 $response,
88d300e4 71
72 # track does set use_json_boolean
a5949bfd 73 { data => \%expected_response, success => JSON::MaybeXS::true },
0b0bf911 74 'correct data returned for track with datetime'
75 );
15754afc 76}
77
68e02291 78{
79 my $req =
7dccc5de 80 GET( $artist_view_url . 'action_with_error', 'Accept' => 'application/json' );
68e02291 81 $mech->request($req);
82 cmp_ok( $mech->status, '==', 404, 'action returned error 404' );
83 my $response = $json->decode( $mech->content );
84 is_deeply(
85 $response,
88d300e4 86
87 # artist doesn't set use_json_boolean
68e02291 88 { success => 'false' },
89 'correct data returned'
90 );
91}
92
609916e5 93done_testing();