forked from googlemaps/google-maps-services-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_common.py
More file actions
152 lines (117 loc) · 5.18 KB
/
test_common.py
File metadata and controls
152 lines (117 loc) · 5.18 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
#
# 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 across modules (or common module)."""
import googlemaps
from googlemaps import common
import unittest
try: # Python 3
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
# NOTE: the current version of "sesponses" doesn't have request_callback.
# Use the master version until it's released.
import responses_master as responses
class CommonTest(unittest.TestCase):
def test_no_api_key(self):
with self.assertRaises(Exception):
ctx = googlemaps.Context()
googlemaps.directions(ctx, "Sydney", "Melbourne")
def test_invalid_api_key(self):
with self.assertRaises(Exception):
ctx = googlemaps.Context(key="Invalid key.")
googlemaps.directions(ctx, "Sydney", "Melbourne")
@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')
ctx = googlemaps.Context(key="AIzaasdf")
googlemaps.geocode(ctx, "Sesame St.")
self.assertEquals(1, len(responses.calls))
url = urlparse(responses.calls[0].request.url)
self.assertEquals("key=AIzaasdf&address=Sesame+St.", url.query)
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.assertEquals(signature, common._hmac_sign(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')
ctx = googlemaps.Context(client_id="foo", client_secret="a2V5")
googlemaps.geocode(ctx, "Sesame St.")
self.assertEquals(1, len(responses.calls))
url = urlparse(responses.calls[0].request.url)
expected = "client=foo&address=Sesame+St.&signature=Ao1r8ULP1g_vPwnf7Fvf2TSCYBQ="
self.assertEquals(expected, url.query)
@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')
ctx = googlemaps.Context(key="AIzaasdf")
googlemaps.geocode(ctx, "Sesame St.")
self.assertEquals(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())
ctx = googlemaps.Context(key="AIzaasdf")
googlemaps.geocode(ctx, "Sesame St.")
self.assertEquals(2, len(responses.calls))
@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())
ctx = googlemaps.Context(key="AIzaasdf")
googlemaps.geocode(ctx, "Sesame St.")
self.assertEquals(2, len(responses.calls))