2012-04-24 11:47:28 +00:00
|
|
|
# Copyright 2012 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/api_client'
|
|
|
|
|
2014-09-17 15:03:33 +00:00
|
|
|
RSpec.describe Google::APIClient::Result do
|
2012-12-30 19:27:45 +00:00
|
|
|
CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
|
2012-04-24 11:47:28 +00:00
|
|
|
|
|
|
|
describe 'with the plus API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@plus = CLIENT.discovered_api('plus', 'v1')
|
2012-04-24 11:47:28 +00:00
|
|
|
@reference = Google::APIClient::Reference.new({
|
|
|
|
:api_method => @plus.activities.list,
|
|
|
|
:parameters => {
|
|
|
|
'userId' => 'me',
|
|
|
|
'collection' => 'public',
|
|
|
|
'maxResults' => 20
|
|
|
|
}
|
|
|
|
})
|
2012-09-05 21:56:52 +00:00
|
|
|
@request = @reference.to_http_request
|
2012-04-24 11:47:28 +00:00
|
|
|
|
2013-08-12 15:19:09 +00:00
|
|
|
# Response double
|
|
|
|
@response = double("response")
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:status).and_return(200)
|
|
|
|
allow(@response).to receive(:headers).and_return({
|
2012-04-24 11:47:28 +00:00
|
|
|
'etag' => '12345',
|
|
|
|
'x-google-apiary-auth-scopes' =>
|
|
|
|
'https://www.googleapis.com/auth/plus.me',
|
|
|
|
'content-type' => 'application/json; charset=UTF-8',
|
|
|
|
'date' => 'Mon, 23 Apr 2012 00:00:00 GMT',
|
|
|
|
'cache-control' => 'private, max-age=0, must-revalidate, no-transform',
|
|
|
|
'server' => 'GSE',
|
|
|
|
'connection' => 'close'
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2012-05-16 19:26:10 +00:00
|
|
|
describe 'with a next page token' do
|
|
|
|
before do
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:body).and_return(
|
2012-05-16 19:26:10 +00:00
|
|
|
<<-END_OF_STRING
|
|
|
|
{
|
|
|
|
"kind": "plus#activityFeed",
|
|
|
|
"etag": "FOO",
|
|
|
|
"nextPageToken": "NEXT+PAGE+TOKEN",
|
|
|
|
"selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
|
|
|
|
"nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
|
|
|
|
"title": "Plus Public Activity Feed for ",
|
|
|
|
"updated": "2012-04-23T00:00:00.000Z",
|
2012-10-25 21:25:54 +00:00
|
|
|
"id": "123456790",
|
2012-05-16 19:26:10 +00:00
|
|
|
"items": []
|
|
|
|
}
|
|
|
|
END_OF_STRING
|
|
|
|
)
|
2012-09-05 21:56:52 +00:00
|
|
|
@result = Google::APIClient::Result.new(@reference, @response)
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
2012-07-25 03:35:07 +00:00
|
|
|
it 'should indicate a successful response' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.error?).to be_falsey
|
2012-07-25 03:35:07 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:26:10 +00:00
|
|
|
it 'should return the correct next page token' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should escape the next page token when calling next_page' do
|
|
|
|
reference = @result.next_page
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(Hash[reference.parameters]).to include('pageToken')
|
|
|
|
expect(Hash[reference.parameters]['pageToken']).to eq('NEXT+PAGE+TOKEN')
|
2013-05-13 21:03:49 +00:00
|
|
|
url = reference.to_env(CLIENT.connection)[:url]
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(url.to_s).to include('pageToken=NEXT%2BPAGE%2BTOKEN')
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return content type correctly' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.media_type).to eq('application/json')
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the result data correctly' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.data?).to be_truthy
|
|
|
|
expect(@result.data.class.to_s).to eq(
|
2012-05-16 19:26:10 +00:00
|
|
|
'Google::APIClient::Schema::Plus::V1::ActivityFeed'
|
2014-08-14 15:54:16 +00:00
|
|
|
)
|
|
|
|
expect(@result.data.kind).to eq('plus#activityFeed')
|
|
|
|
expect(@result.data.etag).to eq('FOO')
|
|
|
|
expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
|
|
|
|
expect(@result.data.selfLink).to eq(
|
2012-05-16 19:26:10 +00:00
|
|
|
'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
|
2014-08-14 15:54:16 +00:00
|
|
|
)
|
|
|
|
expect(@result.data.nextLink).to eq(
|
2012-05-16 19:26:10 +00:00
|
|
|
'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
|
|
|
|
'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
|
2014-08-14 15:54:16 +00:00
|
|
|
)
|
|
|
|
expect(@result.data.title).to eq('Plus Public Activity Feed for ')
|
|
|
|
expect(@result.data.id).to eq("123456790")
|
|
|
|
expect(@result.data.items).to be_empty
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
2012-04-24 11:47:28 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:26:10 +00:00
|
|
|
describe 'without a next page token' do
|
|
|
|
before do
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:body).and_return(
|
2012-05-16 19:26:10 +00:00
|
|
|
<<-END_OF_STRING
|
|
|
|
{
|
|
|
|
"kind": "plus#activityFeed",
|
|
|
|
"etag": "FOO",
|
|
|
|
"selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
|
|
|
|
"title": "Plus Public Activity Feed for ",
|
|
|
|
"updated": "2012-04-23T00:00:00.000Z",
|
2012-10-25 21:25:54 +00:00
|
|
|
"id": "123456790",
|
2012-05-16 19:26:10 +00:00
|
|
|
"items": []
|
|
|
|
}
|
|
|
|
END_OF_STRING
|
|
|
|
)
|
2012-09-05 21:56:52 +00:00
|
|
|
@result = Google::APIClient::Result.new(@reference, @response)
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not return a next page token' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.next_page_token).to eq(nil)
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return content type correctly' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.media_type).to eq('application/json')
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the result data correctly' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.data?).to be_truthy
|
|
|
|
expect(@result.data.class.to_s).to eq(
|
2012-05-16 19:26:10 +00:00
|
|
|
'Google::APIClient::Schema::Plus::V1::ActivityFeed'
|
2014-08-14 15:54:16 +00:00
|
|
|
)
|
|
|
|
expect(@result.data.kind).to eq('plus#activityFeed')
|
|
|
|
expect(@result.data.etag).to eq('FOO')
|
|
|
|
expect(@result.data.selfLink).to eq(
|
2012-05-16 19:26:10 +00:00
|
|
|
'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
|
2014-08-14 15:54:16 +00:00
|
|
|
)
|
|
|
|
expect(@result.data.title).to eq('Plus Public Activity Feed for ')
|
|
|
|
expect(@result.data.id).to eq("123456790")
|
|
|
|
expect(@result.data.items).to be_empty
|
2012-05-16 19:26:10 +00:00
|
|
|
end
|
2012-04-24 11:47:28 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-07-25 03:35:07 +00:00
|
|
|
describe 'with JSON error response' do
|
|
|
|
before do
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:body).and_return(
|
2012-07-25 03:35:07 +00:00
|
|
|
<<-END_OF_STRING
|
|
|
|
{
|
|
|
|
"error": {
|
|
|
|
"errors": [
|
|
|
|
{
|
|
|
|
"domain": "global",
|
|
|
|
"reason": "parseError",
|
|
|
|
"message": "Parse Error"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"code": 400,
|
|
|
|
"message": "Parse Error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_OF_STRING
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:status).and_return(400)
|
2012-09-05 21:56:52 +00:00
|
|
|
@result = Google::APIClient::Result.new(@reference, @response)
|
2012-07-25 03:35:07 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-07-25 03:35:07 +00:00
|
|
|
it 'should return error status correctly' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.error?).to be_truthy
|
2012-07-25 03:35:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the correct error message' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.error_message).to eq('Parse Error')
|
2012-07-25 03:35:07 +00:00
|
|
|
end
|
2012-10-05 01:17:54 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-10-05 01:17:54 +00:00
|
|
|
describe 'with 204 No Content response' do
|
|
|
|
before do
|
2014-08-14 15:54:16 +00:00
|
|
|
allow(@response).to receive(:body).and_return('')
|
|
|
|
allow(@response).to receive(:status).and_return(204)
|
|
|
|
allow(@response).to receive(:headers).and_return({})
|
2012-10-05 01:17:54 +00:00
|
|
|
@result = Google::APIClient::Result.new(@reference, @response)
|
|
|
|
end
|
2012-07-25 03:35:07 +00:00
|
|
|
|
2012-10-05 01:17:54 +00:00
|
|
|
it 'should indicate no data is available' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.data?).to be_falsey
|
2012-10-05 01:17:54 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-10-05 01:17:54 +00:00
|
|
|
it 'should return nil for data' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.data).to eq(nil)
|
2012-10-05 01:17:54 +00:00
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
|
2012-10-05 01:17:54 +00:00
|
|
|
it 'should return nil for media_type' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@result.media_type).to eq(nil)
|
2012-10-05 01:17:54 +00:00
|
|
|
end
|
2012-07-25 03:35:07 +00:00
|
|
|
end
|
2012-04-24 11:47:28 +00:00
|
|
|
end
|
|
|
|
end
|