Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mocket #173

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 13 additions & 31 deletions tests/webservice_test.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import sys
from typing import cast, Dict
import unittest
from pytest_httpserver import HeaderValueMatcher
import pytest_httpserver
import pytest

@@ -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/"),})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct usage of HeaderValueMatcher. I think you still set headers, but you would want to define a custom matcher for "User-Agent".

See the docs.

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(