2015-04-17 00:28:38 +00:00
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'google/apis/core/api_command'
require 'google/apis/core/json_representation'
2016-09-08 20:40:30 +00:00
RSpec . describe Google :: Apis :: Core :: ApiCommand do
2015-04-17 00:28:38 +00:00
include TestHelpers
include_context 'HTTP client'
let ( :model_class ) do
Class . new do
attr_accessor :value
end
end
let ( :representer_class ) do
Class . new ( Google :: Apis :: Core :: JsonRepresentation ) do
property :value
end
end
context ( 'with a request body' ) do
let ( :command ) do
request = model_class . new
request . value = 'hello'
command = Google :: Apis :: Core :: ApiCommand . new ( :post , 'https://www.googleapis.com/zoo/animals' )
command . request_representation = representer_class
command . request_object = request
2016-09-08 20:40:30 +00:00
command . query [ 'a' ] = 'b'
2015-04-17 00:28:38 +00:00
command
end
before ( :example ) do
2016-09-08 20:40:30 +00:00
stub_request ( :post , 'https://www.googleapis.com/zoo/animals?a=b' )
2015-04-17 00:28:38 +00:00
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {} ) )
end
it 'should serialize the request object' do
command . execute ( client )
2016-09-08 20:40:30 +00:00
expect ( a_request ( :post , 'https://www.googleapis.com/zoo/animals?a=b' ) . with do | req |
2015-04-17 00:28:38 +00:00
be_json_eql ( %( {"value":"hello"} ) ) . matches? ( req . body )
end ) . to have_been_made
end
2016-09-08 20:40:30 +00:00
it 'should not form encode query parameters when body expected but nil' do
command . query [ 'a' ] = 'b'
command . request_object = nil
command . execute ( client )
expect ( a_request ( :post , 'https://www.googleapis.com/zoo/animals?a=b' ) . with ( body : nil ) ) . to have_been_made
end
2015-04-17 00:28:38 +00:00
end
2017-03-31 22:26:51 +00:00
context ( 'with a raw request body' ) do
let ( :command ) do
request = model_class . new
command = Google :: Apis :: Core :: ApiCommand . new ( :post , 'https://www.googleapis.com/zoo/animals' )
command . request_representation = representer_class
command . request_object = %( {"value": "hello"} )
command . options . skip_serialization = true
command
end
before ( :example ) do
stub_request ( :post , 'https://www.googleapis.com/zoo/animals' )
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {} ) )
end
it 'should allow raw JSON if skip_serialization = true' do
command . execute ( client )
expect ( a_request ( :post , 'https://www.googleapis.com/zoo/animals' ) . with do | req |
be_json_eql ( %( {"value":"hello"} ) ) . matches? ( req . body )
end ) . to have_been_made
end
end
2015-04-17 00:28:38 +00:00
context ( 'with a JSON response' ) do
let ( :command ) do
command = Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
command . response_representation = representer_class
command . response_class = model_class
command
end
before ( :example ) do
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {"value" : "hello"} ) )
end
it 'should return a model instance' do
result = command . execute ( client )
expect ( result ) . to be_kind_of ( model_class )
end
it 'should return a populated object' do
result = command . execute ( client )
expect ( result . value ) . to eql 'hello'
end
2017-03-31 22:26:51 +00:00
it 'should return a raw JSON if skip_deserialization true' do
command . options . skip_deserialization = true
result = command . execute ( client )
expect ( result ) . to eql %( {"value" : "hello"} )
end
2015-04-17 00:28:38 +00:00
end
context ( 'with an invalid content-type response' ) do
let ( :command ) do
command = Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
command . response_representation = representer_class
command . response_class = model_class
command
end
before ( :example ) do
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( headers : { 'Content-Type' = > 'text/plain' } , body : %( Ignore me ) )
end
it 'should return nil' do
result = command . execute ( client )
expect ( result ) . to be_nil
end
end
context ( 'with a field parameter' ) do
let ( :command ) do
command = Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
2016-02-25 21:06:16 +00:00
command . query [ 'fields' ] = ':items(:id, :long_name, :a_really_long_name), shouldBeLeftAlone '
2015-04-17 00:28:38 +00:00
command
end
before ( :example ) do
stub_request ( :get , / .* / )
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {} ) )
end
it 'should normalize fields params' do
command . execute ( client )
expect ( a_request ( :get , 'https://www.googleapis.com/zoo/animals' )
2016-02-25 21:06:16 +00:00
. with ( query : { 'fields' = > 'items(id, longName, aReallyLongName), shouldBeLeftAlone ' } ) ) . to have_been_made
2015-04-17 00:28:38 +00:00
end
end
context ( 'with a rate limit response' ) do
let ( :command ) do
Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
end
before ( :example ) do
json = <<EOF
{
" error " : {
" errors " : [
{
" domain " : " global " ,
" reason " : " rateLimitExceeded " ,
" message " : " Rate limit exceeded "
}
] ,
" code " : 403 ,
" message " : " Rate limit exceeded "
}
}
EOF
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( status : [ 403 , 'Rate Limit Exceeded' ] , headers : { 'Content-Type' = > 'application/json' } , body : json )
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {} ) )
end
it 'should retry' do
command . execute ( client )
expect ( a_request ( :get , 'https://www.googleapis.com/zoo/animals' ) ) . to have_been_made . times ( 2 )
end
end
2017-02-02 07:20:19 +00:00
context ( 'with a project not linked response' ) do
let ( :command ) do
Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
end
before ( :example ) do
json = <<EOF
{
" error " : {
" errors " : [
{
" domain " : " global " ,
" reason " : " projectNotLinked " ,
" message " : " The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console. "
}
] ,
" code " : 403 ,
" message " : " The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console. "
}
}
EOF
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( status : [ 403 , 'The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.' ] , headers : {
'Content-Type' = > 'application/json'
} , body : json )
. to_return ( headers : { 'Content-Type' = > 'application/json' } , body : %( {} ) )
end
it 'should raise project not linked error' do
expect { command . execute ( client ) } . to raise_error ( Google :: Apis :: ProjectNotLinkedError )
end
it 'should raise an error with the reason and message' do
expect { command . execute ( client ) } . to raise_error (
/ projectNotLinked: The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console. / )
end
end
2016-01-07 22:28:34 +00:00
context ( 'with a client error response' ) do
let ( :command ) do
Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
end
before ( :example ) do
json = <<EOF
{
" error " : {
" errors " : [
{
" domain " : " global " ,
" reason " : " timeRangeEmpty " ,
" message " : " The specified time range is empty. "
}
] ,
" code " : 400 ,
" message " : " The specified time range is empty. "
}
}
EOF
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( status : [ 400 , 'Bad Request' ] , headers : { 'Content-Type' = > 'application/json' } , body : json )
end
it 'should raise client error' do
expect { command . execute ( client ) } . to raise_error ( Google :: Apis :: ClientError )
end
it 'should raise an error with the reason and message' do
expect { command . execute ( client ) } . to raise_error (
/ timeRangeEmpty: The specified time range is empty / )
end
end
2015-04-17 00:28:38 +00:00
context ( 'with an empty error body' ) do
let ( :command ) do
Google :: Apis :: Core :: ApiCommand . new ( :get , 'https://www.googleapis.com/zoo/animals' )
end
before ( :example ) do
json = %( {} )
stub_request ( :get , 'https://www.googleapis.com/zoo/animals' )
. to_return ( status : [ 403 , 'Rate Limit Exceeded' ] , headers : { 'Content-Type' = > 'application/json' } , body : json )
end
it 'should raise client error' do
expect { command . execute ( client ) } . to raise_error ( Google :: Apis :: ClientError )
end
2016-01-07 22:28:34 +00:00
it 'should use the default error message' do
expect { command . execute ( client ) } . to raise_error ( / Invalid request / )
end
2015-04-17 00:28:38 +00:00
end
end