|
8 | 8 | import sys
|
9 | 9 | from typing import cast, Dict
|
10 | 10 | import unittest
|
| 11 | +from pytest_httpserver import HeaderValueMatcher |
11 | 12 | import pytest_httpserver
|
12 | 13 | import pytest
|
13 | 14 |
|
@@ -270,39 +271,20 @@ def test_unknown_error(self):
|
270 | 271 | self.run_client(self.client.country(ip))
|
271 | 272 |
|
272 | 273 | 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/"),}) |
281 | 278 | 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"), |
305 | 286 | )
|
| 287 | + self.run_client(self.client.country("1.2.3.4")) |
306 | 288 |
|
307 | 289 | def test_city_ok(self):
|
308 | 290 | self.httpserver.expect_request("/geoip/v2.1/city/1.2.3.4").respond_with_json(
|
|
0 commit comments