Commit | Line | Data |
07b00970 |
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; |
14 | use JSON; |
15 | use Data::Dumper; |
16 | |
17 | my $json = JSON->new->utf8; |
18 | |
19 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
20 | ok( my $schema = DBICTest->init_schema(), 'got schema' ); |
21 | |
22 | my $base_url = "$base/api/rest/stashedclass"; |
23 | |
24 | # test cd |
25 | { |
26 | my $class = 'RestTestDB::CD'; |
27 | my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); |
28 | $mech->request($req); |
29 | cmp_ok( $mech->status, '==', '200', "status OK" ); |
30 | my $response = $json->decode( $mech->content ); |
31 | is($response->{success}, 'true', 'success'); |
32 | is(scalar( @{$response->{list}} ), 6, 'six results'); |
33 | } |
34 | |
35 | # test author |
36 | { |
37 | my $class = 'RestTestDB::Artist'; |
38 | my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); |
39 | $mech->request($req); |
40 | cmp_ok( $mech->status, '==', '200', "status OK" ); |
41 | my $response = $json->decode( $mech->content ); |
42 | is($response->{success}, 'true', 'success'); |
43 | is(scalar( @{$response->{list}} ), 3, 'three results'); |
44 | } |
45 | |
46 | # test non-existent class |
47 | { |
48 | my $class = 'Foo::Bar::Baz'; |
49 | my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); |
50 | $mech->request($req); |
51 | cmp_ok( $mech->status, '==', '400', "status 400" ); |
52 | my $response = $json->decode( $mech->content ); |
53 | like($response->{messages}[0], qr/current_result_set.*does not pass the type constraint/, 'invalid class'); |
54 | } |
55 | |
3649b790 |
56 | |
57 | { |
58 | no warnings; |
59 | # stash->{class} should always win over $self->class |
60 | *Catalyst::Controller::DBIC::API::class = sub { 'RestTestDB::CD' }; |
61 | |
62 | my $class = 'RestTestDB::Artist'; |
63 | my $req = GET( "$base_url/$class", {}, 'Accept' => 'text/x-json'); |
64 | $mech->request($req); |
65 | cmp_ok( $mech->status, '==', '200', "status OK" ); |
66 | my $response = $json->decode( $mech->content ); |
67 | is($response->{success}, 'true', 'success'); |
68 | is(scalar( @{$response->{list}} ), 3, 'three results - artist'); |
69 | } |
70 | |
71 | { |
72 | no warnings; |
73 | # stash->{class} not present, ->class should be returned |
74 | *Catalyst::Controller::DBIC::API::class = sub { 'RestTestDB::CD' }; |
75 | |
76 | my $req = GET( "$base_url/noclass", {}, 'Accept' => 'text/x-json'); |
77 | $mech->request($req); |
78 | cmp_ok( $mech->status, '==', '200', "status OK" ); |
79 | my $response = $json->decode( $mech->content ); |
80 | is($response->{success}, 'true', 'success'); |
81 | is(scalar( @{$response->{list}} ), 6, 'six results - cd'); |
82 | } |
83 | |
07b00970 |
84 | done_testing(); |