@@ -24,38 +24,6 @@ def _getTargetClass(self):
2424 def _makeOne (self , * args , ** kw ):
2525 return self ._getTargetClass ()(* args , ** kw )
2626
27- def test_ctor_defaults (self ):
28- conn = self ._makeOne ()
29- self .assertEqual (conn .credentials , None )
30-
31- def test_ctor_explicit (self ):
32- creds = object ()
33- conn = self ._makeOne (creds )
34- self .assertTrue (conn .credentials is creds )
35-
36- def test_http_w_existing (self ):
37- conn = self ._makeOne ()
38- conn ._http = http = object ()
39- self .assertTrue (conn .http is http )
40-
41- def test_http_wo_creds (self ):
42- import httplib2
43- conn = self ._makeOne ()
44- self .assertTrue (isinstance (conn .http , httplib2 .Http ))
45-
46- def test_http_w_creds (self ):
47- import httplib2
48- authorized = object ()
49-
50- class Creds (object ):
51- def authorize (self , http ):
52- self ._called_with = http
53- return authorized
54- creds = Creds ()
55- conn = self ._makeOne (creds )
56- self .assertTrue (conn .http is authorized )
57- self .assertTrue (isinstance (creds ._called_with , httplib2 .Http ))
58-
5927 def test_build_api_url_no_extra_query_params (self ):
6028 conn = self ._makeOne ()
6129 URI = '/' .join ([
@@ -77,187 +45,3 @@ def test_build_api_url_w_extra_query_params(self):
7745 '/' .join (['' , 'storage' , conn .API_VERSION , 'foo' ]))
7846 parms = dict (parse_qsl (qs ))
7947 self .assertEqual (parms ['bar' ], 'baz' )
80-
81- def test__make_request_no_data_no_content_type_no_headers (self ):
82- conn = self ._makeOne ()
83- URI = 'http://example.com/test'
84- http = conn ._http = Http (
85- {'status' : '200' , 'content-type' : 'text/plain' },
86- '' ,
87- )
88- headers , content = conn ._make_request ('GET' , URI )
89- self .assertEqual (headers ['status' ], '200' )
90- self .assertEqual (headers ['content-type' ], 'text/plain' )
91- self .assertEqual (content , '' )
92- self .assertEqual (http ._called_with ['method' ], 'GET' )
93- self .assertEqual (http ._called_with ['uri' ], URI )
94- self .assertEqual (http ._called_with ['body' ], None )
95- expected_headers = {
96- 'Accept-Encoding' : 'gzip' ,
97- 'Content-Length' : 0 ,
98- 'User-Agent' : conn .USER_AGENT ,
99- }
100- self .assertEqual (http ._called_with ['headers' ], expected_headers )
101-
102- def test__make_request_w_data_no_extra_headers (self ):
103- conn = self ._makeOne ()
104- URI = 'http://example.com/test'
105- http = conn ._http = Http (
106- {'status' : '200' , 'content-type' : 'text/plain' },
107- '' ,
108- )
109- conn ._make_request ('GET' , URI , {}, 'application/json' )
110- self .assertEqual (http ._called_with ['method' ], 'GET' )
111- self .assertEqual (http ._called_with ['uri' ], URI )
112- self .assertEqual (http ._called_with ['body' ], {})
113- expected_headers = {
114- 'Accept-Encoding' : 'gzip' ,
115- 'Content-Length' : 0 ,
116- 'Content-Type' : 'application/json' ,
117- 'User-Agent' : conn .USER_AGENT ,
118- }
119- self .assertEqual (http ._called_with ['headers' ], expected_headers )
120-
121- def test__make_request_w_extra_headers (self ):
122- conn = self ._makeOne ()
123- URI = 'http://example.com/test'
124- http = conn ._http = Http (
125- {'status' : '200' , 'content-type' : 'text/plain' },
126- '' ,
127- )
128- conn ._make_request ('GET' , URI , headers = {'X-Foo' : 'foo' })
129- self .assertEqual (http ._called_with ['method' ], 'GET' )
130- self .assertEqual (http ._called_with ['uri' ], URI )
131- self .assertEqual (http ._called_with ['body' ], None )
132- expected_headers = {
133- 'Accept-Encoding' : 'gzip' ,
134- 'Content-Length' : 0 ,
135- 'X-Foo' : 'foo' ,
136- 'User-Agent' : conn .USER_AGENT ,
137- }
138- self .assertEqual (http ._called_with ['headers' ], expected_headers )
139-
140- def test_api_request_defaults (self ):
141- PATH = '/path/required'
142- conn = self ._makeOne ()
143- URI = '/' .join ([
144- conn .API_BASE_URL ,
145- 'storage' ,
146- '%s%s' % (conn .API_VERSION , PATH ),
147- ])
148- http = conn ._http = Http (
149- {'status' : '200' , 'content-type' : 'application/json' },
150- '{}' ,
151- )
152- self .assertEqual (conn .api_request ('GET' , PATH ), {})
153- self .assertEqual (http ._called_with ['method' ], 'GET' )
154- self .assertEqual (http ._called_with ['uri' ], URI )
155- self .assertEqual (http ._called_with ['body' ], None )
156- expected_headers = {
157- 'Accept-Encoding' : 'gzip' ,
158- 'Content-Length' : 0 ,
159- 'User-Agent' : conn .USER_AGENT ,
160- }
161- self .assertEqual (http ._called_with ['headers' ], expected_headers )
162-
163- def test_api_request_w_non_json_response (self ):
164- conn = self ._makeOne ()
165- conn ._http = Http (
166- {'status' : '200' , 'content-type' : 'text/plain' },
167- 'CONTENT' ,
168- )
169-
170- self .assertRaises (TypeError , conn .api_request , 'GET' , '/' )
171-
172- def test_api_request_wo_json_expected (self ):
173- conn = self ._makeOne ()
174- conn ._http = Http (
175- {'status' : '200' , 'content-type' : 'text/plain' },
176- 'CONTENT' ,
177- )
178- self .assertEqual (conn .api_request ('GET' , '/' , expect_json = False ),
179- 'CONTENT' )
180-
181- def test_api_request_w_query_params (self ):
182- from six .moves .urllib .parse import parse_qsl
183- from six .moves .urllib .parse import urlsplit
184- conn = self ._makeOne ()
185- http = conn ._http = Http (
186- {'status' : '200' , 'content-type' : 'application/json' },
187- '{}' ,
188- )
189- self .assertEqual (conn .api_request ('GET' , '/' , {'foo' : 'bar' }), {})
190- self .assertEqual (http ._called_with ['method' ], 'GET' )
191- uri = http ._called_with ['uri' ]
192- scheme , netloc , path , qs , _ = urlsplit (uri )
193- self .assertEqual ('%s://%s' % (scheme , netloc ), conn .API_BASE_URL )
194- self .assertEqual (path ,
195- '/' .join (['' , 'storage' , conn .API_VERSION , '' ]))
196- parms = dict (parse_qsl (qs ))
197- self .assertEqual (parms ['foo' ], 'bar' )
198- self .assertEqual (http ._called_with ['body' ], None )
199- expected_headers = {
200- 'Accept-Encoding' : 'gzip' ,
201- 'Content-Length' : 0 ,
202- 'User-Agent' : conn .USER_AGENT ,
203- }
204- self .assertEqual (http ._called_with ['headers' ], expected_headers )
205-
206- def test_api_request_w_data (self ):
207- import json
208- DATA = {'foo' : 'bar' }
209- DATAJ = json .dumps (DATA )
210- conn = self ._makeOne ()
211- URI = '/' .join ([
212- conn .API_BASE_URL ,
213- 'storage' ,
214- conn .API_VERSION ,
215- '' ,
216- ])
217- http = conn ._http = Http (
218- {'status' : '200' , 'content-type' : 'application/json' },
219- '{}' ,
220- )
221- self .assertEqual (conn .api_request ('POST' , '/' , data = DATA ), {})
222- self .assertEqual (http ._called_with ['method' ], 'POST' )
223- self .assertEqual (http ._called_with ['uri' ], URI )
224- self .assertEqual (http ._called_with ['body' ], DATAJ )
225- expected_headers = {
226- 'Accept-Encoding' : 'gzip' ,
227- 'Content-Length' : len (DATAJ ),
228- 'Content-Type' : 'application/json' ,
229- 'User-Agent' : conn .USER_AGENT ,
230- }
231- self .assertEqual (http ._called_with ['headers' ], expected_headers )
232-
233- def test_api_request_w_404 (self ):
234- from gcloud .exceptions import NotFound
235- conn = self ._makeOne ()
236- conn ._http = Http (
237- {'status' : '404' , 'content-type' : 'text/plain' },
238- '{}'
239- )
240- self .assertRaises (NotFound , conn .api_request , 'GET' , '/' )
241-
242- def test_api_request_w_500 (self ):
243- from gcloud .exceptions import InternalServerError
244- conn = self ._makeOne ()
245- conn ._http = Http (
246- {'status' : '500' , 'content-type' : 'text/plain' },
247- '{}' ,
248- )
249- self .assertRaises (InternalServerError , conn .api_request , 'GET' , '/' )
250-
251-
252- class Http (object ):
253-
254- _called_with = None
255-
256- def __init__ (self , headers , content ):
257- from httplib2 import Response
258- self ._response = Response (headers )
259- self ._content = content
260-
261- def request (self , ** kw ):
262- self ._called_with = kw
263- return self ._response , self ._content
0 commit comments