Skip to content

Commit

Permalink
Give a more descriptive error message if you try to call .success or …
Browse files Browse the repository at this point in the history
….failure on a HttpUser request with missing catch_response=True
  • Loading branch information
cyberw committed Jan 31, 2022
1 parent eb25c00 commit 368a4f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
17 changes: 17 additions & 0 deletions locust/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,20 @@ def failure(self, exc):
if not isinstance(exc, Exception):
exc = CatchResponseError(exc)
self._manual_result = exc


# Monkey patch Response class to give some guidance
def _success(self):
raise LocustError(
"If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"
)


def _failure(self):
raise LocustError(
"If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"
)


Response.success = _success
Response.failure = _failure
4 changes: 2 additions & 2 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ def _content(self):
return super()._content()

def success(self):
raise Exception(
raise LocustError(
"If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"
)

def failure(self):
raise Exception(
raise LocustError(
"If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses"
)

Expand Down
2 changes: 1 addition & 1 deletion locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def on_failure(**kw):
def test_missing_catch_response_true(self):
# incorrect usage, missing catch_response=True
with self.user.client.get("/fail") as resp:
self.assertRaises(Exception, resp.success)
self.assertRaises(LocustError, resp.success)


class TestFastHttpSsl(LocustTestCase):
Expand Down
6 changes: 6 additions & 0 deletions locust/test/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ def test_catch_response_missing_with_block(self):
self.assertRaises(LocustError, r.success)
self.assertRaises(LocustError, r.failure, "")

def test_missing_catch_response_true(self):
s = self.get_client()
# incorrect usage, missing catch_response=True
with s.get("/fail") as resp:
self.assertRaises(LocustError, resp.success)

def test_user_context(self):
class TestUser(HttpUser):
host = f"http://127.0.0.1:{self.port}"
Expand Down

0 comments on commit 368a4f3

Please sign in to comment.