-
Notifications
You must be signed in to change notification settings - Fork 30
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
How should I assert no requests made to httpserver #35
Comments
You can check the def test_norequests(httpserver):
# code which may have made requests or not
assert len(httpserver.log) > 0, "At least one request should be made" or: def test_norequests(httpserver):
# code which may have made requests or not
assert httpserver.log, "At least one request should be made" |
Is it good idea to have some method like |
If this function is useful for you, you can implement it in conftest.py or at other places, so it will be available. Personally, I think checking that there was at least one http request made is error prone, as it does not say anything about what kind of http request was made (provided you have more than one handlers), and the original idea of this library was to check the client code by asserting to some value which is known only to the server. Eg: def test_expected_request_json(httpserver: HTTPServer):
httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
assert requests.get(httpserver.url_for("/foobar")).json() == {'foo': 'bar'} Here, the client (which is the requests library in this case) should return the |
I can understand the case for at least one request made. I'm talking about a case where I want to assert that given set of conditions I want |
I see. def test_expected_request_json(httpserver):
### httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
requests.get(httpserver.url_for("/foobar")) # this will get http status 500 describing the issue
httpserver.check_assertions() |
Thanks @csernazs |
Should |
Yes, that's a good question. Originally it was implemented to ensure that the client is doing no more than a single request to that endpoint. It is similar to the ordered handlers except the order does not matter for the oneshot. If I was implementing this from zero, probably consider any leftover oneshot (and ordered) handlers as an error, but now it is a change which would break the tests already using this. You can also check for the leftover handlers: If it would make your life easier a new method can be added to httpserver to check these. |
Hi @csernazs, (Thanks for pytest-httpserver 👍) I found this looking for how to check for unused handlers, and
|
hi @GKTheOne , I think this is a very good idea. We just need to decide what to include to the Something like that:
(so it would be the method and the URI) We would not include the json or data as it can be arbitrary length. Same for headers and query string, but actually query string could be possibly included.
Something like this...? We could optionally include the response status or some other data from the response, but I think we should keep this short and brief (response status code is just "200 OK", that is short, the response body is long). What do you think? |
I found that requestmatcher already have a pytest-httpserver/pytest_httpserver/httpserver.py Lines 342 to 353 in e32a568
We could re-use this code, but I think it is way too verbose, especially if you have json or data specified for the matcher. So we can either re-use this or write a new one which is more brief. What do you think? |
I'll point to re-using the RequestMatcher code. I hear you, in that the string representation could be overly long, but I think that's ok. |
Thanks @csernazs. You are awesome 👍 |
No description provided.
The text was updated successfully, but these errors were encountered: