Skip to content

Commit

Permalink
Throw an exception if someone tries to call response.failure()/succes…
Browse files Browse the repository at this point in the history
…s() without using a with block (helps with some misunderstandings, e.g. #1953)
  • Loading branch information
cyberw committed Dec 8, 2021
1 parent c79533d commit 5c0b397
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 12 additions & 3 deletions locust/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from urllib.parse import urlparse, urlunparse

from .exception import CatchResponseError, ResponseError
from .exception import CatchResponseError, LocustError, ResponseError

absolute_http_url_regexp = re.compile(r"^https?://", re.I)

Expand Down Expand Up @@ -215,15 +215,16 @@ class ResponseContextManager(LocustResponse):
:py:meth:`failure <locust.clients.ResponseContextManager.failure>`.
"""

_manual_result = None

def __init__(self, response, request_event, request_meta):
self._entered = False
self._manual_result = None
# copy data from response to this object
self.__dict__ = response.__dict__
self._request_event = request_event
self.request_meta = request_meta

def __enter__(self):
self._entered = True
return self

def __exit__(self, exc, value, traceback):
Expand Down Expand Up @@ -267,6 +268,10 @@ def success(self):
if response.status_code == 404:
response.success()
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.success()"
)
self._manual_result = True

def failure(self, exc):
Expand All @@ -282,6 +287,10 @@ def failure(self, exc):
if response.content == b"":
response.failure("No data")
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.failure(...)"
)
if not isinstance(exc, Exception):
exc = CatchResponseError(exc)
self._manual_result = exc
17 changes: 13 additions & 4 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def insecure_ssl_context_factory():


class FastHttpSession:
auth_header = None

def __init__(self, environment: Environment, base_url: str, user: "FastHttpUser", insecure=True, **kwargs):
self.environment = environment
self.base_url = base_url
Expand Down Expand Up @@ -93,6 +91,8 @@ def __init__(self, environment: Environment, base_url: str, user: "FastHttpUser"
)
# store authentication header (we construct this by using _basic_auth_str() function from requests.auth)
self.auth_header = _construct_basic_auth_str(parsed_url.username, parsed_url.password)
else:
self.auth_header = None

def _build_url(self, path):
"""prepend url with hostname unless it's already an absolute URL"""
Expand Down Expand Up @@ -412,9 +412,9 @@ class ResponseContextManager(FastResponse):
and :py:meth:`failure <locust.contrib.fasthttp.ResponseContextManager.failure>`.
"""

_manual_result = None

def __init__(self, response, environment, request_meta):
self._entered = False
self._manual_result = None
# copy data from response to this object
self.__dict__ = response.__dict__
self._cached_content = response.content
Expand All @@ -423,6 +423,7 @@ def __init__(self, response, environment, request_meta):
self.request_meta = request_meta

def __enter__(self):
self._entered = True
return self

def __exit__(self, exc, value, traceback):
Expand Down Expand Up @@ -465,6 +466,10 @@ def success(self):
if response.status_code == 404:
response.success()
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.success()"
)
self._manual_result = True

def failure(self, exc):
Expand All @@ -480,6 +485,10 @@ def failure(self, exc):
if response.content == "":
response.failure("No data")
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.failure(...)"
)
if not isinstance(exc, Exception):
exc = CatchResponseError(exc)
self._manual_result = exc

0 comments on commit 5c0b397

Please sign in to comment.