400: Bad Requests unable to be passed into catch_response
See original GitHub issueDescription of issue / feature request
when using client.get() with catch_response=true, the HTTPError is thrown, rather than being able to pass the request on to any subsequent code in the task.
For example:
from locust import HttpLocust, TaskSet, task
class MyTaskSet(TaskSet):
@task(1)
def oneYearTerm(self):
with self.client.get("/myendpoint?param=someBadRequest", catch_response=True) as response:
if response.status_code != 400:
response.failure("Didn't detect bad response, got: " + str(response.status_code))
class MyLocust(HttpLocust):
task_set = MyTaskSet
min_wait = 0
max_wait = 250
host="https://my.url.co.uk/"
In the above, I would expect any exceptions to be caught such that I can test unhappy paths, such as where I have made an API respond with a 400, and perform any assertions on that bad response later on.
I currently do not have a better way in place outside of an ugly try/except, but even then I am not familiar with the locust codebase, and the documentation does not (As far as I could find) highlight where exceptions lie, so the actual HTTPException that was thrown I struggled to catch.
Expected behavior
Any exceptions from bad requests are caught when using catch_response=true, allowing me to operate on them in the subsequent code
Actual behavior
HTTPException is thrown: ‘400 Client Error: Bad Request for url’. This is returned by the API I am load testing, and until my tests say otherwise should be assumed to be a valid response, and be operable on
Environment settings (for bug reports)
- OS: both MacOS and Alpine Containers
- Python version: 3.7 locally on macOS, and 2.7.15 within containers
- Locust version: Locust 0.9.0
Steps to reproduce (for bug reports)
Run a task with self.client.get(), with catch_response=true, against an endpoint or with data that provides a 400 Bad request. This will throw a HTTPException instead of allowing the response to be used as shown in the code example above.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
The problem is that if you don’t explicitly mark the response as a success or failure (by calling
success()
orfailure()
) it’ll use the default way of determining if the request was a success or a failure.So you need to explicitly mark the response as a success to prevent them from being reported as a failure.
If you change the relevant parts of your example above to the following, it works:
I think I must have got something wrong in my own test, as I can’t reproduce it with the one I commented on here either actually. Thanks, seems like there’s nothing wrong.