[Jifty-commit] r4509 - in Net-Jifty: . lib/Net t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Nov 20 17:29:03 EST 2007


Author: sartak
Date: Tue Nov 20 17:29:02 2007
New Revision: 4509

Added:
   Net-Jifty/t/002-method.t
   Net-Jifty/t/003-crud.t
Modified:
   Net-Jifty/   (props changed)
   Net-Jifty/lib/Net/Jifty.pm
   Net-Jifty/t/001-login.t
   Net-Jifty/t/lib/Net/Jifty/Test.pm

Log:
 r45432 at onn:  sartak | 2007-11-20 17:28:57 -0500
 More tests, time to cut an 0.01


Modified: Net-Jifty/lib/Net/Jifty.pm
==============================================================================
--- Net-Jifty/lib/Net/Jifty.pm	(original)
+++ Net-Jifty/lib/Net/Jifty.pm	Tue Nov 20 17:29:02 2007
@@ -193,7 +193,7 @@
     my $self   = shift;
     my $method = lc(shift);
     my $url    = shift;
-    my $args   = shift;
+    my %args   = @_;
 
     $url = $self->join_url(@$url)
         if ref($url) eq 'ARRAY';
@@ -204,21 +204,23 @@
     my $res;
 
     if ($method eq 'get' || $method eq 'head') {
-        my $uri = $self->site . '/=/' . $url . '.yml?';
+        my $uri = $self->site . '/=/' . $url . '.yml';
 
-        while (my ($key, $value) = each %$args) {
-            $uri .= '&' . join '=', map { $self->escape($_) } $key, $value;
+        if (keys %args) {
+            $uri .= '?';
+            while (my ($key, $value) = each %args) {
+                $uri .= '&' . join '=', map { $self->escape($_) } $key, $value;
+            }
+            # it's easier than keeping a flag of "did we already append?"
+            $uri =~ s/\?&/?/;
         }
 
-        # it's easier than keeping a flag of "did we already append?"
-        $uri =~ s/\?&/?/;
-
         $res = $self->ua->$method($uri);
     }
     else {
         $res = $self->ua->$method(
             $self->site . '/=/' . $url . '.yml',
-            $args
+            \%args
         );
     }
 
@@ -262,9 +264,8 @@
 sub act {
     my $self   = shift;
     my $action = $self->canonicalize_action(shift);
-    my %args   = @_;
 
-    return $self->post(["action", $action], \%args);
+    return $self->post(["action", $action], @_);
 }
 
 =head2 create Model, FIELDS
@@ -276,9 +277,8 @@
 sub create {
     my $self = shift;
     my $model = $self->canonicalize_model(shift);
-    my %fields = @_;
 
-    return $self->post(["model", $model], \%fields);
+    return $self->post(["model", $model], @_);
 }
 
 =head2 delete Model, Key => Value
@@ -307,9 +307,8 @@
     my $model  = $self->canonicalize_model(shift);
     my $key    = shift;
     my $value  = shift;
-    my %fields = @_;
 
-    return $self->method(put => ["model", $model, $key, $value], \%fields);
+    return $self->method(put => ["model", $model, $key, $value], @_);
 }
 
 =head2 read Model, Key => Value

Modified: Net-Jifty/t/001-login.t
==============================================================================
--- Net-Jifty/t/001-login.t	(original)
+++ Net-Jifty/t/001-login.t	Tue Nov 20 17:29:02 2007
@@ -1,7 +1,7 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
-use Test::More tests => 3;
+use Test::More tests => 6;
 use lib 't/lib';
 use Net::Jifty::Test;
 
@@ -17,4 +17,14 @@
 };
 
 is_deeply($args->[2], $login, "correct login arguments");
+is($j->sid, 'deadbeef', "get_sid was called");
+
+# make sure we don't try to log in if we've already got a SID
+
+$j = Net::Jifty::Test->new(sid => "ababa");
+($name, $args) = $j->ua->next_call();
+is($name, "cookie_jar", "didn't call post, but went right to cookie_jar");
+
+($name, $args) = $j->ua->next_call();
+is($name, undef, "no other methods called");
 

Added: Net-Jifty/t/002-method.t
==============================================================================
--- (empty file)
+++ Net-Jifty/t/002-method.t	Tue Nov 20 17:29:02 2007
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 18;
+use lib 't/lib';
+use Net::Jifty::Test;
+
+my $j = Net::Jifty::Test->new();
+$j->ua->clear();
+
+$j->get("ping");
+my ($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/ping.yml', 'correct URL');
+
+$j->ua->clear;
+$j->get([qw/foo bar/]);
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar.yml', 'correct URL with array-ref');
+
+$j->ua->clear;
+$j->get("foo/bar");
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar.yml', 'correct URL with internal /');
+
+$j->ua->clear;
+$j->get("foo/bar?baz=quux");
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar?baz=quux.yml', "correct URL. shouldn't try to pass arguments yourself");
+
+$j->ua->clear;
+$j->get([qw{foo bar ?baz =quux}]);
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar/%3Fbaz/%3Dquux.yml', 'URL is properly escaped when passed in as an array ref');
+
+$j->ua->clear;
+$j->get([qw{foo bar ?baz =quux}]);
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar/%3Fbaz/%3Dquux.yml', 'URL is properly escaped when passed in as an array ref');
+
+$j->ua->clear;
+$j->get([qw{foo bar ?baz =quux}], arg => 1);
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar/%3Fbaz/%3Dquux.yml?arg=1', '"get" query parameters work');
+
+$j->ua->clear;
+$j->get([qw{foo bar ?baz =quux}], "?-?" => "=`=");
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/foo/bar/%3Fbaz/%3Dquux.yml?%3F-%3F=%3D%60%3D', '"get" query parameters properly escaped');
+
+$j->ua->clear;
+$j->get(["\x{2668}"], "\x{2668}" => "\x{2668}");
+($name, $args) = $j->ua->next_call();
+is($name, 'get', 'ua->get method called');
+is($args->[1], 'http://jifty.org/=/%E2%99%A8.yml?%E2%99%A8=%E2%99%A8', '"get" query parameters properly encoded and escaped');

Added: Net-Jifty/t/003-crud.t
==============================================================================
--- (empty file)
+++ Net-Jifty/t/003-crud.t	Tue Nov 20 17:29:02 2007
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 13;
+use lib 't/lib';
+use Net::Jifty::Test;
+
+my $j = Net::Jifty::Test->new();
+$j->ua->clear();
+
+my %args = (
+    a => 'b',
+    c => 'd',
+);
+
+$j->create("Foo", %args);
+my ($name, $args) = $j->ua->next_call;
+is($name, 'post', 'used post for create');
+is_deeply($args->[1], 'http://jifty.org/=/model/JiftyApp.Model.Foo.yml', 'correct URL');
+is_deeply($args->[2], \%args, 'correct arguments');
+
+$j->ua->clear;
+$j->read("Foo", a => 'b');
+($name, $args) = $j->ua->next_call;
+is($name, 'get', 'used get for read');
+is_deeply($args->[1], 'http://jifty.org/=/model/JiftyApp.Model.Foo/a/b.yml', 'correct URL');
+
+$j->ua->clear;
+$j->update("Foo", a => 'b', c => 'C', d => 'e');
+($name, $args) = $j->ua->next_call;
+is($name, 'put', 'used put for update');
+is_deeply($args->[1], 'http://jifty.org/=/model/JiftyApp.Model.Foo/a/b.yml', 'correct URL');
+is_deeply($args->[2], {c => 'C', d => 'e'}, 'correct arguments');
+
+$j->ua->clear;
+$j->delete("Foo", '"' => '?');
+($name, $args) = $j->ua->next_call;
+is($name, 'delete', 'used delete for delete');
+is_deeply($args->[1], 'http://jifty.org/=/model/JiftyApp.Model.Foo/%22/%3F.yml', 'correct URL');
+
+$j->ua->clear;
+$j->act("Foo", '"' => '?');
+($name, $args) = $j->ua->next_call;
+is($name, 'post', 'used post for act');
+is_deeply($args->[1], 'http://jifty.org/=/action/JiftyApp.Action.Foo.yml', 'correct URL');
+is_deeply($args->[2], {'"' => '?'}, 'correct arguments');
+

Modified: Net-Jifty/t/lib/Net/Jifty/Test.pm
==============================================================================
--- Net-Jifty/t/lib/Net/Jifty/Test.pm	(original)
+++ Net-Jifty/t/lib/Net/Jifty/Test.pm	Tue Nov 20 17:29:02 2007
@@ -73,5 +73,10 @@
     default => 'password',
 );
 
+# and override some methods
+sub get_sid {
+    shift->sid("deadbeef");
+}
+
 1;
 


More information about the Jifty-commit mailing list