From 63bfdc7a6d7bab2c8c38b198d6e27011a12adfa1 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Mon, 18 Mar 2024 21:46:10 +0100 Subject: [PATCH] Dont crash FastHttpUser if a request is made with stream=True and the connection fails. --- locust/contrib/fasthttp.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/locust/contrib/fasthttp.py b/locust/contrib/fasthttp.py index 66980c9247..fd59c9c810 100644 --- a/locust/contrib/fasthttp.py +++ b/locust/contrib/fasthttp.py @@ -230,16 +230,18 @@ def request( if not allow_redirects: self.client.redirect_resonse_codes = old_redirect_response_codes + request_meta["response_length"] = 0 # default value, if length cannot be determined + # get the length of the content, but if the argument stream is set to True, we take # the size from the content-length header, in order to not trigger fetching of the body if stream: - request_meta["response_length"] = int(response.headers.get("response_length") or 0) + if response.headers and "response_length" in response.headers: + request_meta["response_length"] = int(response.headers["response_length"]) else: try: - request_meta["response_length"] = len(response.content or "") + request_meta["response_length"] = len(response.content) if response.content else 0 except HTTPParseError as e: request_meta["response_time"] = (time.perf_counter() - start_perf_counter) * 1000 - request_meta["response_length"] = 0 request_meta["exception"] = e self.environment.events.request.fire(**request_meta) return response