G+ API changed schema :(
[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
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 stub
38       @response = stub("response")
39       @response.stub(:status).and_return(200)
40       @response.stub(: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         @response.stub(: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         @result.error?.should be_false
74       end
75
76       it 'should return the correct next page token' do
77         @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
78       end
79
80       it 'should escape the next page token when calling next_page' do
81         pending("This is caused by Faraday's encoding of query parameters.")
82         reference = @result.next_page
83         Hash[reference.parameters].should include('pageToken')
84         Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
85         url = reference.to_env(Faraday.default_connection)[:url]
86         url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
87       end
88
89       it 'should return content type correctly' do
90         @result.media_type.should == 'application/json'
91       end
92
93       it 'should return the result data correctly' do
94         @result.data?.should be_true
95         @result.data.class.to_s.should ==
96             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
97         @result.data.kind.should == 'plus#activityFeed'
98         @result.data.etag.should == 'FOO'
99         @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
100         @result.data.selfLink.should ==
101             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
102         @result.data.nextLink.should ==
103             'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
104             'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
105         @result.data.title.should == 'Plus Public Activity Feed for '
106         @result.data.id.should == "123456790"
107         @result.data.items.should be_empty
108       end
109     end
110
111     describe 'without a next page token' do
112       before do
113         @response.stub(:body).and_return(
114           <<-END_OF_STRING
115           {
116             "kind": "plus#activityFeed",
117             "etag": "FOO",
118             "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
119             "title": "Plus Public Activity Feed for ",
120             "updated": "2012-04-23T00:00:00.000Z",
121             "id": "123456790",
122             "items": []
123           }
124           END_OF_STRING
125         )
126         @result = Google::APIClient::Result.new(@reference, @response)
127       end
128
129       it 'should not return a next page token' do
130         @result.next_page_token.should == nil
131       end
132
133       it 'should return content type correctly' do
134         @result.media_type.should == 'application/json'
135       end
136
137       it 'should return the result data correctly' do
138         @result.data?.should be_true
139         @result.data.class.to_s.should ==
140             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
141         @result.data.kind.should == 'plus#activityFeed'
142         @result.data.etag.should == 'FOO'
143         @result.data.selfLink.should ==
144             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
145         @result.data.title.should == 'Plus Public Activity Feed for '
146         @result.data.id.should == "123456790"
147         @result.data.items.should be_empty
148       end
149     end
150     
151     describe 'with JSON error response' do
152       before do
153         @response.stub(:body).and_return(
154          <<-END_OF_STRING
155          {
156           "error": {
157            "errors": [
158             {
159              "domain": "global",
160              "reason": "parseError",
161              "message": "Parse Error"
162             }
163            ],
164            "code": 400,
165            "message": "Parse Error"
166           }
167          }
168          END_OF_STRING
169         )
170         @response.stub(:status).and_return(400)
171         @result = Google::APIClient::Result.new(@reference, @response)
172       end
173       
174       it 'should return error status correctly' do
175         @result.error?.should be_true
176       end
177
178       it 'should return the correct error message' do
179         @result.error_message.should == 'Parse Error'
180       end
181     end
182     
183     describe 'with 204 No Content response' do
184       before do
185         @response.stub(:body).and_return('')
186         @response.stub(:status).and_return(204)
187         @response.stub(:headers).and_return({})
188         @result = Google::APIClient::Result.new(@reference, @response)
189       end
190
191       it 'should indicate no data is available' do
192         @result.data?.should be_false
193       end
194       
195       it 'should return nil for data' do
196         @result.data.should == nil
197       end
198       
199       it 'should return nil for media_type' do
200         @result.media_type.should == nil
201       end
202     end
203   end
204 end