summary refs log tree commit diff
path: root/t/spore-middleware
diff options
context:
space:
mode:
Diffstat (limited to '')
-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
6 files changed, 160 insertions, 0 deletions
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;