about summary refs log tree commit diff
path: root/t
diff options
context:
space:
mode:
Diffstat (limited to 't')
-rw-r--r--t/01_basic.t102
-rw-r--r--t/02_error.t64
-rw-r--r--t/03_serialization.t13
-rw-r--r--t/04_apimethod.t36
-rw-r--r--t/05_authentication.t67
-rw-r--r--t/lib/FakeAPI.pm82
-rw-r--r--t/lib/TestAPI.pm49
7 files changed, 252 insertions, 161 deletions
diff --git a/t/01_basic.t b/t/01_basic.t
index e52f0e1..d21e930 100644
--- a/t/01_basic.t
+++ b/t/01_basic.t
@@ -1,38 +1,96 @@
 use strict;
 use warnings;
+
 use Test::More;
 use Test::Exception;
+
 use lib ('t/lib');
-use FakeAPI;
 
-my $obj = FakeAPI->new;
-ok $obj, "... object created";
-ok $obj->meta->has_attribute('api_useragent'),
-    "... useragent attribute have been added";
+use TestAPI;
+
+ok my $api = TestAPI->new(), 'api object created';
+
+for my $role (qw/UserAgent Format Authentication Serialization Request/) {
+    ok $api->meta->does_role('MooseX::Net::API::Role::' . $role),
+      'does role ' . $role;
+}
+
+# test fetch list of users
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        is $request->method, 'GET', 'GET request';
+        my $res = HTTP::Response->new(200);
+        $res->content('[{"name":"eris"}]');
+        $res;
+    }
+);
 
-ok my $method = $obj->meta->find_method_by_name('get_user'),
-    '... method get_user have been created';
+ok my ($content, $res) = $api->users(), 'api call success';
+is $res->code, 200, 'http code as expected';
+is_deeply $content, [{name => 'eris'}], 'got a list of users';
 
-ok $method->meta->has_attribute('path'), '... method bar have attribute path';
-is $method->path, '/user/$id', '... get good path value';
+# test fetch list of one user
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        is $request->method, 'GET', 'GET request';
+        is $request->uri, 'http://exemple.com/user/eris.json',
+          'valid url generated';
+        my $res = HTTP::Response->new(200);
+        $res->content('{"name":"eris"}');
+        $res;
+    }
+);
 
-ok my @methods = $obj->meta->local_api_methods(), '... get api methods';
-is scalar @methods, 6, '... get 6 methods in our API';
+ok $content = $api->user(user_name => 'eris'), 'api call success';
+is_deeply $content, {name => 'eris'}, 'valid user content';
 
-ok my $users = $obj->users(), "... get users list";
-is $users->{status}, 1, "... get users";
+# test to create a user
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        is $request->method, 'POST', 'POST request';
+        is $request->content,
+          JSON::encode_json({name => 'eris', dob => '01/02/1900'}),
+          'got valid content in POST';
+        my $res = HTTP::Response->new(201);
+        $res->content('{"status":"ok"}');
+        $res;
+    }
+);
 
-ok my $user = $obj->get_user( id => 1 ), "... fetch user";
-is $user->{status}, 1, "... get bruce wayne";
+($content, $res) = $api->add_user(name => 'eris', dob => '01/02/1900');
+ok $content, 'got content';
+is $res->code, 201, 'code as expected';
 
-ok my ($user, $http_response) = $obj->get_user(id => 1), "... fetch user";
-isa_ok $http_response, "HTTP::Response", "... got the HTTP response object";
+# test to update a user
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        my $res     = HTTP::Response->new(201);
+        $res->content('{"status":"ok"}');
+        $res;
+    }
+);
 
-#dies_ok { $obj->get_user( id => 12 ) } "... can't fetch unknown user";
-#my $err = $@;
-#is $err->http_code, 404, "... get 404";
+($content, $res) = $api->update_user(name => 'eris', dob => '02/01/1900');
+ok $content, 'got content after update';
+is $res->code, 201, 'code as expected';
 
-#my $auth_obj = FakeAPI->new();
-#my $res = $auth_obj->auth_get_user(id => 1);
+# test to delete a user
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub{
+        my $request = shift;
+        my $res = HTTP::Response->new(204);
+        $res;
+    }
+);
 
+($content, $res) = $api->delete_user(name => 'eris');
+is $res->code, 204, 'code as expected';
 done_testing;
diff --git a/t/02_error.t b/t/02_error.t
index 332538a..3ab5dcc 100644
--- a/t/02_error.t
+++ b/t/02_error.t
@@ -3,65 +3,15 @@ use warnings;
 use Test::More;
 use Test::Exception;
 
-BEGIN {
-    dies_ok {
-        {
+package test::api::missing_api_base_url;
+use MooseX::Net::API;
 
-            package net_api_fail;
-            use Moose;
-            use MooseX::Net::API;
-            net_api_declare foo => ();
-        }
-    }
-    "... format is missing";
-    like $@, qr/format is missing in your api declaration/,
-        "... format is missing";
-    dies_ok {
-        {
+net_api_method user => (method => 'GET', path => '/user/');
 
-            package net_api_fail;
-            use Moose;
-            use MooseX::Net::API;
-            net_api_declare foo => ( format => 'foo' );
-        }
-    }
-    "... no valid format";
-    like $@, qr/format is not recognised/, "... no valid format";
-    dies_ok {
-        {
+package main;
 
-            package net_api_fail;
-            use Moose;
-            use MooseX::Net::API;
-            net_api_declare foo => ( format => 'json' );
-        }
-    }
-    "... format mode is not set";
-    like $@, qr/format_mode is not set/, "... format mode is not set";
-    dies_ok {
-        {
-
-            package net_api_fail;
-            use Moose;
-            use MooseX::Net::API;
-            net_api_declare foo => ( format => 'json', format_mode => 'bar' );
-        }
-    }
-    "... format mode is unvalid";
-    like $@, qr/must be append or content-type/, "... format mode is unvalid";
-    #dies_ok {
-        #{
-            #package net_api_fail;
-            #use Moose;
-            #use MooseX::Net::API;
-            #net_api_declare foo => (
-                #format      => 'json',
-                #format_mode => 'content-type'
-            #);
-        #}
-    #}
-    #"... bad useragent";
-    #warn $@;
-}
+ok my $t = test::api::missing_api_base_url->new;
+dies_ok { $t->user } 'die with missing url';
+like $@, qr/'api_base_url' have not been defined/, 'missing api_base_url';
 
 done_testing;
diff --git a/t/03_serialization.t b/t/03_serialization.t
new file mode 100644
index 0000000..5fa45c2
--- /dev/null
+++ b/t/03_serialization.t
@@ -0,0 +1,13 @@
+use strict;
+use warnings;
+use Test::More;
+
+use MooseX::Net::API::Parser::XML;
+use MooseX::Net::API::Parser::JSON;
+use MooseX::Net::API::Parser::YAML;
+
+ok my $xml_parser = MooseX::Net::API::Parser::XML->new();
+ok my $yaml_parser = MooseX::Net::API::Parser::YAML->new();
+ok my $json_parser = MooseX::Net::API::Parser::JSON->new();
+
+done_testing;
diff --git a/t/04_apimethod.t b/t/04_apimethod.t
new file mode 100644
index 0000000..cac2715
--- /dev/null
+++ b/t/04_apimethod.t
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+use MooseX::Net::API::Meta::Method;
+
+dies_ok {
+    MooseX::Net::API::Meta::Method->wrap(
+        name         => 'test_method',
+        package_name => 'test::api',
+        body         => sub {1},
+    );
+}
+"missing some params";
+
+ok my $method = MooseX::Net::API::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 = MooseX::Net::API::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/05_authentication.t b/t/05_authentication.t
new file mode 100644
index 0000000..e769a53
--- /dev/null
+++ b/t/05_authentication.t
@@ -0,0 +1,67 @@
+use strict;
+use warnings;
+use Test::More;
+
+package test::auth;
+use MooseX::Net::API;
+
+net_api_declare fake_auth => (
+    api_base_url          => 'http://localhost',
+    format                => 'json',
+    authentication        => 1,
+    authentication_method => 'my_auth',
+);
+
+net_api_method user => (
+    method => 'GET',
+    path   => '/user/',
+);
+
+sub my_auth {
+    my ($self, $request, $ua, $h) = @_;
+    $request->header('Authentication' => 1);
+}
+
+package test::auth::simple;
+use MooseX::Net::API;
+
+net_api_declare fake_auth => (
+    api_base_url          => 'http://localhost',
+    format                => 'json',
+    authentication        => 1,
+);
+
+net_api_method user => (
+    method => 'GET',
+    path   => '/user/',
+);
+
+package main;
+
+ok my $api = test::auth->new, 'object api created';
+$api->api_useragent->add_handler(
+    request_send => sub {
+        my $request = shift;
+        is $request->header('Authentication'), 1, 'authentication header is set';
+        my $res = HTTP::Response->new(200);
+        $res->content('[{"name":"eris"}]');
+        $res;
+    }
+);
+ok $api->user, 'method user success';
+
+ok $api =
+  test::auth::simple->new(api_username => 'foo', api_password => 'bar'),
+  'object api simple created';
+$api->api_useragent->add_handler(
+    request_send => sub {
+        my $request = shift;
+        ok $request->header('authorization'), 'authentication header is set';
+        my $res = HTTP::Response->new(200);
+        $res->content('[{"name":"eris"}]');
+        $res;
+    }
+);
+ok $api->user, 'method user success';
+
+done_testing;
diff --git a/t/lib/FakeAPI.pm b/t/lib/FakeAPI.pm
deleted file mode 100644
index 0f53157..0000000
--- a/t/lib/FakeAPI.pm
+++ /dev/null
@@ -1,82 +0,0 @@
-package FakeAPI;
-use Moose;
-use MooseX::Net::API;
-use LWP::UserAgent;
-use HTTP::Response;
-use JSON::XS;
-
-net_api_declare demorest => (
-    base_url => "http://example.com/",
-    format         => 'json',
-    format_mode    => 'content-type',
-    authentication => 0,
-    username       => 'foo',
-    password       => 'bar',
-    useragent      => sub {
-        my ($self) = @_;
-        my $ua = LWP::UserAgent->new();
-        $ua->add_handler(
-            request_send => sub {
-                my $request = shift;
-                my $res = HTTP::Response->new(200, 'OK');
-                $res->header('content-type' => 'application/json');
-                $res->content(encode_json {status => 1});
-                return $res;
-            }
-        );
-        return $ua;
-    },
-);
-
-net_api_method users => (
-    description => 'get a list of users',
-    method      => 'GET',
-    path        => '/users/',
-    expected    => [qw/200/],
-);
-
-net_api_method get_user => (
-    description => 'fetch information about a specific user',
-    method      => 'GET',
-    path        => '/user/$id',
-    params      => [qw/id/],
-    required    => [qw/id/],
-    expected    => [qw/200 404/],
-);
-
-net_api_method create_user => (
-    description => 'create a new user',
-    method      => 'POST',
-    path        => '/user/',
-    params      => [qw/user nickname/],
-    required    => [qw/user nickname/],
-);
-
-net_api_method update_user => (
-    description => 'update information about a specific user',
-    method      => 'PUT',
-    path        => '/user/$id',
-    params      => [qw/id nickname/],
-    required    => [qw/id nickname/],
-);
-
-net_api_method delete_user => (
-    description => 'terminate an user',
-    method      => 'DELETE',
-    path        => '/user/$id',
-    params      => [qw/id/],
-    required    => [qw/id/],
-);
-
-net_api_method auth_get_user => (
-    description =>
-        'fetch information about a specific user with authentication',
-    method         => 'GET',
-    path           => '/auth_user/$id',
-    params         => [qw/id/],
-    required       => [qw/id/],
-    expected       => [qw/200 404/],
-    authentication => 1,
-);
-
-1;
diff --git a/t/lib/TestAPI.pm b/t/lib/TestAPI.pm
new file mode 100644
index 0000000..1e8bf97
--- /dev/null
+++ b/t/lib/TestAPI.pm
@@ -0,0 +1,49 @@
+package TestAPI;
+use MooseX::Net::API;
+
+use HTTP::Response;
+
+net_api_declare fake_api => (
+    api_base_url => 'http://exemple.com',
+    format       => 'json',
+);
+
+net_api_method users => (
+    method   => 'GET',
+    path     => '/users/',
+    expected => [qw/200/],
+);
+
+net_api_method user => (
+    method   => 'GET',
+    path     => '/user/:user_name',
+    params   => [qw/user_name/],
+    required => [qw/user_name/],
+    expected => [qw/200/],
+);
+
+net_api_method add_user => (
+    method   => 'POST',
+    path     => '/user/',
+    params   => [qw/name dob/],
+    required => [qw/name/],
+    expected => [qw/201/],
+);
+
+net_api_method update_user => (
+    method   => 'PUT',
+    path     => '/user/:name',
+    params   => [qw/name dob/],
+    required => [qw/name/],
+    expected => [qw/201/],
+);
+
+net_api_method delete_user => (
+    method   => 'DELETE',
+    path     => '/user/:name',
+    params   => [qw/name/],
+    required => [qw/name/],
+    expected => [qw/204/],
+);
+
+1;