Permalink
Browse files

test and readme

  • Loading branch information...
1 parent e286120 commit 02da0b3a8cadac0e08b3ebb7895200f72844ca8c @monken committed Feb 5, 2011
Showing with 154 additions and 1 deletion.
  1. +1 −1 README.pod
  2. +51 −0 lib/CatalystX/ExtJS/REST.pm
  3. +102 −0 t/direct/rest.t
View
@@ -0,0 +1,51 @@
+package CatalystX::ExtJS::REST;
+
+# ABSTRACT: Feature-rich REST controller for use with ExtJS
+
+1;
+
+__END__
+
+=head1 SYNOPSIS
+
+package MyApp::Controller::User;
+
+use Moose;
+BEGIN { extends 'CatalystX::Controller::ExtJS::REST' }
+
+__PACKAGE__->config( default_resultset => 'User',
+ forms => {
+ default => [
+ { name => 'id' },
+ { name => 'email', constraint => 'Required' },
+ { name => 'password' }
+ ],
+ } );
+
+1;
+
+
+=head1 DESCRIPTION
+
+This module adds feature-rich REST controllers to your application.
+
+L<CatalystX::Controller::ExtJS::REST> gives examples and describes
+all configuration options.
+
+Have a look at the L<tutorial|CatalystX::ExtJS::Tutorial::Direct>
+which shows integration of this controller with L<CatalystX::ExtJS::Direct>.
+
+
+=head1 SEE ALSO
+
+=over 4
+
+=item L<CatalystX::ExtJS>
+
+Parent namespace. Includes examples and the code for the tutorial.
+
+=item L<CatalystX::ExtJS::Direct>
+
+Enable Ext.Direct in Catalyst controllers.
+
+=back
View
@@ -0,0 +1,102 @@
+use Test::More;
+
+use strict;
+use warnings;
+
+use HTTP::Request::Common;
+use JSON::XS;
+
+use lib qw(t/lib);
+
+use Test::WWW::Mechanize::Catalyst 'MyApp';
+
+my $mech = Test::WWW::Mechanize::Catalyst->new();
+my $tid = 1;
+
+ok(
+ my $api = MyApp->controller('API')->api,
+ 'get api directly from controller'
+);
+
+ok(
+ $mech->request(
+ POST $api->{url},
+ Content_Type => 'application/json',
+ Content => q({"action":"User","method":"create","data":[{"rows":[{"name":"a","password":1},{"name":"a","password":1},{"name":"m","password":1}]}],"type":"rpc","tid":6})
+ ),
+ 'create users'
+);
+
+count_users(3);
+
+count_users(1, [{ending => '2'}]);
+
+count_users(0, [{resultset => 'none'}]);
+
+count_users(0, [['foo', not => [1], not => [2], not => [3], 'not']]);
+
+ok(
+ $mech->request(
+ POST $api->{url},
+ Content_Type => 'application/json',
+ Content => q(
+ {"action":"User","method":"destroy","data":[{"rows":"1"}],"type":"rpc","tid":3}
+)
+ ),
+ 'delete user 1'
+);
+
+count_users(2);
+
+ok(
+ $mech->request(
+ POST $api->{url},
+ Content_Type => 'application/json',
+ Content => q(
+ {"action":"User","method":"destroy","data":[{"rows":[2,3]}],"type":"rpc","tid":3}
+)
+ ),
+ 'delete user 1'
+);
+
+
+count_users(0);
+
+ok(
+ $mech->request(
+ POST $api->{url},
+ Content_Type => 'application/json',
+ Content => q({"action":"User","method":"create","data":[{"name":"a"}],"type":"rpc","tid":6})
+ ),
+ 'create users with only one attribute'
+);
+
+count_users(1);
+
+sub count_users {
+ my $user = shift;
+ my $data = shift || [];
+ ok(
+ $mech->request(
+ POST $api->{url},
+ Content_Type => 'application/json',
+ Content => encode_json(
+ {
+ action => 'User',
+ method => 'list',
+ tid => $tid,
+ data => $data,
+ type => 'rpc',
+ }
+ )
+ ),
+ 'get list of users'
+ );
+
+ ok( my $_json = decode_json( $mech->content ), 'response is valid json' );
+ is($_json->{result}->{results}, $user, $user . ' users');
+ return $_json;
+
+}
+
+done_testing;

0 comments on commit 02da0b3

Please sign in to comment.