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