Commit | Line | Data |
609916e5 |
1 | use 5.6.0; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use lib 't/lib'; |
7 | |
8 | my $base = 'http://localhost'; |
9 | |
10 | use RestTest; |
11 | use DBICTest; |
12 | use URI; |
13 | use Test::More; |
14 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
15 | use HTTP::Request::Common; |
0b0bf911 |
16 | use JSON; |
17 | |
18 | my $json = JSON->new->utf8; |
609916e5 |
19 | |
20 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
0b0bf911 |
21 | ok( my $schema = DBICTest->init_schema(), 'got schema' ); |
609916e5 |
22 | |
23 | my $artist_view_url = "$base/api/rest/artist/"; |
24 | |
25 | { |
26 | my $id = 1; |
0b0bf911 |
27 | my $req = |
28 | GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' ); |
609916e5 |
29 | $mech->request($req); |
30 | cmp_ok( $mech->status, '==', 200, 'open attempt okay' ); |
0b0bf911 |
31 | my %expected_response = |
32 | $schema->resultset('Artist')->find($id)->get_columns; |
33 | my $response = $json->decode( $mech->content ); |
34 | is_deeply( |
35 | $response, |
36 | { data => \%expected_response, success => 'true' }, |
37 | 'correct data returned' |
38 | ); |
609916e5 |
39 | } |
40 | |
41 | { |
42 | my $id = 5; |
0b0bf911 |
43 | my $req = |
44 | GET( $artist_view_url . $id, undef, 'Accept' => 'application/json' ); |
609916e5 |
45 | $mech->request($req); |
46 | cmp_ok( $mech->status, '==', 400, 'open attempt not ok' ); |
0b0bf911 |
47 | my $response = $json->decode( $mech->content ); |
48 | is( $response->{success}, 'false', |
49 | 'not existing object fetch failed ok' ); |
50 | like( |
51 | $response->{messages}->[0], |
52 | qr/^No object found for id/, |
53 | 'error message for not existing object fetch ok' |
54 | ); |
609916e5 |
55 | } |
56 | |
15754afc |
57 | my $track_view_url = "$base/api/rest/track/"; |
58 | |
59 | { |
60 | my $id = 9; |
0b0bf911 |
61 | my $req = |
62 | GET( $track_view_url . $id, undef, 'Accept' => 'application/json' ); |
15754afc |
63 | $mech->request($req); |
64 | cmp_ok( $mech->status, '==', 200, 'got track with datetime object okay' ); |
0b0bf911 |
65 | my %expected_response = |
66 | $schema->resultset('Track')->find($id)->get_columns; |
67 | my $response = $json->decode( $mech->content ); |
68 | is_deeply( |
69 | $response, |
70 | { data => \%expected_response, success => 'true' }, |
71 | 'correct data returned for track with datetime' |
72 | ); |
15754afc |
73 | } |
74 | |
609916e5 |
75 | done_testing(); |