1 # Copyright 2012 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'google/api_client'
19 RSpec.describe Google::APIClient::Result do
20 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22 describe 'with the plus API' do
24 CLIENT.authorization = nil
25 @plus = CLIENT.discovered_api('plus', 'v1')
26 @reference = Google::APIClient::Reference.new({
27 :api_method => @plus.activities.list,
30 'collection' => 'public',
34 @request = @reference.to_http_request
37 @response = double("response")
38 allow(@response).to receive(:status).and_return(200)
39 allow(@response).to receive(:headers).and_return({
41 'x-google-apiary-auth-scopes' =>
42 'https://www.googleapis.com/auth/plus.me',
43 'content-type' => 'application/json; charset=UTF-8',
44 'date' => 'Mon, 23 Apr 2012 00:00:00 GMT',
45 'cache-control' => 'private, max-age=0, must-revalidate, no-transform',
47 'connection' => 'close'
51 describe 'with a next page token' do
53 allow(@response).to receive(:body).and_return(
56 "kind": "plus#activityFeed",
58 "nextPageToken": "NEXT+PAGE+TOKEN",
59 "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
60 "nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
61 "title": "Plus Public Activity Feed for ",
62 "updated": "2012-04-23T00:00:00.000Z",
68 @result = Google::APIClient::Result.new(@reference, @response)
71 it 'should indicate a successful response' do
72 expect(@result.error?).to be_falsey
75 it 'should return the correct next page token' do
76 expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
79 it 'should escape the next page token when calling next_page' do
80 reference = @result.next_page
81 expect(Hash[reference.parameters]).to include('pageToken')
82 expect(Hash[reference.parameters]['pageToken']).to eq('NEXT+PAGE+TOKEN')
83 url = reference.to_env(CLIENT.connection)[:url]
84 expect(url.to_s).to include('pageToken=NEXT%2BPAGE%2BTOKEN')
87 it 'should return content type correctly' do
88 expect(@result.media_type).to eq('application/json')
91 it 'should return the result data correctly' do
92 expect(@result.data?).to be_truthy
93 expect(@result.data.class.to_s).to eq(
94 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
96 expect(@result.data.kind).to eq('plus#activityFeed')
97 expect(@result.data.etag).to eq('FOO')
98 expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
99 expect(@result.data.selfLink).to eq(
100 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
102 expect(@result.data.nextLink).to eq(
103 'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
104 'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
106 expect(@result.data.title).to eq('Plus Public Activity Feed for ')
107 expect(@result.data.id).to eq("123456790")
108 expect(@result.data.items).to be_empty
112 describe 'without a next page token' do
114 allow(@response).to receive(:body).and_return(
117 "kind": "plus#activityFeed",
119 "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
120 "title": "Plus Public Activity Feed for ",
121 "updated": "2012-04-23T00:00:00.000Z",
127 @result = Google::APIClient::Result.new(@reference, @response)
130 it 'should not return a next page token' do
131 expect(@result.next_page_token).to eq(nil)
134 it 'should return content type correctly' do
135 expect(@result.media_type).to eq('application/json')
138 it 'should return the result data correctly' do
139 expect(@result.data?).to be_truthy
140 expect(@result.data.class.to_s).to eq(
141 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
143 expect(@result.data.kind).to eq('plus#activityFeed')
144 expect(@result.data.etag).to eq('FOO')
145 expect(@result.data.selfLink).to eq(
146 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
148 expect(@result.data.title).to eq('Plus Public Activity Feed for ')
149 expect(@result.data.id).to eq("123456790")
150 expect(@result.data.items).to be_empty
154 describe 'with JSON error response' do
156 allow(@response).to receive(:body).and_return(
163 "reason": "parseError",
164 "message": "Parse Error"
168 "message": "Parse Error"
173 allow(@response).to receive(:status).and_return(400)
174 @result = Google::APIClient::Result.new(@reference, @response)
177 it 'should return error status correctly' do
178 expect(@result.error?).to be_truthy
181 it 'should return the correct error message' do
182 expect(@result.error_message).to eq('Parse Error')
186 describe 'with 204 No Content response' do
188 allow(@response).to receive(:body).and_return('')
189 allow(@response).to receive(:status).and_return(204)
190 allow(@response).to receive(:headers).and_return({})
191 @result = Google::APIClient::Result.new(@reference, @response)
194 it 'should indicate no data is available' do
195 expect(@result.data?).to be_falsey
198 it 'should return nil for data' do
199 expect(@result.data).to eq(nil)
202 it 'should return nil for media_type' do
203 expect(@result.media_type).to eq(nil)