21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / spec / google / api_client / result_spec.rb
1 # Copyright 2012 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'spec_helper'
16
17 require 'google/api_client'
18
19 RSpec.describe Google::APIClient::Result do
20   CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
21
22   describe 'with the plus API' do
23     before do
24       CLIENT.authorization = nil
25       @plus = CLIENT.discovered_api('plus', 'v1')
26       @reference = Google::APIClient::Reference.new({
27         :api_method => @plus.activities.list,
28         :parameters => {
29           'userId' => 'me',
30           'collection' => 'public',
31           'maxResults' => 20
32         }
33       })
34       @request = @reference.to_http_request
35
36       # Response double
37       @response = double("response")
38       allow(@response).to receive(:status).and_return(200)
39       allow(@response).to receive(:headers).and_return({
40         'etag' => '12345',
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',
46         'server' => 'GSE',
47         'connection' => 'close'
48       })
49     end
50
51     describe 'with a next page token' do
52       before do
53         allow(@response).to receive(:body).and_return(
54           <<-END_OF_STRING
55           {
56             "kind": "plus#activityFeed",
57             "etag": "FOO",
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",
63             "id": "123456790",
64             "items": []
65           }
66           END_OF_STRING
67         )
68         @result = Google::APIClient::Result.new(@reference, @response)
69       end
70
71       it 'should indicate a successful response' do
72         expect(@result.error?).to be_falsey
73       end
74
75       it 'should return the correct next page token' do
76         expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
77       end
78
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')
85       end
86
87       it 'should return content type correctly' do
88         expect(@result.media_type).to eq('application/json')
89       end
90
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'
95         )
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?'
101         )
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'
105         )
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
109       end
110     end
111
112     describe 'without a next page token' do
113       before do
114         allow(@response).to receive(:body).and_return(
115           <<-END_OF_STRING
116           {
117             "kind": "plus#activityFeed",
118             "etag": "FOO",
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",
122             "id": "123456790",
123             "items": []
124           }
125           END_OF_STRING
126         )
127         @result = Google::APIClient::Result.new(@reference, @response)
128       end
129
130       it 'should not return a next page token' do
131         expect(@result.next_page_token).to eq(nil)
132       end
133
134       it 'should return content type correctly' do
135         expect(@result.media_type).to eq('application/json')
136       end
137
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'
142         )
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?'
147         )
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
151       end
152     end
153
154     describe 'with JSON error response' do
155       before do
156         allow(@response).to receive(:body).and_return(
157          <<-END_OF_STRING
158          {
159           "error": {
160            "errors": [
161             {
162              "domain": "global",
163              "reason": "parseError",
164              "message": "Parse Error"
165             }
166            ],
167            "code": 400,
168            "message": "Parse Error"
169           }
170          }
171          END_OF_STRING
172         )
173         allow(@response).to receive(:status).and_return(400)
174         @result = Google::APIClient::Result.new(@reference, @response)
175       end
176
177       it 'should return error status correctly' do
178         expect(@result.error?).to be_truthy
179       end
180
181       it 'should return the correct error message' do
182         expect(@result.error_message).to eq('Parse Error')
183       end
184     end
185
186     describe 'with 204 No Content response' do
187       before 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)
192       end
193
194       it 'should indicate no data is available' do
195         expect(@result.data?).to be_falsey
196       end
197
198       it 'should return nil for data' do
199         expect(@result.data).to eq(nil)
200       end
201
202       it 'should return nil for media_type' do
203         expect(@result.media_type).to eq(nil)
204       end
205     end
206   end
207 end