summary refs log tree commit diff
path: root/t/spore-request
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-09-13 13:31:56 +0200
committerfranck cuny <franck@lumberjaph.net>2010-09-13 13:31:56 +0200
commit3e3dc478fc9b4eb90681df89156dfcc8f7f81481 (patch)
treeb9788b0d48f524bc4c0aeeb48c744a8f7b097910 /t/spore-request
downloadnet-http-spore-3e3dc478fc9b4eb90681df89156dfcc8f7f81481.tar.gz
initial import
Diffstat (limited to 't/spore-request')
-rw-r--r--t/spore-request/base.t71
-rw-r--r--t/spore-request/exception.t17
-rw-r--r--t/spore-request/finalize.t27
-rw-r--r--t/spore-request/new.t25
-rw-r--r--t/spore-request/path_info.t25
-rw-r--r--t/spore-request/query_string.t25
-rw-r--r--t/spore-request/uri.t109
7 files changed, 299 insertions, 0 deletions
diff --git a/t/spore-request/base.t b/t/spore-request/base.t
new file mode 100644
index 0000000..7ae91e9
--- /dev/null
+++ b/t/spore-request/base.t
@@ -0,0 +1,71 @@
+use strict;
+use warnings;
+
+use Net::HTTP::Spore::Request;
+
+use Test::More;
+
+my @tests = (
+    {
+        host => 'localhost',
+        base => 'http://localhost/'
+    },
+    {
+        script_name => '/foo',
+        host        => 'localhost',
+        base        => 'http://localhost/foo'
+    },
+    {
+        script_name => '/foo bar',
+        host        => 'localhost',
+        base        => 'http://localhost/foo%20bar'
+    },
+    {
+        scheme => 'http',
+        host   => 'localhost:91',
+        base   => 'http://localhost:91/'
+    },
+    {
+        scheme => 'http',
+        host   => 'example.com',
+        base   => 'http://example.com/'
+    },
+    {
+        scheme => 'https',
+        host   => 'example.com',
+        base   => 'https://example.com/'
+    },
+    {
+        scheme      => 'http',
+        server_name => 'example.com',
+        server_port => 80,
+        base        => 'http://example.com/'
+    },
+    {
+        scheme      => 'http',
+        server_name => 'example.com',
+        server_port => 8080,
+        base        => 'http://example.com:8080/'
+    },
+    {
+        host        => 'foobar.com',
+        server_name => 'example.com',
+        server_port => 8080,
+        base        => 'http://foobar.com/'
+    },
+);
+
+plan tests => 1 * @tests;
+
+for my $block (@tests) {
+    my $env = {
+        'spore.url_scheme' => $block->{scheme}      || 'http',
+        HTTP_HOST          => $block->{host}        || undef,
+        SERVER_NAME        => $block->{server_name} || undef,
+        SERVER_PORT        => $block->{server_port} || undef,
+        SCRIPT_NAME        => $block->{script_name} || '',
+    };
+
+    my $req = Net::HTTP::Spore::Request->new($env);
+    is $req->base, $block->{base};
+}
diff --git a/t/spore-request/exception.t b/t/spore-request/exception.t
new file mode 100644
index 0000000..162370a
--- /dev/null
+++ b/t/spore-request/exception.t
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Net::HTTP::Spore;
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json',
+    api_base_url => 'http://localhost:5984' );
+
+$client->enable( 'Test::Response', callback => sub { die } );
+
+my $res = $client->get_all_documents(database => 'test_spore');
+is $res->[0], 599;
+like $res->[2]->{error}, qr/Died/;
+
+done_testing;
diff --git a/t/spore-request/finalize.t b/t/spore-request/finalize.t
new file mode 100644
index 0000000..230c416
--- /dev/null
+++ b/t/spore-request/finalize.t
@@ -0,0 +1,27 @@
+use strict;
+use Test::More;
+
+use Net::HTTP::Spore::Request;
+
+my $env = {
+    REQUEST_METHOD  => 'GET',
+    SERVER_NAME     => 'localhost',
+    SERVER_PORT     => '80',
+    SCRIPT_NAME     => '',
+    PATH_INFO       => '/:database',
+    REQUEST_URI     => '',
+    QUERY_STRING    => '',
+    SERVER_PROTOCOL => 'HTTP/1.0',
+    'spore.params'  => [qw/database test_spore key foo rev 123/],
+};
+
+ok my $request = Net::HTTP::Spore::Request->new($env);
+
+ok my $http_req = $request->finalize();
+isa_ok($http_req, 'HTTP::Request');
+
+is $env->{PATH_INFO}, '/test_spore';
+is $env->{QUERY_STRING}, 'key=foo&rev=123';
+is $http_req->uri->canonical, 'http://localhost/test_spore?key=foo&rev=123';
+
+done_testing;
diff --git a/t/spore-request/new.t b/t/spore-request/new.t
new file mode 100644
index 0000000..6cb9d56
--- /dev/null
+++ b/t/spore-request/new.t
@@ -0,0 +1,25 @@
+use strict;
+use Test::More;
+use Net::HTTP::Spore::Request;
+
+my $req = Net::HTTP::Spore::Request->new(
+    {
+        REQUEST_METHOD    => 'GET',
+        SERVER_PROTOCOL   => 'HTTP/1.1',
+        SERVER_PORT       => 80,
+        SERVER_NAME       => 'example.com',
+        SCRIPT_NAME       => '/foo',
+        REMOTE_ADDR       => '127.0.0.1',
+        'spore.scheme'    => 'http',
+    }
+);
+
+isa_ok( $req, 'Net::HTTP::Spore::Request' );
+
+is( $req->method,   'GET',                    'method' );
+is( $req->protocol, 'HTTP/1.1',               'protocol' );
+is( $req->uri,      'http://example.com/foo', 'uri' );
+is( $req->port,     80,                       'port' );
+is( $req->scheme,   'http',                   'url_scheme' );
+
+done_testing();
diff --git a/t/spore-request/path_info.t b/t/spore-request/path_info.t
new file mode 100644
index 0000000..020a958
--- /dev/null
+++ b/t/spore-request/path_info.t
@@ -0,0 +1,25 @@
+use strict;
+use Test::More;
+
+use Net::HTTP::Spore::Request;
+
+my $env = {
+    REQUEST_METHOD  => 'GET',
+    SERVER_NAME     => 'localhost',
+    SERVER_PORT     => '80',
+    SCRIPT_NAME     => '',
+    PATH_INFO       => '/:database/:key',
+    REQUEST_URI     => '',
+    QUERY_STRING    => '',
+    SERVER_PROTOCOL => 'HTTP/1.0',
+    'spore.params'  => [qw/database test_spore key foo/],
+};
+
+ok my $request = Net::HTTP::Spore::Request->new($env);
+
+is $request->path_info, '/test_spore/foo';
+
+$env->{'spore.params'} = [qw/database test_spore key foo another key/];
+is $request->path_info, '/test_spore/foo';
+
+done_testing;
diff --git a/t/spore-request/query_string.t b/t/spore-request/query_string.t
new file mode 100644
index 0000000..2ee7979
--- /dev/null
+++ b/t/spore-request/query_string.t
@@ -0,0 +1,25 @@
+use strict;
+use Test::More;
+
+use Net::HTTP::Spore::Request;
+
+my $env = {
+    REQUEST_METHOD  => 'GET',
+    SERVER_NAME     => 'localhost',
+    SERVER_PORT     => '80',
+    SCRIPT_NAME     => '',
+    PATH_INFO       => '/:database',
+    REQUEST_URI     => '',
+    QUERY_STRING    => '',
+    SERVER_PROTOCOL => 'HTTP/1.0',
+    'spore.params'  => [qw/database test_spore key foo rev 123/],
+};
+
+ok my $request = Net::HTTP::Spore::Request->new($env);
+
+is $request->query_string, 'key=foo&rev=123';
+
+$env->{PATH_INFO} = '/:database/:key';
+is $request->query_string, 'rev=123';
+
+done_testing;
diff --git a/t/spore-request/uri.t b/t/spore-request/uri.t
new file mode 100644
index 0000000..d3f8b82
--- /dev/null
+++ b/t/spore-request/uri.t
@@ -0,0 +1,109 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Net::HTTP::Spore::Request;
+
+my @tests = (
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => "",
+        },
+        uri        => 'http://example.com/',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => "",
+            PATH_INFO   => "/foo bar",
+        },
+        uri        => 'http://example.com/foo%20bar',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => '/test.c',
+        },
+        uri        => 'http://example.com/test.c',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => '/test.c',
+            PATH_INFO   => '/info',
+        },
+        uri        => 'http://example.com/test.c/info',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST    => 'example.com',
+            SCRIPT_NAME  => '/test',
+            'spore.params' => [qw/dynamic daikuma/],
+        },
+        uri        => 'http://example.com/test?dynamic=daikuma',
+        parameters => { dynamic => 'daikuma' }
+    },
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => '/exec/'
+        },
+        uri        => 'http://example.com/exec/',
+        parameters => {}
+    },
+    {
+        add_env    => { SERVER_NAME => 'example.com' },
+        uri        => 'http://example.com/',
+        parameters => {}
+    },
+    {
+        add_env    => {},
+        uri        => 'http:///',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST    => 'example.com',
+            SCRIPT_NAME  => "",
+            'spore.params' => [qw/aco tie/],
+        },
+        uri        => 'http://example.com/?aco=tie',
+        parameters => { aco => 'tie' }
+    },
+    {
+        add_env => {
+            HTTP_HOST    => 'example.com',
+            SCRIPT_NAME  => "",
+            'spore.params' => [qw/0/],
+        },
+        uri        => 'http://example.com/?0',
+        parameters => {}
+    },
+    {
+        add_env => {
+            HTTP_HOST   => 'example.com',
+            SCRIPT_NAME => "/foo bar",
+            PATH_INFO   => "/baz quux",
+        },
+        uri        => 'http://example.com/foo%20bar/baz%20quux',
+        parameters => {}
+    }
+);
+
+plan tests => 1 * @tests;
+
+for my $block (@tests) {
+    my $env = { SERVER_PORT => 80 };
+    while ( my ( $key, $val ) = each %{ $block->{add_env} || {} } ) {
+        $env->{$key} = $val;
+    }
+    my $req = Net::HTTP::Spore::Request->new($env);
+
+    is $req->uri,                     $block->{uri};
+#    is_deeply $req->query_parameters, $block->{parameters};
+}