Updated rspec to v3 and replaced rcov with simplecov.
[arvados.git] / 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 require 'google/api_client/version'
19
20 describe Google::APIClient::Result do
21   CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
22
23   describe 'with the plus API' do
24     before do
25       CLIENT.authorization = nil
26       @plus = CLIENT.discovered_api('plus', 'v1')
27       @reference = Google::APIClient::Reference.new({
28         :api_method => @plus.activities.list,
29         :parameters => {
30           'userId' => 'me',
31           'collection' => 'public',
32           'maxResults' => 20
33         }
34       })
35       @request = @reference.to_http_request
36
37       # Response double
38       @response = double("response")
39       allow(@response).to receive(:status).and_return(200)
40       allow(@response).to receive(:headers).and_return({
41         'etag' => '12345',
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',
47         'server' => 'GSE',
48         'connection' => 'close'
49       })
50     end
51
52     describe 'with a next page token' do
53       before do
54         allow(@response).to receive(:body).and_return(
55           <<-END_OF_STRING
56           {
57             "kind": "plus#activityFeed",
58             "etag": "FOO",
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",
64             "id": "123456790",
65             "items": []
66           }
67           END_OF_STRING
68         )
69         @result = Google::APIClient::Result.new(@reference, @response)
70       end
71
72       it 'should indicate a successful response' do
73         expect(@result.error?).to be_falsey
74       end
75
76       it 'should return the correct next page token' do
77         expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
78       end
79
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')
86       end
87
88       it 'should return content type correctly' do
89         expect(@result.media_type).to eq('application/json')
90       end
91
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'
96         )
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?'
102         )
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'
106         )
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
110       end
111     end
112
113     describe 'without a next page token' do
114       before do
115         allow(@response).to receive(:body).and_return(
116           <<-END_OF_STRING
117           {
118             "kind": "plus#activityFeed",
119             "etag": "FOO",
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",
123             "id": "123456790",
124             "items": []
125           }
126           END_OF_STRING
127         )
128         @result = Google::APIClient::Result.new(@reference, @response)
129       end
130
131       it 'should not return a next page token' do
132         expect(@result.next_page_token).to eq(nil)
133       end
134
135       it 'should return content type correctly' do
136         expect(@result.media_type).to eq('application/json')
137       end
138
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'
143         )
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?'
148         )
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
152       end
153     end
154
155     describe 'with JSON error response' do
156       before do
157         allow(@response).to receive(:body).and_return(
158          <<-END_OF_STRING
159          {
160           "error": {
161            "errors": [
162             {
163              "domain": "global",
164              "reason": "parseError",
165              "message": "Parse Error"
166             }
167            ],
168            "code": 400,
169            "message": "Parse Error"
170           }
171          }
172          END_OF_STRING
173         )
174         allow(@response).to receive(:status).and_return(400)
175         @result = Google::APIClient::Result.new(@reference, @response)
176       end
177
178       it 'should return error status correctly' do
179         expect(@result.error?).to be_truthy
180       end
181
182       it 'should return the correct error message' do
183         expect(@result.error_message).to eq('Parse Error')
184       end
185     end
186
187     describe 'with 204 No Content response' do
188       before 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)
193       end
194
195       it 'should indicate no data is available' do
196         expect(@result.data?).to be_falsey
197       end
198
199       it 'should return nil for data' do
200         expect(@result.data).to eq(nil)
201       end
202
203       it 'should return nil for media_type' do
204         expect(@result.media_type).to eq(nil)
205       end
206     end
207   end
208 end