33
44import copy
55import ipaddress
6+ import json
67import sys
78from typing import cast , Dict
89import unittest
910
1011sys .path .append (".." )
1112
13+ import httpretty # type: ignore
1214import geoip2
13- import requests_mock # type: ignore
1415from geoip2 .errors import (
1516 AddressNotFoundError ,
1617 AuthenticationError ,
2324from geoip2 .webservice import Client
2425
2526
27+ @httpretty .activate
2628class TestClient (unittest .TestCase ):
2729 def setUp (self ):
2830 self .client = Client (42 , "abcdef123456" )
@@ -58,13 +60,13 @@ def _content_type(self, endpoint):
5860 + "+json; charset=UTF-8; version=1.0"
5961 )
6062
61- @ requests_mock . mock ()
62- def test_country_ok ( self , mock ):
63- mock . get (
63+ def test_country_ok ( self ):
64+ httpretty . register_uri (
65+ httpretty . GET ,
6466 self .base_uri + "country/1.2.3.4" ,
65- json = self .country ,
66- status_code = 200 ,
67- headers = { "Content-Type" : self ._content_type ("country" )} ,
67+ body = json . dumps ( self .country ) ,
68+ status = 200 ,
69+ content_type = self ._content_type ("country" ),
6870 )
6971 country = self .client .country ("1.2.3.4" )
7072 self .assertEqual (
@@ -103,13 +105,13 @@ def test_country_ok(self, mock):
103105 )
104106 self .assertEqual (country .raw , self .country , "raw response is correct" )
105107
106- @ requests_mock . mock ()
107- def test_me ( self , mock ):
108- mock . get (
108+ def test_me ( self ):
109+ httpretty . register_uri (
110+ httpretty . GET ,
109111 self .base_uri + "country/me" ,
110- json = self .country ,
111- status_code = 200 ,
112- headers = { "Content-Type" : self ._content_type ("country" )} ,
112+ body = json . dumps ( self .country ) ,
113+ status = 200 ,
114+ content_type = self ._content_type ("country" ),
113115 )
114116 implicit_me = self .client .country ()
115117 self .assertEqual (
@@ -122,12 +124,13 @@ def test_me(self, mock):
122124 "country('me') returns Country object" ,
123125 )
124126
125- @ requests_mock . mock ()
126- def test_200_error ( self , mock ):
127- mock . get (
127+ def test_200_error ( self ):
128+ httpretty . register_uri (
129+ httpretty . GET ,
128130 self .base_uri + "country/1.1.1.1" ,
129- status_code = 200 ,
130- headers = {"Content-Type" : self ._content_type ("country" )},
131+ body = "" ,
132+ status = 200 ,
133+ content_type = self ._content_type ("country" ),
131134 )
132135 with self .assertRaisesRegex (
133136 GeoIP2Error , "could not decode the response as JSON"
@@ -140,145 +143,136 @@ def test_bad_ip_address(self):
140143 ):
141144 self .client .country ("1.2.3" )
142145
143- @ requests_mock . mock ()
144- def test_no_body_error ( self , mock ):
145- mock . get (
146+ def test_no_body_error ( self ):
147+ httpretty . register_uri (
148+ httpretty . GET ,
146149 self .base_uri + "country/" + "1.2.3.7" ,
147- text = "" ,
148- status_code = 400 ,
149- headers = { "Content-Type" : self ._content_type ("country" )} ,
150+ body = "" ,
151+ status = 400 ,
152+ content_type = self ._content_type ("country" ),
150153 )
151154 with self .assertRaisesRegex (
152155 HTTPError , "Received a 400 error for .* with no body"
153156 ):
154157 self .client .country ("1.2.3.7" )
155158
156- @ requests_mock . mock ()
157- def test_weird_body_error ( self , mock ):
158- mock . get (
159+ def test_weird_body_error ( self ):
160+ httpretty . register_uri (
161+ httpretty . GET ,
159162 self .base_uri + "country/" + "1.2.3.8" ,
160- text = '{"wierd": 42}' ,
161- status_code = 400 ,
162- headers = { "Content-Type" : self ._content_type ("country" )} ,
163+ body = '{"wierd": 42}' ,
164+ status = 400 ,
165+ content_type = self ._content_type ("country" ),
163166 )
164167 with self .assertRaisesRegex (
165168 HTTPError ,
166169 "Response contains JSON but it does not " "specify code or error keys" ,
167170 ):
168171 self .client .country ("1.2.3.8" )
169172
170- @ requests_mock . mock ()
171- def test_bad_body_error ( self , mock ):
172- mock . get (
173+ def test_bad_body_error ( self ):
174+ httpretty . register_uri (
175+ httpretty . GET ,
173176 self .base_uri + "country/" + "1.2.3.9" ,
174- text = "bad body" ,
175- status_code = 400 ,
176- headers = { "Content-Type" : self ._content_type ("country" )} ,
177+ body = "bad body" ,
178+ status = 400 ,
179+ content_type = self ._content_type ("country" ),
177180 )
178181 with self .assertRaisesRegex (
179182 HTTPError , "it did not include the expected JSON body"
180183 ):
181184 self .client .country ("1.2.3.9" )
182185
183- @requests_mock .mock ()
184- def test_500_error (self , mock ):
185- mock .get (self .base_uri + "country/" + "1.2.3.10" , status_code = 500 )
186+ def test_500_error (self ):
187+ httpretty .register_uri (
188+ httpretty .GET , self .base_uri + "country/" + "1.2.3.10" , status = 500
189+ )
186190 with self .assertRaisesRegex (HTTPError , r"Received a server error \(500\) for" ):
187191 self .client .country ("1.2.3.10" )
188192
189- @ requests_mock . mock ()
190- def test_300_error ( self , mock ):
191- mock . get (
193+ def test_300_error ( self ):
194+ httpretty . register_uri (
195+ httpretty . GET ,
192196 self .base_uri + "country/" + "1.2.3.11" ,
193- status_code = 300 ,
194- headers = { "Content-Type" : self ._content_type ("country" )} ,
197+ status = 300 ,
198+ content_type = self ._content_type ("country" ),
195199 )
196200 with self .assertRaisesRegex (
197201 HTTPError , r"Received a very surprising HTTP status \(300\) for"
198202 ):
199203
200204 self .client .country ("1.2.3.11" )
201205
202- @requests_mock .mock ()
203- def test_ip_address_required (self , mock ):
204- self ._test_error (mock , 400 , "IP_ADDRESS_REQUIRED" , InvalidRequestError )
206+ def test_ip_address_required (self ):
207+ self ._test_error (400 , "IP_ADDRESS_REQUIRED" , InvalidRequestError )
205208
206- @requests_mock .mock ()
207- def test_ip_address_not_found (self , mock ):
208- self ._test_error (mock , 404 , "IP_ADDRESS_NOT_FOUND" , AddressNotFoundError )
209+ def test_ip_address_not_found (self ):
210+ self ._test_error (404 , "IP_ADDRESS_NOT_FOUND" , AddressNotFoundError )
209211
210- @requests_mock .mock ()
211- def test_ip_address_reserved (self , mock ):
212- self ._test_error (mock , 400 , "IP_ADDRESS_RESERVED" , AddressNotFoundError )
212+ def test_ip_address_reserved (self ):
213+ self ._test_error (400 , "IP_ADDRESS_RESERVED" , AddressNotFoundError )
213214
214- @requests_mock .mock ()
215- def test_permission_required (self , mock ):
216- self ._test_error (mock , 403 , "PERMISSION_REQUIRED" , PermissionRequiredError )
215+ def test_permission_required (self ):
216+ self ._test_error (403 , "PERMISSION_REQUIRED" , PermissionRequiredError )
217217
218- @requests_mock .mock ()
219- def test_auth_invalid (self , mock ):
220- self ._test_error (mock , 400 , "AUTHORIZATION_INVALID" , AuthenticationError )
218+ def test_auth_invalid (self ):
219+ self ._test_error (400 , "AUTHORIZATION_INVALID" , AuthenticationError )
221220
222- @requests_mock .mock ()
223- def test_license_key_required (self , mock ):
224- self ._test_error (mock , 401 , "LICENSE_KEY_REQUIRED" , AuthenticationError )
221+ def test_license_key_required (self ):
222+ self ._test_error (401 , "LICENSE_KEY_REQUIRED" , AuthenticationError )
225223
226- @requests_mock .mock ()
227- def test_account_id_required (self , mock ):
228- self ._test_error (mock , 401 , "ACCOUNT_ID_REQUIRED" , AuthenticationError )
224+ def test_account_id_required (self ):
225+ self ._test_error (401 , "ACCOUNT_ID_REQUIRED" , AuthenticationError )
229226
230- @requests_mock .mock ()
231- def test_user_id_required (self , mock ):
232- self ._test_error (mock , 401 , "USER_ID_REQUIRED" , AuthenticationError )
227+ def test_user_id_required (self ):
228+ self ._test_error (401 , "USER_ID_REQUIRED" , AuthenticationError )
233229
234- @requests_mock .mock ()
235- def test_account_id_unkown (self , mock ):
236- self ._test_error (mock , 401 , "ACCOUNT_ID_UNKNOWN" , AuthenticationError )
230+ def test_account_id_unkown (self ):
231+ self ._test_error (401 , "ACCOUNT_ID_UNKNOWN" , AuthenticationError )
237232
238- @requests_mock .mock ()
239- def test_user_id_unkown (self , mock ):
240- self ._test_error (mock , 401 , "USER_ID_UNKNOWN" , AuthenticationError )
233+ def test_user_id_unkown (self ):
234+ self ._test_error (401 , "USER_ID_UNKNOWN" , AuthenticationError )
241235
242- @requests_mock .mock ()
243- def test_out_of_queries_error (self , mock ):
244- self ._test_error (mock , 402 , "OUT_OF_QUERIES" , OutOfQueriesError )
236+ def test_out_of_queries_error (self ):
237+ self ._test_error (402 , "OUT_OF_QUERIES" , OutOfQueriesError )
245238
246- def _test_error (self , mock , status , error_code , error_class ):
239+ def _test_error (self , status , error_code , error_class ):
247240 msg = "Some error message"
248241 body = {"error" : msg , "code" : error_code }
249- mock .get (
242+ httpretty .register_uri (
243+ httpretty .GET ,
250244 self .base_uri + "country/1.2.3.18" ,
251- json = body ,
252- status_code = status ,
253- headers = { "Content-Type" : self ._content_type ("country" )} ,
245+ body = json . dumps ( body ) ,
246+ status = status ,
247+ content_type = self ._content_type ("country" ),
254248 )
255249 with self .assertRaisesRegex (error_class , msg ):
256250 self .client .country ("1.2.3.18" )
257251
258- @requests_mock .mock ()
259- def test_unknown_error (self , mock ):
252+ def test_unknown_error (self ):
260253 msg = "Unknown error type"
261254 ip = "1.2.3.19"
262255 body = {"error" : msg , "code" : "UNKNOWN_TYPE" }
263- mock .get (
256+ httpretty .register_uri (
257+ httpretty .GET ,
264258 self .base_uri + "country/" + ip ,
265- json = body ,
266- status_code = 400 ,
267- headers = { "Content-Type" : self ._content_type ("country" )} ,
259+ body = json . dumps ( body ) ,
260+ status = 400 ,
261+ content_type = self ._content_type ("country" ),
268262 )
269263 with self .assertRaisesRegex (InvalidRequestError , msg ):
270264 self .client .country (ip )
271265
272- @ requests_mock . mock ()
273- def test_request ( self , mock ):
274- mock . get (
266+ def test_request ( self ):
267+ httpretty . register_uri (
268+ httpretty . GET ,
275269 self .base_uri + "country/" + "1.2.3.4" ,
276- json = self .country ,
277- status_code = 200 ,
278- headers = { "Content-Type" : self ._content_type ("country" )} ,
270+ body = json . dumps ( self .country ) ,
271+ status = 200 ,
272+ content_type = self ._content_type ("country" ),
279273 )
280274 self .client .country ("1.2.3.4" )
281- request = mock . request_history [- 1 ]
275+ request = httpretty . latest_requests () [- 1 ]
282276
283277 self .assertEqual (
284278 request .path , "/geoip/v2.1/country/1.2.3.4" , "correct URI is used"
@@ -297,27 +291,27 @@ def test_request(self, mock):
297291 "correct auth" ,
298292 )
299293
300- @ requests_mock . mock ()
301- def test_city_ok ( self , mock ):
302- mock . get (
294+ def test_city_ok ( self ):
295+ httpretty . register_uri (
296+ httpretty . GET ,
303297 self .base_uri + "city/" + "1.2.3.4" ,
304- json = self .country ,
305- status_code = 200 ,
306- headers = { "Content-Type" : self ._content_type ("city" )} ,
298+ body = json . dumps ( self .country ) ,
299+ status = 200 ,
300+ content_type = self ._content_type ("city" ),
307301 )
308302 city = self .client .city ("1.2.3.4" )
309303 self .assertEqual (type (city ), geoip2 .models .City , "return value of client.city" )
310304 self .assertEqual (
311305 city .traits .network , ipaddress .ip_network ("1.2.3.0/24" ), "network"
312306 )
313307
314- @ requests_mock . mock ()
315- def test_insights_ok ( self , mock ):
316- mock . get (
308+ def test_insights_ok ( self ):
309+ httpretty . register_uri (
310+ httpretty . GET ,
317311 self .base_uri + "insights/1.2.3.4" ,
318- json = self .insights ,
319- status_code = 200 ,
320- headers = { "Content-Type" : self ._content_type ("country" )} ,
312+ body = json . dumps ( self .insights ) ,
313+ status = 200 ,
314+ content_type = self ._content_type ("country" ),
321315 )
322316 insights = self .client .insights ("1.2.3.4" )
323317 self .assertEqual (
0 commit comments