Added some more specs for Google::APIClient::Result
[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   before do
22     @client = Google::APIClient.new
23   end
24
25   describe 'with the plus API' do
26     before do
27       @client.authorization = nil
28       @plus = @client.discovered_api('plus', 'v1')
29       @reference = Google::APIClient::Reference.new({
30         :api_method => @plus.activities.list,
31         :parameters => {
32           'userId' => 'me',
33           'collection' => 'public',
34           'maxResults' => 20
35         }
36       })
37       @request = @reference.to_request
38
39       # Response stub
40       @response = stub("response")
41       @response.stub(:status).and_return(200)
42       @response.stub(:headers).and_return({
43         'etag' => '12345',
44         'x-google-apiary-auth-scopes' =>
45           'https://www.googleapis.com/auth/plus.me',
46         'content-type' => 'application/json; charset=UTF-8',
47         'date' => 'Mon, 23 Apr 2012 00:00:00 GMT',
48         'cache-control' => 'private, max-age=0, must-revalidate, no-transform',
49         'server' => 'GSE',
50         'connection' => 'close'
51       })
52     end
53
54     describe 'with a next page token' do
55       before do
56         @response.stub(:body).and_return(
57           <<-END_OF_STRING
58           {
59             "kind": "plus#activityFeed",
60             "etag": "FOO",
61             "nextPageToken": "NEXT+PAGE+TOKEN",
62             "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
63             "nextLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN",
64             "title": "Plus Public Activity Feed for ",
65             "updated": "2012-04-23T00:00:00.000Z",
66             "id": "tag:google.com,2010:/plus/people/foo/activities/public",
67             "items": []
68           }
69           END_OF_STRING
70         )
71         @result = Google::APIClient::Result.new(@reference, @request, @response)
72       end
73
74       it 'should return the correct next page token' do
75         @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
76       end
77
78       it 'should escape the next page token when calling next_page' do
79         reference = @result.next_page
80         reference.parameters.should include('pageToken')
81         reference.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN'
82         path = reference.to_request.path.to_s
83         path.should include 'pageToken=NEXT%2BPAGE%2BTOKEN'
84       end
85
86       it 'should return content type correctly' do
87         @result.media_type.should == 'application/json'
88       end
89
90       it 'should return the result data correctly' do
91         @result.data?.should be_true
92         @result.data.class.to_s.should ==
93             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
94         @result.data.kind.should == 'plus#activityFeed'
95         @result.data.etag.should == 'FOO'
96         @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
97         @result.data.selfLink.should ==
98             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
99         @result.data.nextLink.should ==
100             'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
101             'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
102         @result.data.title.should == 'Plus Public Activity Feed for '
103         @result.data.id.should ==
104             'tag:google.com,2010:/plus/people/foo/activities/public'
105         @result.data.items.should be_empty
106       end
107     end
108
109     describe 'without a next page token' do
110       before do
111         @response.stub(:body).and_return(
112           <<-END_OF_STRING
113           {
114             "kind": "plus#activityFeed",
115             "etag": "FOO",
116             "selfLink": "https://www.googleapis.com/plus/v1/people/foo/activities/public?",
117             "title": "Plus Public Activity Feed for ",
118             "updated": "2012-04-23T00:00:00.000Z",
119             "id": "tag:google.com,2010:/plus/people/foo/activities/public",
120             "items": []
121           }
122           END_OF_STRING
123         )
124         @result = Google::APIClient::Result.new(@reference, @request, @response)
125       end
126
127       it 'should not return a next page token' do
128         @result.next_page_token.should == nil
129       end
130
131       it 'should return content type correctly' do
132         @result.media_type.should == 'application/json'
133       end
134
135       it 'should return the result data correctly' do
136         @result.data?.should be_true
137         @result.data.class.to_s.should ==
138             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
139         @result.data.kind.should == 'plus#activityFeed'
140         @result.data.etag.should == 'FOO'
141         @result.data.selfLink.should ==
142             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
143         @result.data.title.should == 'Plus Public Activity Feed for '
144         @result.data.id.should ==
145             'tag:google.com,2010:/plus/people/foo/activities/public'
146         @result.data.items.should be_empty
147       end
148     end
149   end
150 end