Skip to content

Commit

Permalink
Remove custom handler and use header matching
Browse files Browse the repository at this point in the history
  • Loading branch information
shadromani committed Jul 8, 2024
1 parent fb95083 commit 2d92b56
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions tests/webservice_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
from typing import cast, Dict
import unittest
from pytest_httpserver import HeaderValueMatcher
import pytest_httpserver
import pytest

Expand Down Expand Up @@ -270,39 +271,20 @@ def test_unknown_error(self):
self.run_client(self.client.country(ip))

def test_request(self):

request = None

def custom_handler(r):
nonlocal request
request = r
return ""

matcher = HeaderValueMatcher({
"Accept": "application/json",
"Authorization":"Basic NDI6YWJjZGVmMTIzNDU2",
"User-Agent": lambda x: x.startswith("GeoIP2-Python-Client/"),})
self.httpserver.expect_request(
"/geoip/v2.1/country/1.2.3.4"
).respond_with_handler(custom_handler)
try:
self.run_client(self.client.country("1.2.3.4"))
except Exception as e:
# just to avoid the exception
pass

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

headers = {k.lower(): v for k, v in request.headers.items()}
self.assertEqual(headers["accept"], "application/json", "correct Accept header")
self.assertRegex(
headers["user-agent"],
"^GeoIP2-Python-Client/",
"Correct User-Agent",
)
self.assertEqual(
headers["authorization"],
"Basic NDI6YWJjZGVmMTIzNDU2",
"correct auth",
"/geoip/v2.1/country/1.2.3.4",
header_value_matcher=matcher,

).respond_with_json(
self.country,
status=200,
content_type=self._content_type("country"),
)
self.run_client(self.client.country("1.2.3.4"))

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

0 comments on commit 2d92b56

Please sign in to comment.