Skip to content

Commit 47b008d

Browse files
committed
Remove mocket
1 parent 214a229 commit 47b008d

File tree

4 files changed

+60
-91
lines changed

4 files changed

+60
-91
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
# We don't test on Windows currently as it appears mocket may not
15-
# work there.
14+
# TODO: add windows-latest also
1615
platform: [ubuntu-latest, macos-latest]
1716
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]
1817

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers = [
3737

3838
[project.optional-dependencies]
3939
test = [
40-
"mocket>=3.12.8",
40+
"pytest-httpserver>=1.0.10",
4141
]
4242

4343
[tool.setuptools.package-data]

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ python =
2424

2525
[testenv:{py38,py39,py310,py311,py312}-test]
2626
deps =
27-
mocket
27+
pytest-httpserver
2828
pytest
2929
commands = pytest tests
3030

tests/webservice_test.py

Lines changed: 57 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
import sys
99
from typing import cast, Dict
1010
import unittest
11+
import requests
12+
import pytest_httpserver
13+
import pytest
1114

1215
sys.path.append("..")
13-
14-
# httpretty currently doesn't work, but mocket with the compat interface
15-
# does.
16-
from mocket import Mocket # type: ignore
17-
from mocket.plugins.httpretty import httpretty, httprettified # type: ignore
1816
import geoip2
1917
from geoip2.errors import (
2018
AddressNotFoundError,
@@ -29,7 +27,7 @@
2927

3028

3129
class TestBaseClient(unittest.TestCase):
32-
base_uri = "https://geoip.maxmind.com/geoip/v2.1/"
30+
# base_uri = "https://geoip.maxmind.com/geoip/v2.1/"
3331
country = {
3432
"continent": {"code": "NA", "geoname_id": 42, "names": {"en": "North America"}},
3533
"country": {
@@ -64,12 +62,13 @@ def _content_type(self, endpoint):
6462
+ "+json; charset=UTF-8; version=1.0"
6563
)
6664

67-
@httprettified
65+
@pytest.fixture(autouse=True)
66+
def setup_httpserver(self, httpserver: pytest_httpserver.HTTPServer):
67+
self.httpserver = httpserver
68+
6869
def test_country_ok(self):
69-
httpretty.register_uri(
70-
httpretty.GET,
71-
self.base_uri + "country/1.2.3.4",
72-
body=json.dumps(self.country),
70+
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json(
71+
self.country,
7372
status=200,
7473
content_type=self._content_type("country"),
7574
)
@@ -111,12 +110,9 @@ def test_country_ok(self):
111110
self.assertTrue(country.traits.is_anycast)
112111
self.assertEqual(country.raw, self.country, "raw response is correct")
113112

114-
@httprettified
115113
def test_me(self):
116-
httpretty.register_uri(
117-
httpretty.GET,
118-
self.base_uri + "country/me",
119-
body=json.dumps(self.country),
114+
self.httpserver.expect_request("/geoip/v2.1/country/me").respond_with_json(
115+
self.country,
120116
status=200,
121117
content_type=self._content_type("country"),
122118
)
@@ -131,33 +127,27 @@ def test_me(self):
131127
"country('me') returns Country object",
132128
)
133129

134-
@httprettified
135130
def test_200_error(self):
136-
httpretty.register_uri(
137-
httpretty.GET,
138-
self.base_uri + "country/1.1.1.1",
139-
body="",
131+
self.httpserver.expect_request("/geoip/v2.1/country/1.1.1.1").respond_with_data(
132+
"",
140133
status=200,
141134
content_type=self._content_type("country"),
142135
)
136+
143137
with self.assertRaisesRegex(
144138
GeoIP2Error, "could not decode the response as JSON"
145139
):
146140
self.run_client(self.client.country("1.1.1.1"))
147141

148-
@httprettified
149142
def test_bad_ip_address(self):
150143
with self.assertRaisesRegex(
151144
ValueError, "'1.2.3' does not appear to be an IPv4 " "or IPv6 address"
152145
):
153146
self.run_client(self.client.country("1.2.3"))
154147

155-
@httprettified
156148
def test_no_body_error(self):
157-
httpretty.register_uri(
158-
httpretty.GET,
159-
self.base_uri + "country/" + "1.2.3.7",
160-
body="",
149+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.7").respond_with_data(
150+
"",
161151
status=400,
162152
content_type=self._content_type("country"),
163153
)
@@ -166,27 +156,24 @@ def test_no_body_error(self):
166156
):
167157
self.run_client(self.client.country("1.2.3.7"))
168158

169-
@httprettified
170159
def test_weird_body_error(self):
171-
httpretty.register_uri(
172-
httpretty.GET,
173-
self.base_uri + "country/" + "1.2.3.8",
174-
body='{"wierd": 42}',
160+
161+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.8").respond_with_json(
162+
{"wierd": 42},
175163
status=400,
176164
content_type=self._content_type("country"),
177165
)
166+
178167
with self.assertRaisesRegex(
179168
HTTPError,
180169
"Response contains JSON but it does not " "specify code or error keys",
181170
):
182171
self.run_client(self.client.country("1.2.3.8"))
183172

184-
@httprettified
185173
def test_bad_body_error(self):
186-
httpretty.register_uri(
187-
httpretty.GET,
188-
self.base_uri + "country/" + "1.2.3.9",
189-
body="bad body",
174+
175+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.9").respond_with_data(
176+
"bad body",
190177
status=400,
191178
content_type=self._content_type("country"),
192179
)
@@ -195,19 +182,19 @@ def test_bad_body_error(self):
195182
):
196183
self.run_client(self.client.country("1.2.3.9"))
197184

198-
@httprettified
199185
def test_500_error(self):
200-
httpretty.register_uri(
201-
httpretty.GET, self.base_uri + "country/" + "1.2.3.10", status=500
186+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.10").respond_with_data(
187+
"",
188+
status=500,
189+
content_type=self._content_type("country"),
202190
)
203191
with self.assertRaisesRegex(HTTPError, r"Received a server error \(500\) for"):
204192
self.run_client(self.client.country("1.2.3.10"))
205193

206-
@httprettified
207194
def test_300_error(self):
208-
httpretty.register_uri(
209-
httpretty.GET,
210-
self.base_uri + "country/" + "1.2.3.11",
195+
196+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.11").respond_with_data(
197+
"",
211198
status=300,
212199
content_type=self._content_type("country"),
213200
)
@@ -216,96 +203,83 @@ def test_300_error(self):
216203
):
217204
self.run_client(self.client.country("1.2.3.11"))
218205

219-
@httprettified
220206
def test_ip_address_required(self):
221207
self._test_error(400, "IP_ADDRESS_REQUIRED", InvalidRequestError)
222208

223-
@httprettified
224209
def test_ip_address_not_found(self):
225210
self._test_error(404, "IP_ADDRESS_NOT_FOUND", AddressNotFoundError)
226211

227-
@httprettified
228212
def test_ip_address_reserved(self):
229213
self._test_error(400, "IP_ADDRESS_RESERVED", AddressNotFoundError)
230214

231-
@httprettified
232215
def test_permission_required(self):
233216
self._test_error(403, "PERMISSION_REQUIRED", PermissionRequiredError)
234217

235-
@httprettified
236218
def test_auth_invalid(self):
237219
self._test_error(400, "AUTHORIZATION_INVALID", AuthenticationError)
238220

239-
@httprettified
240221
def test_license_key_required(self):
241222
self._test_error(401, "LICENSE_KEY_REQUIRED", AuthenticationError)
242223

243-
@httprettified
244224
def test_account_id_required(self):
245225
self._test_error(401, "ACCOUNT_ID_REQUIRED", AuthenticationError)
246226

247-
@httprettified
248227
def test_user_id_required(self):
249228
self._test_error(401, "USER_ID_REQUIRED", AuthenticationError)
250229

251-
@httprettified
252230
def test_account_id_unkown(self):
253231
self._test_error(401, "ACCOUNT_ID_UNKNOWN", AuthenticationError)
254232

255-
@httprettified
256233
def test_user_id_unkown(self):
257234
self._test_error(401, "USER_ID_UNKNOWN", AuthenticationError)
258235

259-
@httprettified
260236
def test_out_of_queries_error(self):
261237
self._test_error(402, "OUT_OF_QUERIES", OutOfQueriesError)
262238

263239
def _test_error(self, status, error_code, error_class):
264240
msg = "Some error message"
265241
body = {"error": msg, "code": error_code}
266-
httpretty.register_uri(
267-
httpretty.GET,
268-
self.base_uri + "country/1.2.3.18",
269-
body=json.dumps(body),
242+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.18").respond_with_json(
243+
body,
270244
status=status,
271245
content_type=self._content_type("country"),
272246
)
273247
with self.assertRaisesRegex(error_class, msg):
274248
self.run_client(self.client.country("1.2.3.18"))
275249

276-
@httprettified
250+
277251
def test_unknown_error(self):
278252
msg = "Unknown error type"
279253
ip = "1.2.3.19"
280254
body = {"error": msg, "code": "UNKNOWN_TYPE"}
281-
httpretty.register_uri(
282-
httpretty.GET,
283-
self.base_uri + "country/" + ip,
284-
body=json.dumps(body),
255+
self.httpserver.expect_request("/geoip/v2.1/country/" + ip).respond_with_json(
256+
body,
285257
status=400,
286258
content_type=self._content_type("country"),
287259
)
288260
with self.assertRaisesRegex(InvalidRequestError, msg):
289261
self.run_client(self.client.country(ip))
290262

291-
@httprettified
292263
def test_request(self):
293-
httpretty.register_uri(
294-
httpretty.GET,
295-
self.base_uri + "country/" + "1.2.3.4",
296-
body=json.dumps(self.country),
297-
status=200,
298-
content_type=self._content_type("country"),
299-
)
300-
self.run_client(self.client.country("1.2.3.4"))
301-
request = httpretty.last_request
264+
265+
request = None
266+
def custom_handler(r):
267+
nonlocal request
268+
request = r
269+
return ""
270+
self.httpserver.expect_request("/geoip/v2.1/country/" + "1.2.3.4").respond_with_handler(custom_handler)
271+
try:
272+
self.run_client(self.client.country("1.2.3.4"))
273+
except Exception as e:
274+
# just to avoid the exception
275+
pass
276+
277+
302278

303279
self.assertEqual(
304280
request.path, "/geoip/v2.1/country/1.2.3.4", "correct URI is used"
305281
)
306282

307-
# This is to prevent breakage if header normalization in Mocket
308-
# changes again in the future.
309283
headers = {k.lower(): v for k, v in request.headers.items()}
310284
self.assertEqual(headers["accept"], "application/json", "correct Accept header")
311285
self.assertRegex(
@@ -319,12 +293,9 @@ def test_request(self):
319293
"correct auth",
320294
)
321295

322-
@httprettified
323296
def test_city_ok(self):
324-
httpretty.register_uri(
325-
httpretty.GET,
326-
self.base_uri + "city/" + "1.2.3.4",
327-
body=json.dumps(self.country),
297+
self.httpserver.expect_request("/geoip/v2.1/city/1.2.3.4").respond_with_json(
298+
self.country,
328299
status=200,
329300
content_type=self._content_type("city"),
330301
)
@@ -335,14 +306,11 @@ def test_city_ok(self):
335306
)
336307
self.assertTrue(city.traits.is_anycast)
337308

338-
@httprettified
339309
def test_insights_ok(self):
340-
httpretty.register_uri(
341-
httpretty.GET,
342-
self.base_uri + "insights/1.2.3.4",
343-
body=json.dumps(self.insights),
310+
self.httpserver.expect_request("/geoip/v2.1/insights/" + "1.2.3.4").respond_with_json(
311+
self.insights,
344312
status=200,
345-
content_type=self._content_type("country"),
313+
content_type=self._content_type("insights"),
346314
)
347315
insights = self.run_client(self.client.insights("1.2.3.4"))
348316
self.assertEqual(
@@ -374,6 +342,7 @@ class TestClient(TestBaseClient):
374342
def setUp(self):
375343
self.client_class = Client
376344
self.client = Client(42, "abcdef123456")
345+
self.client._base_uri = self.httpserver.url_for("/")+ "geoip/v2.1"
377346

378347
def run_client(self, v):
379348
return v
@@ -384,6 +353,7 @@ def setUp(self):
384353
self._loop = asyncio.new_event_loop()
385354
self.client_class = AsyncClient
386355
self.client = AsyncClient(42, "abcdef123456")
356+
self.client._base_uri = self.httpserver.url_for("/")+ "geoip/v2.1"
387357

388358
def tearDown(self):
389359
self._loop.run_until_complete(self.client.close())

0 commit comments

Comments
 (0)