8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $artist_create_url = "$base/api/rpc/artist/create";
24 my $any_artist_create_url = "$base/api/rpc/any/artist/create";
25 my $producer_create_url = "$base/api/rpc/producer/create";
27 # test validation when no params sent
31 { wrong_param => 'value' },
32 'Accept' => 'text/json'
34 $mech->request( $req, $content_type );
35 cmp_ok( $mech->status, '==', 400,
36 'attempt without required params caught' );
37 my $response = $json->decode( $mech->content );
39 $response->{messages}->[0],
40 qr/No value supplied for name and no default/,
41 'correct message returned'
45 # test default value used if default value exists
52 'Accept' => 'text/json'
54 $mech->request( $req, $content_type );
55 cmp_ok( $mech->status, '==', 200,
56 'default value used when not supplied' );
57 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
58 'record created with default name' );
61 # test create works as expected when passing required value
65 { name => 'king luke' },
66 'Accept' => 'text/json'
68 $mech->request( $req, $content_type );
69 cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
72 $schema->resultset('Producer')->find( { name => 'king luke' } );
73 ok( $new_obj, 'record created with specified name' );
75 my $response = $json->decode( $mech->content );
78 { $new_obj->get_columns },
79 'json for new producer returned'
83 # test stash config handling
86 $any_artist_create_url,
87 { name => 'queen monkey' },
88 'Accept' => 'text/json'
90 $mech->request( $req, $content_type );
91 cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
94 $schema->resultset('Artist')->find( { name => 'queen monkey' } );
95 ok( $new_obj, 'record created with specified name' );
97 my $response = $json->decode( $mech->content );
100 { success => 'true' },
101 'json for new artist returned'
105 # test create returns an error as expected when passing invalid value
107 my $long_string = '-' x 1024;
110 $producer_create_url,
111 { producerid => $long_string,
112 name => $long_string,
114 'Accept' => 'text/json'
116 $mech->request( $req, $content_type );
117 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );