This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag 0.07
in repository libdancer-plugin-rest-perl.

commit 9b9073ac0d0941616fe440f4d95c3333aeecee55
Author: franck cuny <fra...@lumberjaph.net>
Date:   Mon Sep 6 20:59:42 2010 +0200

    add some helpers funcions to modify the HTTP status
---
 lib/Dancer/Plugin/REST.pm | 63 ++++++++++++++++++++++++++++++
 t/04_helpers.t            | 97 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 160 insertions(+)

diff --git a/lib/Dancer/Plugin/REST.pm b/lib/Dancer/Plugin/REST.pm
index 8ecf2b3..1c31a48 100644
--- a/lib/Dancer/Plugin/REST.pm
+++ b/lib/Dancer/Plugin/REST.pm
@@ -58,6 +58,35 @@ sub {
     del "/${resource}/:id" => $triggers{delete};
 };
 
+register send_entity => sub {
+    my ($entity, $http_code) = @_;
+
+    $http_code ||= 200;
+
+    status($http_code);
+    $entity;
+};
+
+register status_ok => sub {
+    send_entity($_[0]);
+};
+
+register status_created => sub {
+    send_entity($_[0], 201);
+};
+
+register status_accepted => sub {
+    send_entity($_[0], 202);
+};
+
+register status_bad_request => sub {
+    send_entity({error => $_[0]}, 400);
+};
+
+register status_not_found => sub {
+    send_entity({error => $_[0]}, 404);
+};
+
 register_plugin;
 
 1;
@@ -127,6 +156,40 @@ This keyword lets you declare a resource your application 
will handle.
     # PUT /user/:id
     # PUT /user/:id.:format
 
+=head2 helpers
+
+Some helpers are available. This helper will set an appropriate HTTP status 
for you.
+
+=head3 status_ok
+
+    status_ok({users => {...}});
+
+Set the HTTP status to 200
+
+=head3 status_created
+
+    status_created({users => {...}});
+
+Set the HTTP status to 201
+
+=head3 status_accepted
+
+    status_accepted({users => {...}});
+
+Set the HTTP status to 202
+
+=head3 status_bad_request
+
+    status_bad_request("user foo can't be found");
+
+Set the HTTP status to 400. This function as for argument a scalar that will 
be used under the key B<error>.
+
+=head3 status_not_found
+
+    status_not_found("users doesn't exists");
+
+Set the HTTP status to 404. This function as for argument a scalar that will 
be used under the key B<error>.
+
 =head1 LICENCE
 
 This module is released under the same terms as Perl itself.
diff --git a/t/04_helpers.t b/t/04_helpers.t
new file mode 100644
index 0000000..0fb2901
--- /dev/null
+++ b/t/04_helpers.t
@@ -0,0 +1,97 @@
+use strict;
+use warnings;
+use Dancer::ModuleLoader;
+use Test::More import => ['!pass'];
+
+plan tests => 16;
+
+{
+    package Webservice;
+    use Dancer;
+    use Dancer::Plugin::REST;
+
+    resource user => 
+        'get' => \&on_get_user,
+        'create' => \&on_create_user,
+        'delete' => \&on_delete_user,
+        'update' => \&on_update_user;
+
+    my $users = {};
+    my $last_id = 0;
+
+    sub on_get_user {
+        my $id = params->{'id'};
+        return status_bad_request('id is missing') if !defined $users->{$id};
+        status_ok( { user => $users->{$id} } );
+    }
+
+    sub on_create_user {
+        my $id   = ++$last_id;
+        my $user = params('body');
+        $user->{id} = $id;
+        $users->{$id} = $user;
+
+        status_created( { user => $users->{$id} } );
+    }
+
+    sub on_delete_user {
+        my $id = params->{'id'};
+        my $deleted = $users->{$id};
+        delete $users->{$id};
+        status_accepted({ user => $deleted });
+    }
+
+    sub on_update_user {
+        my $id   = params->{'id'};
+        my $user = $users->{$id};
+        return status_not_found("user undef") unless defined $user;
+
+        $users->{$id} = { %$user, %{ params('body') } };
+        status_accepted { user => $users->{$id} };
+    }
+
+}
+
+use lib 't';
+use TestUtils;
+
+my $r = get_response_for_request(GET => '/user/1');
+is $r->{status}, 400, 'HTTP code is 400';
+is $r->{content}->{error}, 'id is missing', 'Valid content';
+
+$r = get_response_for_request(POST => '/user', { name => 'Alexis' });
+is $r->{status}, 201, 'HTTP code is 201';
+is_deeply $r->{content}, { user => { id => 1, name => "Alexis" } },
+    "create user works";
+
+$r = get_response_for_request(GET => '/user/1');
+is $r->{status}, 200, 'HTTP code is 200';
+is_deeply $r->{content}, {user => { id => 1, name => 'Alexis'}},
+    "user 1 is defined";
+
+$r = get_response_for_request(PUT => '/user/1', { nick => 'sukria', name =>
+'Alexis Sukrieh' });
+is $r->{status}, 202, 'HTTP code is 202';
+is_deeply $r->{content}, {user => { id => 1, name => 'Alexis Sukrieh', nick => 
'sukria'}},
+    "user 1 is updated";
+
+$r = get_response_for_request(PUT => '/user/23', { nick => 'john doe', name =>
+'John Doe' });
+is $r->{status}, 404, 'HTTP code is 404';
+is_deeply $r->{content}->{error}, 'user undef', 'valid content';
+
+$r = get_response_for_request(DELETE => '/user/1');
+is_deeply $r->{content}, {user => { id => 1, name => 'Alexis Sukrieh', nick => 
'sukria'}},
+    "user 1 is deleted";
+is $r->{status}, 202, 'HTTP code is 202';
+
+
+$r = get_response_for_request(GET => '/user/1');
+is $r->{status}, 400, 'HTTP code is 400';
+is_deeply $r->{content}->{error}, 'id is missing', 'valid response';
+
+$r = get_response_for_request(POST => '/user', { name => 'Franck Cuny' });
+is_deeply $r->{content}, { user => { id => 2, name => "Franck Cuny" } },
+    "id is correctly increased";
+is $r->{status}, 201, 'HTTP code is 201';
+

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-perl/packages/libdancer-plugin-rest-perl.git

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits

Reply via email to