Skip to content

Commit 2d92b56

Browse files
committed
Remove custom handler and use header matching
1 parent fb95083 commit 2d92b56

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

tests/webservice_test.py

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
from typing import cast, Dict
1010
import unittest
11+
from pytest_httpserver import HeaderValueMatcher
1112
import pytest_httpserver
1213
import pytest
1314

@@ -270,39 +271,20 @@ def test_unknown_error(self):
270271
self.run_client(self.client.country(ip))
271272

272273
def test_request(self):
273-
274-
request = None
275-
276-
def custom_handler(r):
277-
nonlocal request
278-
request = r
279-
return ""
280-
274+
matcher = HeaderValueMatcher({
275+
"Accept": "application/json",
276+
"Authorization":"Basic NDI6YWJjZGVmMTIzNDU2",
277+
"User-Agent": lambda x: x.startswith("GeoIP2-Python-Client/"),})
281278
self.httpserver.expect_request(
282-
"/geoip/v2.1/country/1.2.3.4"
283-
).respond_with_handler(custom_handler)
284-
try:
285-
self.run_client(self.client.country("1.2.3.4"))
286-
except Exception as e:
287-
# just to avoid the exception
288-
pass
289-
290-
self.assertEqual(
291-
request.path, "/geoip/v2.1/country/1.2.3.4", "correct URI is used"
292-
)
293-
294-
headers = {k.lower(): v for k, v in request.headers.items()}
295-
self.assertEqual(headers["accept"], "application/json", "correct Accept header")
296-
self.assertRegex(
297-
headers["user-agent"],
298-
"^GeoIP2-Python-Client/",
299-
"Correct User-Agent",
300-
)
301-
self.assertEqual(
302-
headers["authorization"],
303-
"Basic NDI6YWJjZGVmMTIzNDU2",
304-
"correct auth",
279+
"/geoip/v2.1/country/1.2.3.4",
280+
header_value_matcher=matcher,
281+
282+
).respond_with_json(
283+
self.country,
284+
status=200,
285+
content_type=self._content_type("country"),
305286
)
287+
self.run_client(self.client.country("1.2.3.4"))
306288

307289
def test_city_ok(self):
308290
self.httpserver.expect_request("/geoip/v2.1/city/1.2.3.4").respond_with_json(

0 commit comments

Comments
 (0)