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 @response.stub(:status).and_return(200)
40 @response.stub(: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 @response.stub(: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 @result.error?.should be_false
76 it 'should return the correct next page token' do
77 @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
80 it 'should escape the next page token when calling next_page' do
81 reference = @result.next_page
82 Hash[reference.parameters].should include('pageToken')
83 Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
84 url = reference.to_env(CLIENT.connection)[:url]
85 url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
88 it 'should return content type correctly' do
89 @result.media_type.should == 'application/json'
92 it 'should return the result data correctly' do
93 @result.data?.should be_true
94 @result.data.class.to_s.should ==
95 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
96 @result.data.kind.should == 'plus#activityFeed'
97 @result.data.etag.should == 'FOO'
98 @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
99 @result.data.selfLink.should ==
100 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
101 @result.data.nextLink.should ==
102 'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
103 'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
104 @result.data.title.should == 'Plus Public Activity Feed for '
105 @result.data.id.should == "123456790"
106 @result.data.items.should be_empty
110 describe 'without a next page token' do
112 @response.stub(:body).and_return(
115 "kind": "plus#activityFeed",
117 "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
118 "title": "Plus Public Activity Feed for ",
119 "updated": "2012-04-23T00:00:00.000Z",
125 @result = Google::APIClient::Result.new(@reference, @response)
128 it 'should not return a next page token' do
129 @result.next_page_token.should == nil
132 it 'should return content type correctly' do
133 @result.media_type.should == 'application/json'
136 it 'should return the result data correctly' do
137 @result.data?.should be_true
138 @result.data.class.to_s.should ==
139 'Google::APIClient::Schema::Plus::V1::ActivityFeed'
140 @result.data.kind.should == 'plus#activityFeed'
141 @result.data.etag.should == 'FOO'
142 @result.data.selfLink.should ==
143 'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
144 @result.data.title.should == 'Plus Public Activity Feed for '
145 @result.data.id.should == "123456790"
146 @result.data.items.should be_empty
150 describe 'with JSON error response' do
152 @response.stub(:body).and_return(
159 "reason": "parseError",
160 "message": "Parse Error"
164 "message": "Parse Error"
169 @response.stub(:status).and_return(400)
170 @result = Google::APIClient::Result.new(@reference, @response)
173 it 'should return error status correctly' do
174 @result.error?.should be_true
177 it 'should return the correct error message' do
178 @result.error_message.should == 'Parse Error'
182 describe 'with 204 No Content response' do
184 @response.stub(:body).and_return('')
185 @response.stub(:status).and_return(204)
186 @response.stub(:headers).and_return({})
187 @result = Google::APIClient::Result.new(@reference, @response)
190 it 'should indicate no data is available' do
191 @result.data?.should be_false
194 it 'should return nil for data' do
195 @result.data.should == nil
198 it 'should return nil for media_type' do
199 @result.media_type.should == nil