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'
18 require 'google/api_client/version'
20 describe Google::APIClient::Result do
21 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
23 describe 'with the plus API' do
25 CLIENT.authorization = nil
26 @plus = CLIENT.discovered_api('plus', 'v1')
27 @reference = Google::APIClient::Reference.new({
28 :api_method => @plus.activities.list,
31 'collection' => 'public',
35 @request = @reference.to_http_request
38 @response = double("response")
39 allow(@response).to receive(:status).and_return(200)
40 allow(@response).to receive(:headers).and_return({
42 'x-google-apiary-auth-scopes' =>
43 'https://www.googleapis.com/auth/plus.me',
44 'content-type' => 'application/json; charset=UTF-8',
45 'date' => 'Mon, 23 Apr 2012 00:00:00 GMT',
46 'cache-control' => 'private, max-age=0, must-revalidate, no-transform',
48 'connection' => 'close'
52 describe 'with a next page token' do
54 allow(@response).to receive(:body).and_return(
57 "kind": "plus#activityFeed",
59 "nextPageToken": "NEXT+PAGE+TOKEN",
60 "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
61 "nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
62 "title": "Plus Public Activity Feed for ",
63 "updated": "2012-04-23T00:00:00.000Z",
69 @result = Google::APIClient::Result.new(@reference, @response)
72 it 'should indicate a successful response' do
73 expect(@result.error?).to be_falsey
76 it 'should return the correct next page token' do
77 expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
80 it 'should escape the next page token when calling next_page' do
81 reference = @result.next_page
82 expect(Hash[reference.parameters]).to include('pageToken')
83 expect(Hash[reference.parameters]['pageToken']).to eq('NEXT+PAGE+TOKEN')
84 url = reference.to_env(CLIENT.connection)[:url]
85 expect(url.to_s).to include('pageToken=NEXT%2BPAGE%2BTOKEN')
88 it 'should return content type correctly' do
89 expect(@result.media_type).to eq('application/json')
92 it 'should return the result data correctly' do
93 expect(@result.data?).to be_truthy
94 expect(@result.data.class.to_s).to eq(
95 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
97 expect(@result.data.kind).to eq('plus#activityFeed')
98 expect(@result.data.etag).to eq('FOO')
99 expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
100 expect(@result.data.selfLink).to eq(
101 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
103 expect(@result.data.nextLink).to eq(
104 'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
105 'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
107 expect(@result.data.title).to eq('Plus Public Activity Feed for ')
108 expect(@result.data.id).to eq("123456790")
109 expect(@result.data.items).to be_empty
113 describe 'without a next page token' do
115 allow(@response).to receive(:body).and_return(
118 "kind": "plus#activityFeed",
120 "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
121 "title": "Plus Public Activity Feed for ",
122 "updated": "2012-04-23T00:00:00.000Z",
128 @result = Google::APIClient::Result.new(@reference, @response)
131 it 'should not return a next page token' do
132 expect(@result.next_page_token).to eq(nil)
135 it 'should return content type correctly' do
136 expect(@result.media_type).to eq('application/json')
139 it 'should return the result data correctly' do
140 expect(@result.data?).to be_truthy
141 expect(@result.data.class.to_s).to eq(
142 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
144 expect(@result.data.kind).to eq('plus#activityFeed')
145 expect(@result.data.etag).to eq('FOO')
146 expect(@result.data.selfLink).to eq(
147 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
149 expect(@result.data.title).to eq('Plus Public Activity Feed for ')
150 expect(@result.data.id).to eq("123456790")
151 expect(@result.data.items).to be_empty
155 describe 'with JSON error response' do
157 allow(@response).to receive(:body).and_return(
164 "reason": "parseError",
165 "message": "Parse Error"
169 "message": "Parse Error"
174 allow(@response).to receive(:status).and_return(400)
175 @result = Google::APIClient::Result.new(@reference, @response)
178 it 'should return error status correctly' do
179 expect(@result.error?).to be_truthy
182 it 'should return the correct error message' do
183 expect(@result.error_message).to eq('Parse Error')
187 describe 'with 204 No Content response' do
189 allow(@response).to receive(:body).and_return('')
190 allow(@response).to receive(:status).and_return(204)
191 allow(@response).to receive(:headers).and_return({})
192 @result = Google::APIClient::Result.new(@reference, @response)
195 it 'should indicate no data is available' do
196 expect(@result.data?).to be_falsey
199 it 'should return nil for data' do
200 expect(@result.data).to eq(nil)
203 it 'should return nil for media_type' do
204 expect(@result.media_type).to eq(nil)