forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
432 lines (354 loc) · 14.2 KB
/
test_client.py
File metadata and controls
432 lines (354 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#
# Copyright 2014 Google Inc. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
"""Tests for client module."""
import time
import responses
import requests
import uuid
import googlemaps
import googlemaps.client as _client
from . import TestCase
from googlemaps.client import _X_GOOG_MAPS_EXPERIENCE_ID
class ClientTest(TestCase):
def test_no_api_key(self):
with self.assertRaises(Exception):
client = googlemaps.Client()
client.directions("Sydney", "Melbourne")
def test_invalid_api_key(self):
with self.assertRaises(Exception):
client = googlemaps.Client(key="Invalid key.")
client.directions("Sydney", "Melbourne")
def test_urlencode(self):
# See GH #72.
encoded_params = _client.urlencode_params([("address", "=Sydney ~")])
self.assertEqual("address=%3DSydney+~", encoded_params)
@responses.activate
def test_queries_per_second(self):
# This test assumes that the time to run a mocked query is
# relatively small, eg a few milliseconds. We define a rate of
# 3 queries per second, and run double that, which should take at
# least 1 second but no more than 2.
queries_per_second = 3
query_range = range(queries_per_second * 2)
for _ in query_range:
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(
key="AIzaasdf", queries_per_second=queries_per_second
)
start = time.time()
for _ in query_range:
client.geocode("Sesame St.")
end = time.time()
self.assertTrue(start + 1 < end < start + 2)
@responses.activate
def test_key_sent(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
client.geocode("Sesame St.")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=AIzaasdf&address=Sesame+St.",
responses.calls[0].request.url,
)
@responses.activate
def test_extra_params(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
client.geocode("Sesame St.", extra_params={"foo": "bar"})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=AIzaasdf&address=Sesame+St.&foo=bar",
responses.calls[0].request.url,
)
def test_hmac(self):
"""
From http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
HMAC_SHA1("key", "The quick brown fox jumps over the lazy dog")
= 0xde7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
"""
message = "The quick brown fox jumps over the lazy dog"
key = "a2V5" # "key" -> base64
signature = "3nybhbi3iqa8ino29wqQcBydtNk="
self.assertEqual(signature, _client.sign_hmac(key, message))
@responses.activate
def test_url_signed(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(client_id="foo", client_secret="a2V5")
client.geocode("Sesame St.")
self.assertEqual(1, len(responses.calls))
# Check ordering of parameters.
self.assertIn(
"address=Sesame+St.&client=foo&signature", responses.calls[0].request.url
)
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"address=Sesame+St.&client=foo&"
"signature=fxbWUIcNPZSekVOhp2ul9LW5TpY=",
responses.calls[0].request.url,
)
@responses.activate
def test_ua_sent(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
client.geocode("Sesame St.")
self.assertEqual(1, len(responses.calls))
user_agent = responses.calls[0].request.headers["User-Agent"]
self.assertTrue(user_agent.startswith("GoogleGeoApiClientPython"))
@responses.activate
def test_retry(self):
class request_callback:
def __init__(self):
self.first_req = True
def __call__(self, req):
if self.first_req:
self.first_req = False
return (200, {}, '{"status":"OVER_QUERY_LIMIT"}')
return (200, {}, '{"status":"OK","results":[]}')
responses.add_callback(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
content_type="application/json",
callback=request_callback(),
)
client = googlemaps.Client(key="AIzaasdf")
client.geocode("Sesame St.")
self.assertEqual(2, len(responses.calls))
self.assertEqual(responses.calls[0].request.url, responses.calls[1].request.url)
@responses.activate
def test_transport_error(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
status=404,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
with self.assertRaises(googlemaps.exceptions.HTTPError) as e:
client.geocode("Foo")
self.assertEqual(e.exception.status_code, 404)
@responses.activate
def test_host_override_on_init(self):
responses.add(
responses.GET,
"https://foo.com/bar",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf", base_url="https://foo.com")
client._get("/bar", {})
self.assertEqual(1, len(responses.calls))
@responses.activate
def test_host_override_per_request(self):
responses.add(
responses.GET,
"https://foo.com/bar",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
client._get("/bar", {}, base_url="https://foo.com")
self.assertEqual(1, len(responses.calls))
@responses.activate
def test_custom_extract(self):
def custom_extract(resp):
return resp.json()
responses.add(
responses.GET,
"https://maps.googleapis.com/bar",
body='{"error":"errormessage"}',
status=403,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf")
b = client._get("/bar", {}, extract_body=custom_extract)
self.assertEqual(1, len(responses.calls))
self.assertEqual("errormessage", b["error"])
@responses.activate
def test_retry_intermittent(self):
class request_callback:
def __init__(self):
self.first_req = True
def __call__(self, req):
if self.first_req:
self.first_req = False
return (500, {}, "Internal Server Error.")
return (200, {}, '{"status":"OK","results":[]}')
responses.add_callback(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
content_type="application/json",
callback=request_callback(),
)
client = googlemaps.Client(key="AIzaasdf")
client.geocode("Sesame St.")
self.assertEqual(2, len(responses.calls))
def test_invalid_channel(self):
# Cf. limitations here:
# https://developers.google.com/maps/premium/reports
# /usage-reports#channels
with self.assertRaises(ValueError):
client = googlemaps.Client(
client_id="foo", client_secret="a2V5", channel="auieauie$? "
)
def test_auth_url_with_channel(self):
client = googlemaps.Client(
key="AIzaasdf", client_id="foo", client_secret="a2V5", channel="MyChannel_1"
)
# Check ordering of parameters + signature.
auth_url = client._generate_auth_url(
"/test", {"param": "param"}, accepts_clientid=True
)
self.assertEqual(
auth_url,
"/test?param=param"
"&channel=MyChannel_1"
"&client=foo"
"&signature=OH18GuQto_mEpxj99UimKskvo4k=",
)
# Check if added to requests to API with accepts_clientid=False
auth_url = client._generate_auth_url(
"/test", {"param": "param"}, accepts_clientid=False
)
self.assertEqual(auth_url, "/test?param=param&key=AIzaasdf")
def test_requests_version(self):
client_args_timeout = {
"key": "AIzaasdf",
"client_id": "foo",
"client_secret": "a2V5",
"channel": "MyChannel_1",
"connect_timeout": 5,
"read_timeout": 5,
}
client_args = client_args_timeout.copy()
del client_args["connect_timeout"]
del client_args["read_timeout"]
requests.__version__ = "2.3.0"
with self.assertRaises(NotImplementedError):
googlemaps.Client(**client_args_timeout)
googlemaps.Client(**client_args)
requests.__version__ = "2.4.0"
googlemaps.Client(**client_args_timeout)
googlemaps.Client(**client_args)
def test_single_experience_id(self):
experience_id1 = "Exp1"
client = googlemaps.Client(key="AIzaasdf", experience_id=experience_id1)
self.assertEqual(experience_id1, client.get_experience_id())
experience_id2 = "Exp2"
client.set_experience_id(experience_id2)
self.assertEqual(experience_id2, client.get_experience_id())
def test_multiple_experience_id(self):
client = googlemaps.Client(key="AIzaasdf")
experience_id1 = "Exp1"
experience_id2 = "Exp2"
client.set_experience_id(experience_id1, experience_id2)
result = "%s,%s" % (experience_id1, experience_id2)
self.assertEqual(result, client.get_experience_id())
def test_no_experience_id(self):
client = googlemaps.Client(key="AIzaasdf")
self.assertIsNone(client.get_experience_id())
def test_clearing_experience_id(self):
client = googlemaps.Client(key="AIzaasdf")
client.set_experience_id("ExpId")
client.clear_experience_id()
self.assertIsNone(client.get_experience_id())
def test_experience_id_sample(self):
# [START maps_experience_id]
experience_id = str(uuid.uuid4())
# instantiate client with experience id
client = googlemaps.Client(key="AIza-Maps-API-Key", experience_id=experience_id)
# clear the current experience id
client.clear_experience_id()
# set a new experience id
other_experience_id = str(uuid.uuid4())
client.set_experience_id(experience_id, other_experience_id)
# make API request, the client will set the header
# X-GOOG-MAPS-EXPERIENCE-ID: experience_id,other_experience_id
# get current experience id
ids = client.get_experience_id()
# [END maps_experience_id]
result = "%s,%s" % (experience_id, other_experience_id)
self.assertEqual(result, ids)
@responses.activate
def _perform_mock_request(self, experience_id=None):
# Mock response
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
# Perform network call
client = googlemaps.Client(key="AIzaasdf")
client.set_experience_id(experience_id)
client.geocode("Sesame St.")
return responses.calls[0].request
def test_experience_id_in_header(self):
experience_id = "Exp1"
request = self._perform_mock_request(experience_id)
header_value = request.headers[_X_GOOG_MAPS_EXPERIENCE_ID]
self.assertEqual(experience_id, header_value)
def test_experience_id_no_in_header(self):
request = self._perform_mock_request()
self.assertIsNone(request.headers.get(_X_GOOG_MAPS_EXPERIENCE_ID))
@responses.activate
def test_no_retry_over_query_limit(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/foo",
body='{"status":"OVER_QUERY_LIMIT"}',
status=200,
content_type="application/json",
)
client = googlemaps.Client(key="AIzaasdf", retry_over_query_limit=False)
with self.assertRaises(googlemaps.exceptions.ApiError):
client._request("/foo", {})
self.assertEqual(1, len(responses.calls))