summary refs log tree commit diff
path: root/t
diff options
context:
space:
mode:
Diffstat (limited to 't')
-rw-r--r--t/specs/couchdb.json77
-rw-r--r--t/spore-method/base.t38
-rw-r--r--t/spore-middleware/auth-basic.t32
-rw-r--r--t/spore-middleware/format-json.t30
-rw-r--r--t/spore-middleware/format-xml.t30
-rw-r--r--t/spore-middleware/format-yaml.t30
-rw-r--r--t/spore-middleware/runtime.t19
-rw-r--r--t/spore-middleware/useragent.t19
-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
-rw-r--r--t/spore-response/body.t21
-rw-r--r--t/spore-response/headers.t20
-rw-r--r--t/spore-response/new.t34
-rw-r--r--t/spore-response/response.t23
19 files changed, 672 insertions, 0 deletions
diff --git a/t/specs/couchdb.json b/t/specs/couchdb.json
new file mode 100644
index 0000000..f7c44b2
--- /dev/null
+++ b/t/specs/couchdb.json
@@ -0,0 +1,77 @@
+{
+   "version" : "0.1",
+   "methods" : {
+      "create_document_without_id" : {
+         "required" : [
+            "database"
+         ],
+         "path" : "/:database",
+         "method" : "POST"
+      },
+      "get_all_documents" : {
+         "params" : [
+            "descending",
+            "startkey",
+            "endkey",
+            "limit",
+            "include_docs"
+         ],
+         "required" : [
+            "database"
+         ],
+         "path" : "/:database/_all_docs",
+         "method" : "GET"
+      },
+      "create_document_with_id" : {
+         "required" : [
+            "database",
+            "doc_id"
+         ],
+         "path" : "/:database/:doc_id",
+         "method" : "POST"
+      },
+      "get_document" : {
+         "params" : [
+            "rev",
+            "revs"
+         ],
+         "required" : [
+            "database",
+            "doc_id"
+         ],
+         "path" : "/:database/:doc_id",
+         "method" : "GET"
+      },
+      "get_all_documents_by_seq" : {
+         "params" : [
+            "startkey",
+            "endkey",
+            "limit",
+            "include_docs"
+         ],
+         "required" : [
+            "database"
+         ],
+         "path" : "/:database/_all_docs_by_seq",
+         "method" : "GET"
+      },
+      "delete_document" : {
+         "params" : [
+            "rev"
+         ],
+         "required" : [
+            "database",
+            "doc_id"
+         ],
+         "path" : "/:database/:doc_id",
+         "method" : "DELETE"
+      }
+   },
+   "api_format" : [
+      "json"
+   ],
+   "name" : "CouchDB",
+   "author" : [
+      "franck cuny <franck@lumberjaph.net>"
+   ]
+}
diff --git a/t/spore-method/base.t b/t/spore-method/base.t
new file mode 100644
index 0000000..5010c38
--- /dev/null
+++ b/t/spore-method/base.t
@@ -0,0 +1,38 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+use Net::HTTP::Spore::Meta::Method;
+
+dies_ok {
+    Net::HTTP::Spore::Meta::Method->wrap(
+        name         => 'test_method',
+        package_name => 'test::api',
+        body         => sub { 1 },
+    );
+}
+"missing some params";
+
+like $@, qr/Attribute \(method\) is required/;
+
+ok my $method = Net::HTTP::Spore::Meta::Method->wrap(
+    name         => 'test_method',
+    package_name => 'test::api',
+    body         => sub { 1 },
+    method       => 'GET',
+    path         => '/user/',
+  ),
+  'method created';
+
+is $method->method, 'GET', 'method is GET';
+
+ok $method = Net::HTTP::Spore::Meta::Method->wrap(
+    name         => 'test_method',
+    package_name => 'test::api',
+    method       => 'GET',
+    path         => '/user/',
+    params       => [qw/name id street/],
+    required     => [qw/name id/],
+);
+
+done_testing;
diff --git a/t/spore-middleware/auth-basic.t b/t/spore-middleware/auth-basic.t
new file mode 100644
index 0000000..92776ba
--- /dev/null
+++ b/t/spore-middleware/auth-basic.t
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+
+use Test::More;
+use MIME::Base64;
+
+use Net::HTTP::Spore;
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json',
+    api_base_url => 'http://localhost:5984' );
+
+my $username = 'franck';
+my $password = 's3kr3t';
+
+$client->enable( 'Auth::Basic', username => $username, password => $password );
+$client->enable(
+    'Test::Response',
+    body    => 'result is ok',
+    headers => [ 'Content-Type' => 'text/html' ]
+);
+
+my $res = $client->get_all_documents( database => 'test_spore' );
+is $res->[0], 200;
+
+my $req = $res->request;
+
+is $req->header('Authorization'),
+  'Basic ' . encode_base64( $username . ':' . $password, '' );
+
+done_testing;
+
diff --git a/t/spore-middleware/format-json.t b/t/spore-middleware/format-json.t
new file mode 100644
index 0000000..3e3b59b
--- /dev/null
+++ b/t/spore-middleware/format-json.t
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+
+use Test::More;
+use JSON;
+
+use Net::HTTP::Spore;
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json',
+    api_base_url => 'http://localhost:5984' );
+
+my $content = { keys => [qw/1 2 3/] };
+
+$client->enable('Format::JSON');
+$client->enable(
+    'Test::Response',
+    body    => JSON::encode_json($content),
+    headers => [ 'Content-Type' => 'application/json' ]
+);
+
+my $res = $client->get_all_documents( database => 'test_spore' );
+is $res->[0],        200;
+is_deeply $res->[2], $content;
+is $res->header('Content-Type'), 'application/json';
+
+my $req = $res->request;
+is $req->header('Accept'), 'application/json';
+
+done_testing;
diff --git a/t/spore-middleware/format-xml.t b/t/spore-middleware/format-xml.t
new file mode 100644
index 0000000..0a01633
--- /dev/null
+++ b/t/spore-middleware/format-xml.t
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+
+use Test::More;
+use XML::Simple;
+
+use Net::HTTP::Spore;
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json',
+    api_base_url => 'http://localhost:5984' );
+
+my $content = { keys => [qw/1 2 3/] };
+
+$client->enable('Format::XML');
+$client->enable(
+    'Test::Response',
+    body    => XMLout($content),
+    headers => [ 'Content-Type' => 'text/xml' ]
+);
+
+my $res = $client->get_all_documents( database => 'test_spore' );
+is $res->[0],        200;
+is_deeply $res->[2], $content;
+is $res->header('Content-Type'), 'text/xml';
+
+my $req = $res->request;
+is $req->header('Accept'), 'text/xml';
+
+done_testing;
diff --git a/t/spore-middleware/format-yaml.t b/t/spore-middleware/format-yaml.t
new file mode 100644
index 0000000..c104cc5
--- /dev/null
+++ b/t/spore-middleware/format-yaml.t
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+
+use Test::More;
+use YAML;
+
+use Net::HTTP::Spore;
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json',
+    api_base_url => 'http://localhost:5984' );
+
+my $content = { keys => [qw/1 2 3/] };
+
+$client->enable('Format::YAML');
+$client->enable(
+    'Test::Response',
+    body    => Dump($content),
+    headers => [ 'Content-Type' => 'text/x-yaml' ]
+);
+
+my $res = $client->get_all_documents( database => 'test_spore' );
+is $res->[0],        200;
+is_deeply $res->[2], $content;
+is $res->header('Content-Type'), 'text/x-yaml';
+
+my $req = $res->request;
+is $req->header('Accept'), 'text/x-yaml';
+
+done_testing;
diff --git a/t/spore-middleware/runtime.t b/t/spore-middleware/runtime.t
new file mode 100644
index 0000000..d6c9b55
--- /dev/null
+++ b/t/spore-middleware/runtime.t
@@ -0,0 +1,19 @@
+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' );
+
+my $ua_str = 'Test::Spore middleware';
+
+$client->enable('Runtime');
+$client->enable('Test::Response');
+
+my $res = $client->get_all_documents(database => 'test_spore');
+ok $res->header('X-Spore-Runtime');
+
+done_testing;
diff --git a/t/spore-middleware/useragent.t b/t/spore-middleware/useragent.t
new file mode 100644
index 0000000..14dc9a6
--- /dev/null
+++ b/t/spore-middleware/useragent.t
@@ -0,0 +1,19 @@
+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' );
+
+my $ua_str = 'Test::Spore middleware';
+
+$client->enable('UserAgent', useragent => $ua_str);
+$client->enable('Test::Response');
+
+my $res = $client->get_all_documents(database => 'test_spore');
+is $res->request->header('User-Agent'), $ua_str;
+
+done_testing;
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};
+}
diff --git a/t/spore-response/body.t b/t/spore-response/body.t
new file mode 100644
index 0000000..2a35d6b
--- /dev/null
+++ b/t/spore-response/body.t
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use Test::More;
+use Net::HTTP::Spore::Response;
+use URI;
+
+sub r($) {
+    my $res = Net::HTTP::Spore::Response->new(200);
+    $res->body(@_);
+    return $res->finalize->[2];
+}
+
+is_deeply r "Hello World", "Hello World";
+is_deeply r [ "Hello", "World" ], [ "Hello", "World" ];
+
+{
+    my $uri = URI->new("foo");    # stringified object
+    is_deeply r $uri, $uri;
+}
+
+done_testing;
diff --git a/t/spore-response/headers.t b/t/spore-response/headers.t
new file mode 100644
index 0000000..b9cf319
--- /dev/null
+++ b/t/spore-response/headers.t
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Net::HTTP::Spore::Response;
+
+my $status = 200;
+my $body = '{"foo":1}';
+my $ct   = 'application/json';
+my $cl   = length($body);
+
+my $response =
+  Net::HTTP::Spore::Response->new( $status,
+    [ 'Content-Type', $ct, 'Content-Length', length($body) ], $body );
+
+is $response->content_type,   $ct;
+is $response->content_length, $cl;
+is $response->status, 200;
+
+done_testing;
diff --git a/t/spore-response/new.t b/t/spore-response/new.t
new file mode 100644
index 0000000..fb271ab
--- /dev/null
+++ b/t/spore-response/new.t
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use Test::More;
+use Net::HTTP::Spore::Response;
+
+{
+    my $res = Net::HTTP::Spore::Response->new(302);
+        is $res->status, 302;
+        is $res->code, 302;
+}
+
+{
+    my $res = Net::HTTP::Spore::Response->new(200, [ 'Content-Type' => 'text/plain' ]);
+        is $res->content_type, 'text/plain';
+}
+
+{
+    my $res = Net::HTTP::Spore::Response->new(200, { 'Content-Type' => 'text/plain' });
+        is $res->content_type, 'text/plain';
+}
+
+{
+    my $res = Net::HTTP::Spore::Response->new(200);
+        $res->content_type('image/png');
+        is $res->content_type, 'image/png';
+}
+
+{
+    my $res = Net::HTTP::Spore::Response->new(200);
+        $res->header('X-Foo' => "bar");
+        is $res->header('X-Foo'), "bar";
+}
+
+done_testing;
diff --git a/t/spore-response/response.t b/t/spore-response/response.t
new file mode 100644
index 0000000..56be6d2
--- /dev/null
+++ b/t/spore-response/response.t
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use Test::More;
+use Net::HTTP::Spore::Response;
+
+sub res {
+    my $res = Net::HTTP::Spore::Response->new;
+    my %v   = @_;
+    while ( my ( $k, $v ) = each %v ) {
+        $res->$k($v);
+    }
+    $res->finalize;
+}
+
+is_deeply(
+    res(
+        status => 200,
+        body   => 'hello',
+    ),
+    [ 200, +[], 'hello' ]
+);
+
+done_testing;