Merge pull request #756 from wing328/perl_improve_accept_header

[Perl] better logic for accept and content-type, added test cases
This commit is contained in:
Tony Tam 2015-05-18 06:36:11 -07:00
commit e068ad4494
7 changed files with 165 additions and 85 deletions

View File

@ -253,4 +253,39 @@ sub deserialize
}
# return 'Accept' based on an array of accept provided
# @param [Array] header_accept_array Array fo 'Accept'
# @return String Accept (e.g. application/json)
sub select_header_accept
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return undef;
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}
}
# return the content type based on an array of content-type provided
# @param [Array] content_type_array Array fo content-type
# @return String Content-Type (e.g. application/json)
sub select_header_content_type
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return 'application/json'; # default to application/json
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}
}
1;

View File

@ -85,12 +85,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = '{{#produces}}{{mediaType}}{{#hasMore}}, {{/hasMore}}{{/produces}}';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept({{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}});
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ({{#consumes}}'{{mediaType}}'{{#hasMore}},{{/hasMore}}{{/consumes}});
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type({{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}});
{{#queryParams}} # query params
if ( exists $args{'{{paramName}}'}) {

View File

@ -253,4 +253,39 @@ sub deserialize
}
# return 'Accept' based on an array of accept provided
# @param [Array] header_accept_array Array fo 'Accept'
# @return String Accept (e.g. application/json)
sub select_header_accept
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return undef;
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}
}
# return the content type based on an array of content-type provided
# @param [Array] content_type_array Array fo content-type
# @return String Content-Type (e.g. application/json)
sub select_header_content_type
{
my ($self, @header) = @_;
if (@header == 0 || (@header == 1 && $header[0] eq '')) {
return 'application/json'; # default to application/json
} elsif (grep(/^application\/json$/i, @header)) {
return 'application/json';
} else {
return join(',', @header);
}
}
1;

View File

@ -86,12 +86,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ('application/json','application/xml',);
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', );
@ -137,12 +137,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ('application/json','application/xml',);
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/json', 'application/xml', );
@ -188,12 +188,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
if ( exists $args{'status'}) {
@ -242,12 +242,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
if ( exists $args{'tags'}) {
@ -301,12 +301,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -364,12 +364,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ('application/x-www-form-urlencoded',);
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('application/x-www-form-urlencoded', );
@ -433,12 +433,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# header params
@ -496,12 +496,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ('multipart/form-data',);
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type('multipart/form-data', );

View File

@ -81,12 +81,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -132,12 +132,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -191,12 +191,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -252,12 +252,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();

View File

@ -86,12 +86,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -137,12 +137,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -188,12 +188,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -240,12 +240,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
# query params
if ( exists $args{'username'}) {
@ -296,12 +296,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -349,12 +349,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -411,12 +411,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();
@ -472,12 +472,12 @@ sub new {
my $header_params = {};
my $form_params = {};
my $_header_accept = 'application/json, application/xml';
if ($_header_accept ne '') {
# 'Accept' and 'Content-Type' header
my $_header_accept = $self->{api_client}->select_header_accept('application/json', 'application/xml');
if ($_header_accept) {
$header_params->{'Accept'} = $_header_accept;
}
my @_header_content_type = ();
$header_params->{'Content-Type'} = scalar(@_header_content_type) > 0 ? $_header_content_type[0] : 'application/json';
$header_params->{'Content-Type'} = $self->{api_client}->select_header_content_type();

View File

@ -1,4 +1,4 @@
use Test::More tests => 25;
use Test::More tests => 31;
use Test::Exception;
use lib 'lib';
@ -12,6 +12,16 @@ use_ok('WWW::SwaggerClient::Object::Tag');
use_ok('WWW::SwaggerClient::Object::Category');
my $api = WWW::SwaggerClient::PetApi->new();
# test select_header_content_type
is $api->{api_client}->select_header_content_type('application/xml', 'Application/JSON'), 'application/json', 'get the proper content type application/json but not application/xml';
is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper content type application/json';
is $api->{api_client}->select_header_content_type(''), 'application/json', 'get the proper content type application/json (default)';
# test select_header_accept
is $api->{api_client}->select_header_accept('application/xml', 'Application/JSON'), 'application/json', 'get the proper accept application/json but not application/xml';
is $api->{api_client}->select_header_content_type('application/xml'), 'application/xml', 'get the proper accept application/json';
is $api->{api_client}->select_header_accept(''), undef, 'get the proper accept "undef" (default)';
my $pet_id = 10008;
my $category = WWW::SwaggerClient::Object::Category->new('id' => '22', 'name' => 'perl');