summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-11-01 17:05:57 +0100
committerfranck cuny <franck@lumberjaph.net>2010-11-01 17:05:57 +0100
commit489fda6fe372491b88804a85b4f1650cd47a65cd (patch)
tree4307d16b54673c19591b316cc5fa34863f5e70fa
parentrewrite request using moose; not yet finished (diff)
downloadnet-http-spore-489fda6fe372491b88804a85b4f1650cd47a65cd.tar.gz
add some tests
-rw-r--r--t/spore-middleware/auth-oauth.t40
-rw-r--r--t/spore-request/form_data.t27
-rw-r--r--t/spore-request/headers.t37
3 files changed, 104 insertions, 0 deletions
diff --git a/t/spore-middleware/auth-oauth.t b/t/spore-middleware/auth-oauth.t
new file mode 100644
index 0000000..a704e24
--- /dev/null
+++ b/t/spore-middleware/auth-oauth.t
@@ -0,0 +1,40 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+plan tests => 3;
+
+use NET::HTTP::Spore;
+use JSON;
+
+my $api = {
+    base_url => "http://term.ie/oauth/example",
+    name     => "term.ie",
+    methods  => {
+        echo => {
+            path            => "/echo_api.php",
+            method          => "GET",
+            expected_status => [200],
+            authentication => 1,
+        }
+    },
+};
+
+SKIP: {
+    skip "require RUN_HTTP_TEST", 3 unless $ENV{RUN_HTTP_TEST};
+
+    my $client = Net::HTTP::Spore->new_from_string( JSON::encode_json($api) );
+
+    $client->enable(
+        'Auth::OAuth',
+        consumer_key    => 'key',
+        consumer_secret => 'secret',
+        token           => 'accesskey',
+        token_secret    => 'accesssecret',
+    );
+
+    ok my $r = $client->echo(method => 'foo', bar => 'baz');
+    is $r->status, 200;
+    like $r->body, qr/bar=baz&method=foo/;
+}
diff --git a/t/spore-request/form_data.t b/t/spore-request/form_data.t
new file mode 100644
index 0000000..6400465
--- /dev/null
+++ b/t/spore-request/form_data.t
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Net::HTTP::Spore;
+
+my $mock_server = {
+    '/email' => sub {
+        my $req   = shift;
+        my $final = $req->finalize;
+        like $final->header('Content-Type'),
+          qr/multipart\/form-data; boundary=/;
+        $req->new_response( 200, [ 'Content-Type' => 'text/html' ], 'ok' );
+    },
+};
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/api.json',
+    base_url => 'http://localhost' );
+
+$client->enable( 'Mock', tests => $mock_server );
+
+my $res = $client->add_email( email => 'foo@bar.com' );
+is $res->[0],   200;
+like $res->[2], qr/ok/;
+
+done_testing;
diff --git a/t/spore-request/headers.t b/t/spore-request/headers.t
new file mode 100644
index 0000000..1d0e3f5
--- /dev/null
+++ b/t/spore-request/headers.t
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+use Net::HTTP::Spore::Request;
+use Net::HTTP::Spore;
+
+my $env = { 'HTTP_CONTENT_TYPE' => 'text/html', };
+
+ok my $request = Net::HTTP::Spore::Request->new($env);
+
+isa_ok $request->headers, 'HTTP::Headers';
+is $request->header('Content-Type'), 'text/html';
+ok $request->header( 'Content-Type' => 'application/json' );
+is $request->header('Content-Type'), 'application/json';
+
+my $mock_server = {
+    '/file' => sub {
+        my $req   = shift;
+        my $final = $req->finalize;
+        if ( $final->header('Content-Type') eq 'image/png' ) {
+            return $req->new_response( 200, [ 'Content-Type' => 'text/html' ],
+                'ok' );
+        }
+        $req->new_response( 500, [ 'Content-Type' => 'text/html' ], 'nok' );
+    },
+};
+
+ok my $client =
+  Net::HTTP::Spore->new_from_spec( 't/specs/api.json',
+    base_url => 'http://localhost', );
+
+$client->enable( 'Mock', tests => $mock_server );
+
+my $res = $client->attach_file( file => 'foo', 'content_type' => 'image/png' );
+is $res->[0], 200;
+