From 6c5dba1f8d4d012cf4a9d1e10fd20094a737b071 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Mon, 25 Oct 2010 17:23:14 +0200 Subject: api spec can be modularized: * add two new methods: new_from_specs and new_from_strings theses two methods can receive more than one description to create a client * add tests and api desc --- t/specs/api2.json | 20 +++++++++++++ t/spore/01_new_from_string.t | 68 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 t/specs/api2.json (limited to 't') diff --git a/t/specs/api2.json b/t/specs/api2.json new file mode 100644 index 0000000..7341405 --- /dev/null +++ b/t/specs/api2.json @@ -0,0 +1,20 @@ +{ + "name": "Test API", + "methods" : { + "get_projects" : { + "authentication": true, + "path" : "/projects/show", + "method" : "GET", + "optional_params" : [ + "name" + ] + }, + "get_project_info" : { + "required_params" : [ + "project" + ], + "path" : "/project/show", + "method" : "GET" + } + } +} diff --git a/t/spore/01_new_from_string.t b/t/spore/01_new_from_string.t index e9d9357..6a21994 100644 --- a/t/spore/01_new_from_string.t +++ b/t/spore/01_new_from_string.t @@ -3,18 +3,39 @@ use warnings; use Test::More; use Test::Exception; -plan tests => 14; +plan tests => 27; +use JSON; use IO::All; use Net::HTTP::Spore; my $api_spec = 't/specs/api.json'; +my $api2_spec = 't/specs/api2.json'; + my %args = ( base_url => 'http://localhost/', ); my $github_spec = "http://github.com/franckcuny/spore/raw/master/services/github.json"; -my $content < io($api_spec); +my $api_ok = { + base_url => "http://services.org/restapi", + methods => { get_info => { method => 'GET', path => '/show' } }, +}; + +my $second_api = { + base_url => "http://services.org/restapi", + methods => { list_users => { method => 'GET', path => '/users' } }, +}; + +my $api_without_path = { + base_url => "http://services.org/restapi", + methods => { get_info => { method => 'GET' } }, +}; + +my $api_without_method = { + base_url => "http://services.org/restapi", + methods => { get_info => { method => 'PET', path => '/show' } }, +}; dies_ok { Net::HTTP::Spore->new_from_spec }; like $@, qr/specification file is missing/; @@ -22,31 +43,50 @@ like $@, qr/specification file is missing/; dies_ok { Net::HTTP::Spore->new_from_spec( "/foo/bar/baz", ) }; like $@, qr/does not exists/; -dies_ok { Net::HTTP::Spore->new_from_spec( $api_spec, ) }; +dies_ok { Net::HTTP::Spore->new_from_spec( $api_spec ) }; like $@, qr/base_url is missing/; ok my $client = Net::HTTP::Spore->new_from_spec( $api_spec, %args ); -ok $client = Net::HTTP::Spore->new_from_string( $content, %args ); +ok $client = + Net::HTTP::Spore->new_from_string( JSON::encode_json($api_ok), %args ); +ok $client->meta->_find_spore_method_by_name(sub{/^get_info$/}); SKIP: { - skip "require RUN_HTTP_TEST", 1 unless $ENV{RUN_HTTP_TEST}; + skip "require RUN_HTTP_TEST", 2 unless $ENV{RUN_HTTP_TEST}; ok $client = Net::HTTP::Spore->new_from_spec( $github_spec, %args ); + ok $client->meta->_find_spore_method_by_name(sub{/^user_search$/}); } dies_ok { - Net::HTTP::Spore->new_from_string( -'{"base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "method" : "GET" } } }' - ); + Net::HTTP::Spore->new_from_string( JSON::encode_json($api_without_path) ); }; like $@, qr/Attribute \(path\) is required/; dies_ok { - Net::HTTP::Spore->new_from_string( -'{"base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "method" : "PET", "path":"/info" } } }' - ); + Net::HTTP::Spore->new_from_string(JSON::encode_json($api_without_method)); }; like $@, qr/Attribute \(method\) does not pass the type constraint/; -ok $client = Net::HTTP::Spore->new_from_string( -'{"base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "path" : "/show", "method" : "GET" } } }' -); +ok $client = Net::HTTP::Spore->new_from_string(JSON::encode_json($api_ok)); +ok $client->meta->_find_spore_method_by_name(sub{/^get_info$/}); + +dies_ok { + Net::HTTP::Spore->new_from_strings('/a/b/c', '/a/b/c'); +}; + +for ( {}, { base_url => 'http://localhost/api' } ) { + ok $client = Net::HTTP::Spore->new_from_strings( JSON::encode_json($api_ok), + JSON::encode_json($second_api), $_ ); + ok $client->meta->_find_spore_method_by_name( sub { /^get_info$/ } ); + ok $client->meta->_find_spore_method_by_name( sub { /^list_users$/ } ); +} + +dies_ok { + $client = Net::HTTP::Spore->new_from_specs($api_spec, $api2_spec); +}; +like $@, qr/base_url is missing/; + +ok $client = + Net::HTTP::Spore->new_from_specs( $api_spec, $api2_spec, + { base_url => 'http://localhost' } ); + -- cgit v1.2.3