question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

400: Bad Requests unable to be passed into catch_response

See original GitHub issue

Description 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:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
heymancommented, Feb 4, 2020

The problem is that if you don’t explicitly mark the response as a success or failure (by calling success() or failure()) 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:

with self.client.get("/401", catch_response=True) as response:
    if response.status_code != 401:
        response.failure("Got unexpected response code: " + str(response.status_code) + " Error: " + str(response.text))
    else:
        print("401 Test passed")
        response.success()
1reaction
DanielRussellktcommented, Feb 5, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The 400 Bad Request error indicates that the server cannot or process the request due to a client error. Read about the common...
Read more >
How to Fix a 400 Bad Request Error: 8 Easy Methods
Stuck with a 400 bad request error? Read this article to find out 8 simple methods to solve the problem and its possible...
Read more >
400 Bad Request - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request ......
Read more >
How To Fix A 400 Bad Request Error [Causes And Solutions]
A 400 bad request error indicates that the client's request was inaccurate or corrupted, and the server was unable to interpret it.
Read more >
Exceptions - Manual - PHP
In prior versions it was a statement and was required to be on its own line. catch ¶. A catch block defines how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found