-
Notifications
You must be signed in to change notification settings - Fork 142
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
Remove mocket #173
Conversation
47b008d
to
55f57b3
Compare
55f57b3
to
8a58936
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I had a few comments.
tests/webservice_test.py
Outdated
httpretty.GET, | ||
self.base_uri + "country/1.2.3.4", | ||
body=json.dumps(self.country), | ||
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should specify the method on all of the expect_request
calls as the default is to allow all methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you please elaborate how? The test has different responses depending on the specific ip and we can not return the default on all requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I completely understand the question, but I am referring to the HTTP method. If we don't set it explicitly to GET
, any method will be allowed. To do so, I think you would add something like this to each of them:
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json( | |
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4", method="GET").respond_with_json( |
This would be equivalent to httpretty.GET
in the previous version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I misread your comment.
tests/webservice_test.py
Outdated
self.run_client(self.client.country("1.2.3.4")) | ||
except Exception as e: | ||
# just to avoid the exception | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to return a valid JSON response. I think if you use the header matchers, this will be easy to do with respond_with_json
.
tests/webservice_test.py
Outdated
def custom_handler(r): | ||
nonlocal request | ||
request = r | ||
return "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom handler is a bit confusing. I think we could eliminate it by using headers
or header_value_matcher
to match the expected headers on expect_request
. I don't think we need to test the path again.
0d0c317
to
94c3584
Compare
tests/webservice_test.py
Outdated
matcher = HeaderValueMatcher({ | ||
"Accept": "application/json", | ||
"Authorization":"Basic NDI6YWJjZGVmMTIzNDU2", | ||
"User-Agent": lambda x: x.startswith("GeoIP2-Python-Client/"),}) |
There was a problem hiding this comment.
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".
"Accept": accept_compare, | ||
"Authorization": authorization_compare, | ||
} | ||
), |
There was a problem hiding this comment.
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 works either. If I change the values, the tests don't fail. I was expecting something like this:
def user_agent_compare(actual: str, expected: str) -> bool:
if actual is None:
return False
return actual.startswith(expected)
self.httpserver.expect_request(
"/geoip/v2.1/country/1.2.3.4",
method="GET",
headers={
"Accept": "application/json",
"Authorization": "Basic NDI6YWJjZGVmMTIzNDU2",
"User-Agent": "GeoIP2-Python-Client/"
},
header_value_matcher=HeaderValueMatcher(defaultdict(
lambda: HeaderValueMatcher.default_header_value_matcher,
{"User-Agent": user_agent_compare}
)),
).respond_with_json(
self.country,
status=200,
content_type=self._content_type("country"),
)
Alternatively, you could modify the default matcher as directed in the docs.
I'm curious, what's the rational behind the switch from mocket to http-server? |
We unfortunately had quite a few problem with it breaking, e.g., on new or old Python versions, on new We decided to use |
No description provided.