Skip to content

Commit 55f57b3

Browse files
committed
Remove mocket
1 parent 214a229 commit 55f57b3

File tree

4 files changed

+74
-91
lines changed

4 files changed

+74
-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: 71 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,6 @@
2927

3028

3129
class TestBaseClient(unittest.TestCase):
32-
base_uri = "https://geoip.maxmind.com/geoip/v2.1/"
3330
country = {
3431
"continent": {"code": "NA", "geoname_id": 42, "names": {"en": "North America"}},
3532
"country": {
@@ -64,12 +61,13 @@ def _content_type(self, endpoint):
6461
+ "+json; charset=UTF-8; version=1.0"
6562
)
6663

67-
@httprettified
64+
@pytest.fixture(autouse=True)
65+
def setup_httpserver(self, httpserver: pytest_httpserver.HTTPServer):
66+
self.httpserver = httpserver
67+
6868
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),
69+
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json(
70+
self.country,
7371
status=200,
7472
content_type=self._content_type("country"),
7573
)
@@ -111,12 +109,9 @@ def test_country_ok(self):
111109
self.assertTrue(country.traits.is_anycast)
112110
self.assertEqual(country.raw, self.country, "raw response is correct")
113111

114-
@httprettified
115112
def test_me(self):
116-
httpretty.register_uri(
117-
httpretty.GET,
118-
self.base_uri + "country/me",
119-
body=json.dumps(self.country),
113+
self.httpserver.expect_request("/geoip/v2.1/country/me").respond_with_json(
114+
self.country,
120115
status=200,
121116
content_type=self._content_type("country"),
122117
)
@@ -131,33 +126,29 @@ def test_me(self):
131126
"country('me') returns Country object",
132127
)
133128

134-
@httprettified
135129
def test_200_error(self):
136-
httpretty.register_uri(
137-
httpretty.GET,
138-
self.base_uri + "country/1.1.1.1",
139-
body="",
130+
self.httpserver.expect_request("/geoip/v2.1/country/1.1.1.1").respond_with_data(
131+
"",
140132
status=200,
141133
content_type=self._content_type("country"),
142134
)
135+
143136
with self.assertRaisesRegex(
144137
GeoIP2Error, "could not decode the response as JSON"
145138
):
146139
self.run_client(self.client.country("1.1.1.1"))
147140

148-
@httprettified
149141
def test_bad_ip_address(self):
150142
with self.assertRaisesRegex(
151143
ValueError, "'1.2.3' does not appear to be an IPv4 " "or IPv6 address"
152144
):
153145
self.run_client(self.client.country("1.2.3"))
154146

155-
@httprettified
156147
def test_no_body_error(self):
157-
httpretty.register_uri(
158-
httpretty.GET,
159-
self.base_uri + "country/" + "1.2.3.7",
160-
body="",
148+
self.httpserver.expect_request(
149+
"/geoip/v2.1/country/" + "1.2.3.7"
150+
).respond_with_data(
151+
"",
161152
status=400,
162153
content_type=self._content_type("country"),
163154
)
@@ -166,27 +157,28 @@ def test_no_body_error(self):
166157
):
167158
self.run_client(self.client.country("1.2.3.7"))
168159

169-
@httprettified
170160
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}',
161+
162+
self.httpserver.expect_request(
163+
"/geoip/v2.1/country/" + "1.2.3.8"
164+
).respond_with_json(
165+
{"wierd": 42},
175166
status=400,
176167
content_type=self._content_type("country"),
177168
)
169+
178170
with self.assertRaisesRegex(
179171
HTTPError,
180172
"Response contains JSON but it does not " "specify code or error keys",
181173
):
182174
self.run_client(self.client.country("1.2.3.8"))
183175

184-
@httprettified
185176
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",
177+
178+
self.httpserver.expect_request(
179+
"/geoip/v2.1/country/" + "1.2.3.9"
180+
).respond_with_data(
181+
"bad body",
190182
status=400,
191183
content_type=self._content_type("country"),
192184
)
@@ -195,19 +187,23 @@ def test_bad_body_error(self):
195187
):
196188
self.run_client(self.client.country("1.2.3.9"))
197189

198-
@httprettified
199190
def test_500_error(self):
200-
httpretty.register_uri(
201-
httpretty.GET, self.base_uri + "country/" + "1.2.3.10", status=500
191+
self.httpserver.expect_request(
192+
"/geoip/v2.1/country/" + "1.2.3.10"
193+
).respond_with_data(
194+
"",
195+
status=500,
196+
content_type=self._content_type("country"),
202197
)
203198
with self.assertRaisesRegex(HTTPError, r"Received a server error \(500\) for"):
204199
self.run_client(self.client.country("1.2.3.10"))
205200

206-
@httprettified
207201
def test_300_error(self):
208-
httpretty.register_uri(
209-
httpretty.GET,
210-
self.base_uri + "country/" + "1.2.3.11",
202+
203+
self.httpserver.expect_request(
204+
"/geoip/v2.1/country/" + "1.2.3.11"
205+
).respond_with_data(
206+
"",
211207
status=300,
212208
content_type=self._content_type("country"),
213209
)
@@ -216,96 +212,86 @@ def test_300_error(self):
216212
):
217213
self.run_client(self.client.country("1.2.3.11"))
218214

219-
@httprettified
220215
def test_ip_address_required(self):
221216
self._test_error(400, "IP_ADDRESS_REQUIRED", InvalidRequestError)
222217

223-
@httprettified
224218
def test_ip_address_not_found(self):
225219
self._test_error(404, "IP_ADDRESS_NOT_FOUND", AddressNotFoundError)
226220

227-
@httprettified
228221
def test_ip_address_reserved(self):
229222
self._test_error(400, "IP_ADDRESS_RESERVED", AddressNotFoundError)
230223

231-
@httprettified
232224
def test_permission_required(self):
233225
self._test_error(403, "PERMISSION_REQUIRED", PermissionRequiredError)
234226

235-
@httprettified
236227
def test_auth_invalid(self):
237228
self._test_error(400, "AUTHORIZATION_INVALID", AuthenticationError)
238229

239-
@httprettified
240230
def test_license_key_required(self):
241231
self._test_error(401, "LICENSE_KEY_REQUIRED", AuthenticationError)
242232

243-
@httprettified
244233
def test_account_id_required(self):
245234
self._test_error(401, "ACCOUNT_ID_REQUIRED", AuthenticationError)
246235

247-
@httprettified
248236
def test_user_id_required(self):
249237
self._test_error(401, "USER_ID_REQUIRED", AuthenticationError)
250238

251-
@httprettified
252239
def test_account_id_unkown(self):
253240
self._test_error(401, "ACCOUNT_ID_UNKNOWN", AuthenticationError)
254241

255-
@httprettified
256242
def test_user_id_unkown(self):
257243
self._test_error(401, "USER_ID_UNKNOWN", AuthenticationError)
258244

259-
@httprettified
260245
def test_out_of_queries_error(self):
261246
self._test_error(402, "OUT_OF_QUERIES", OutOfQueriesError)
262247

263248
def _test_error(self, status, error_code, error_class):
264249
msg = "Some error message"
265250
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),
251+
self.httpserver.expect_request(
252+
"/geoip/v2.1/country/" + "1.2.3.18"
253+
).respond_with_json(
254+
body,
270255
status=status,
271256
content_type=self._content_type("country"),
272257
)
273258
with self.assertRaisesRegex(error_class, msg):
274259
self.run_client(self.client.country("1.2.3.18"))
275260

276-
@httprettified
277261
def test_unknown_error(self):
278262
msg = "Unknown error type"
279263
ip = "1.2.3.19"
280264
body = {"error": msg, "code": "UNKNOWN_TYPE"}
281-
httpretty.register_uri(
282-
httpretty.GET,
283-
self.base_uri + "country/" + ip,
284-
body=json.dumps(body),
265+
self.httpserver.expect_request("/geoip/v2.1/country/" + ip).respond_with_json(
266+
body,
285267
status=400,
286268
content_type=self._content_type("country"),
287269
)
288270
with self.assertRaisesRegex(InvalidRequestError, msg):
289271
self.run_client(self.client.country(ip))
290272

291-
@httprettified
292273
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
274+
275+
request = None
276+
277+
def custom_handler(r):
278+
nonlocal request
279+
request = r
280+
return ""
281+
282+
self.httpserver.expect_request(
283+
"/geoip/v2.1/country/" + "1.2.3.4"
284+
).respond_with_handler(custom_handler)
285+
try:
286+
self.run_client(self.client.country("1.2.3.4"))
287+
except Exception as e:
288+
# just to avoid the exception
289+
pass
302290

303291
self.assertEqual(
304292
request.path, "/geoip/v2.1/country/1.2.3.4", "correct URI is used"
305293
)
306294

307-
# This is to prevent breakage if header normalization in Mocket
308-
# changes again in the future.
309295
headers = {k.lower(): v for k, v in request.headers.items()}
310296
self.assertEqual(headers["accept"], "application/json", "correct Accept header")
311297
self.assertRegex(
@@ -319,12 +305,9 @@ def test_request(self):
319305
"correct auth",
320306
)
321307

322-
@httprettified
323308
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),
309+
self.httpserver.expect_request("/geoip/v2.1/city/1.2.3.4").respond_with_json(
310+
self.country,
328311
status=200,
329312
content_type=self._content_type("city"),
330313
)
@@ -335,14 +318,13 @@ def test_city_ok(self):
335318
)
336319
self.assertTrue(city.traits.is_anycast)
337320

338-
@httprettified
339321
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),
322+
self.httpserver.expect_request(
323+
"/geoip/v2.1/insights/" + "1.2.3.4"
324+
).respond_with_json(
325+
self.insights,
344326
status=200,
345-
content_type=self._content_type("country"),
327+
content_type=self._content_type("insights"),
346328
)
347329
insights = self.run_client(self.client.insights("1.2.3.4"))
348330
self.assertEqual(
@@ -374,6 +356,7 @@ class TestClient(TestBaseClient):
374356
def setUp(self):
375357
self.client_class = Client
376358
self.client = Client(42, "abcdef123456")
359+
self.client._base_uri = self.httpserver.url_for("/") + "geoip/v2.1"
377360

378361
def run_client(self, v):
379362
return v
@@ -384,6 +367,7 @@ def setUp(self):
384367
self._loop = asyncio.new_event_loop()
385368
self.client_class = AsyncClient
386369
self.client = AsyncClient(42, "abcdef123456")
370+
self.client._base_uri = self.httpserver.url_for("/") + "geoip/v2.1"
387371

388372
def tearDown(self):
389373
self._loop.run_until_complete(self.client.close())

0 commit comments

Comments
 (0)