summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-19 21:59:07 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-19 22:02:33 +0200
commitaa41217a463e1bc0ba21bb3e4c8b36e87bfad99a (patch)
tree0fe6374e6c076f6a0fb29a22aa735f8acdff7b72
parentupdate tests to use the dummy api, and replace api_base_url with base_url (diff)
downloadnet-http-spore-aa41217a463e1bc0ba21bb3e4c8b36e87bfad99a.tar.gz
new middlewares:
* fileupload: upload a file (will detect mime type to set appropriate headers)
* paranoidagent: handle blacklist and whitelist
* redirection: follows HTTP redirection (HTTP status 30*)
* test for redirection
-rw-r--r--lib/Net/HTTP/Spore/Middleware/FileUpload.pm12
-rw-r--r--lib/Net/HTTP/Spore/Middleware/ParanoidAgent.pm13
-rw-r--r--lib/Net/HTTP/Spore/Middleware/Redirection.pm44
-rw-r--r--t/spore-middleware/redirection.t27
4 files changed, 96 insertions, 0 deletions
diff --git a/lib/Net/HTTP/Spore/Middleware/FileUpload.pm b/lib/Net/HTTP/Spore/Middleware/FileUpload.pm
new file mode 100644
index 0000000..6677e54
--- /dev/null
+++ b/lib/Net/HTTP/Spore/Middleware/FileUpload.pm
@@ -0,0 +1,12 @@
+package Net::HTTP::Spore::Middleware::FileUpload;
+
+use Moose;
+extends 'Net::HTTP::Spore::Middleware';
+
+use LWP::MediaTypes qw/read_media_types/;
+
+sub call {
+    my ($self, $request) = @_;
+}
+
+1;
diff --git a/lib/Net/HTTP/Spore/Middleware/ParanoidAgent.pm b/lib/Net/HTTP/Spore/Middleware/ParanoidAgent.pm
new file mode 100644
index 0000000..c1cce0c
--- /dev/null
+++ b/lib/Net/HTTP/Spore/Middleware/ParanoidAgent.pm
@@ -0,0 +1,13 @@
+package Net::HTTP::Spore::Middleware::ParanoidAgent;
+
+use Moose;
+extends 'Net::HTTP::Spore::Middleware';
+
+has black_list => ();
+has white_list => ();
+
+sub call {
+    my ($self, $request) = @_;
+}
+
+1;
diff --git a/lib/Net/HTTP/Spore/Middleware/Redirection.pm b/lib/Net/HTTP/Spore/Middleware/Redirection.pm
new file mode 100644
index 0000000..07046d8
--- /dev/null
+++ b/lib/Net/HTTP/Spore/Middleware/Redirection.pm
@@ -0,0 +1,44 @@
+package Net::HTTP::Spore::Middleware::Redirection;
+
+use Moose;
+
+extends 'Net::HTTP::Spore::Middleware';
+
+with 'Net::HTTP::Spore::Role::Request', 'Net::HTTP::Spore::Role::UserAgent';
+
+has max_redirect => ( is => 'rw', isa => 'Int', lazy => 1, default => 5 );
+
+sub call {
+    my ( $self, $req ) = @_;
+
+    my $nredirect = 0;
+
+    return $self->response_cb(
+        sub {
+            my $res      = shift;
+            while ( $nredirect < $self->max_redirect ) {
+                my $location = $res->header('location');
+                my $status   = $res->status;
+                if (
+                    $location
+                    and (  $status == 301
+                        or $status == 302
+                        or $status == 303
+                        or $status == 307 )
+                  )
+                {
+                    my $uri = URI->new($location);
+                    $req->env->{HTTP_HOST} = $uri->host;
+                    $req->env->{PATH_INFO} = $uri->path;
+                    $res = $self->_request($req);
+                    $nredirect++;
+                }else{
+                    last;
+                }
+            }
+            return $res;
+        }
+    );
+}
+
+1;
diff --git a/t/spore-middleware/redirection.t b/t/spore-middleware/redirection.t
new file mode 100644
index 0000000..0084956
--- /dev/null
+++ b/t/spore-middleware/redirection.t
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+plan tests => 2;
+
+use Net::HTTP::Spore;
+
+SKIP: {
+    skip "require RUN_HTTP_TEST", 2 unless $ENV{RUN_HTTP_TEST};
+    my $client = Net::HTTP::Spore->new_from_string(
+        '{
+    "base_url" : "http://fperrad.googlepages.com",
+      "name"   : "googlepages",
+      "methods"
+      : { "get_home"
+        : { "path" : "/home", "method" : "GET", "expected_status" : [200] } }
+    }');
+
+    $client->enable(' Redirection ');
+
+    my $r = $client->get_home();
+    is $r->status, 200;
+    is $r->request->uri,
+      'http://sites.google.com/site/fperrad/home ';
+}