diff options
author | franck cuny <franck@lumberjaph.net> | 2009-12-08 16:13:54 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2009-12-08 16:13:54 +0100 |
commit | 100caaf831eb877adecce830fe7f83c7b3d37080 (patch) | |
tree | db03a6d673457919f42feefe1d56eb85e16efb77 /lib/MooseX/Net/API | |
parent | method to deserialize (diff) | |
download | moosex-net-api-100caaf831eb877adecce830fe7f83c7b3d37080.tar.gz |
update tests to use catalyst, bla
Diffstat (limited to '')
-rw-r--r-- | lib/MooseX/Net/API.pm | 49 | ||||
-rw-r--r-- | lib/MooseX/Net/API/Role/CatalystTest.pm | 57 | ||||
-rw-r--r-- | lib/MooseX/Net/API/Test.pm | 46 |
3 files changed, 72 insertions, 80 deletions
diff --git a/lib/MooseX/Net/API.pm b/lib/MooseX/Net/API.pm index a2f663e..6a6d2d7 100644 --- a/lib/MooseX/Net/API.pm +++ b/lib/MooseX/Net/API.pm @@ -10,8 +10,9 @@ use Moose::Exporter; use MooseX::Net::API::Error; use MooseX::Net::API::Meta::Class; use MooseX::Net::API::Meta::Method; -use MooseX::Net::API::Role::Serialize; -use MooseX::Net::API::Role::Deserialize; +with qw/ + MooseX::Net::API::Role::Serialize + MooseX::Net::API::Role::Deserialize/; our $VERSION = '0.02'; @@ -20,12 +21,6 @@ my $list_content_type = { 'yaml' => 'text/x-yaml', 'xml' => 'text/xml', }; -my $reverse_content_type = { - 'application/json' => 'json', - 'application/x-yaml' => 'yaml', - 'text/xml' => 'xml', - 'application/xml' => 'xml', -}; # XXX uri builder # XXX encoding @@ -176,19 +171,22 @@ sub net_api_method { } } + my $path = $options{path}; + # replace all args in the url - while ( $options{path} =~ /\$(\w+)/ ) { + while ( $path =~ /\$(\w+)/ ) { my $match = $1; if ( my $value = delete $args{$match} ) { - $options{path} =~ s/\$$match/$value/; + $path =~ s/\$$match/$value/; } } # XXX improve uri building - my $url = $self->api_base_url . $options{path}; + my $url = $self->api_base_url . $path; my $format = $self->api_format(); $url .= "." . $format if ( $self->api_format_mode() eq 'append' ); my $uri = URI->new($url); + my $res = _request( $self, $format, \%options, $uri, \%args ); my $content_type = $res->headers->{"content-type"}; @@ -217,7 +215,7 @@ sub net_api_method { }; } else { - $code = delete $options{code}; + $code = $options{code}; } $class->add_method( @@ -263,16 +261,15 @@ sub _request { my $req; my $method = $options->{method}; + if ( $method =~ /^(?:GET|DELETE)$/ || $options->{params_in_url} ) { $uri->query_form(%$args); $req = HTTP::Request->new( $method => $uri ); } elsif ( $method =~ /^(?:POST|PUT)$/ ) { $req = HTTP::Request->new( $method => $uri ); - - # XXX proper serialisation - use JSON::XS; - $req->content( encode_json $args ); + my $content = _do_serialization($self, $args, $format); + $req->content( $content ); } else { croak "$method is not defined"; @@ -301,26 +298,6 @@ sub _do_authentication { return $req; } -sub _do_deserialization { - my ( $caller, $raw_content, @content_types ) = @_; - - my $content; - foreach my $deserializer (@content_types) { - my $method; - if ( $reverse_content_type->{$deserializer} ) { - $method = '_from_' . $reverse_content_type->{$deserializer}; - } - else { - $method = '_from_' . $deserializer; - } - next if ( !$caller->meta->find_method_by_name($method) ); - try { - $content = $caller->$method($raw_content); - }; - return $content if $content; - } -} - 1; __END__ diff --git a/lib/MooseX/Net/API/Role/CatalystTest.pm b/lib/MooseX/Net/API/Role/CatalystTest.pm new file mode 100644 index 0000000..d81b956 --- /dev/null +++ b/lib/MooseX/Net/API/Role/CatalystTest.pm @@ -0,0 +1,57 @@ +package MooseX::Net::API::Role::CatalystTest; + +use lib ('t/lib'); +use Moose::Role; with qw/ + MooseX::Net::API::Role::Serialize + MooseX::Net::API::Role::Deserialize/; + +my $list_content_type = { + 'json' => 'application/json', + 'yaml' => 'text/x-yaml', + 'xml' => 'text/xml', +}; + +after qw/test_api_declare/ => sub { + my $caller = shift; + my $name = shift; + my %options = @_; + + if ( $options{catalyst} ) { + my $app = $options{catalyst_app_name}; + + Class::MOP::load_class("HTTP::Request"); + Class::MOP::load_class("Catalyst::Test"); + + Catalyst::Test->import($app); + + my $res = __PACKAGE__->meta->remove_method('_request'); + MooseX::Net::API->meta->add_method( + '_request' => sub { + my ( $class, $format, $options, $uri, $args ) = @_; + my $method = $options->{method}; + + my $res; + if ( $method =~ /^(?:GET|DELETE)$/ + || $options->{params_in_url} ) + { + $uri->query_form(%$args); + my $req = HTTP::Request->new( $method => $uri ); + $req->header( + 'Content-Type' => $list_content_type->{$format} ); + $res = request($req); + } + else { + my $req = HTTP::Request->new( $method => $uri ); + $req->header( + 'Content-Type' => $list_content_type->{$format} ); + my $content = _do_serialization($class, $args, $format); + $req->content( $content ); + $res = request($req); + } + return $res; + } + ); + } +}; + +1; diff --git a/lib/MooseX/Net/API/Test.pm b/lib/MooseX/Net/API/Test.pm index e991b7f..dbd4349 100644 --- a/lib/MooseX/Net/API/Test.pm +++ b/lib/MooseX/Net/API/Test.pm @@ -9,6 +9,8 @@ use Moose::Exporter; use MooseX::Net::API::Meta::Class; use MooseX::Net::API::Meta::Method; +with qw/MooseX::Net::API::Role::CatalystTest/; + Moose::Exporter->setup_import_methods( with_caller => [qw/test_api_method test_api_declare run/] ); @@ -24,14 +26,6 @@ sub init_meta { ); } -my $list_content_type = { - 'json' => 'application/json', - 'yaml' => 'text/x-yaml', - 'xml' => 'text/xml', -}; - -my $tests_count = 0; - sub test_api_declare { my $caller = shift; my $name = shift; @@ -42,42 +36,6 @@ sub test_api_declare { } $api_to_test = $name; - - if ( $options{catalyst} ) { - my $app = $options{catalyst_app_name}; - - Class::MOP::load_class("HTTP::Request"); - Class::MOP::load_class("Catalyst::Test"); - - Catalyst::Test->import($app); - - my $res = __PACKAGE__->meta->remove_method('_request'); - MooseX::Net::API->meta->add_method( - '_request' => sub { - my ( $class, $format, $options, $uri, $args ) = @_; - my $method = $options->{method}; - - my $res; - if ( $method =~ /^(?:GET|DELETE)$/ - || $options->{params_in_url} ) - { - $uri->query_form(%$args); - my $req = HTTP::Request->new( $method => $uri ); - $req->header( - 'Content-Type' => $list_content_type->{$format} ); - $res = request($req); - } - else { - my $req = HTTP::Request->new( $method => $uri ); - $req->header( - 'Content-Type' => $list_content_type->{$format} ); - $req->header( 'Content' => Dump $args); - $res = request($req); - } - return $res; - } - ); - } } sub test_api_method { |