locustio / locust

Write scalable load tests in plain Python 🚗💨
MIT License
24.6k stars 2.96k forks source link

2.18 - fasthttp error for no content response #2450

Closed scottARob closed 10 months ago

scottARob commented 10 months ago

Prerequisites

Description

Sending a request that generates a no content response is throwing a error when using fasthttp rest. This isn't happening under 2.17

Command line

Locust UI

Locustfile contents

File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__
worker-1  |     return next(self.gen)
worker-1  |            ^^^^^^^^^^^^^^
worker-1  |   File "/opt/venv/lib/python3.11/site-packages/locust/contrib/fasthttp.py", line 382, in rest
worker-1  |     if resp.text is None:
worker-1  |        ^^^^^^^^^
worker-1  |   File "/opt/venv/lib/python3.11/site-packages/locust/contrib/fasthttp.py", line 467, in text
worker-1  |     return str(self.content, self.encoding, errors="replace")
worker-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
worker-1  | TypeError: str() argument 'encoding' must be str, not None

Python version

3.11

Locust version

2.18

Operating system

linux

scottARob commented 10 months ago

I think its due to this change https://github.com/locustio/locust/pull/2416/files

it is possible that get_encoding_from_headers function will return None which will throw the above error. Need to return as a empty string as per 2.17

cyberw commented 10 months ago

Oh... yea sounds about right. Or maybe we should just not try to decode at all when there is no content? I dont have time to look at this right now...

scottARob commented 10 months ago

Could just throw in a condition as a quick fix so are able to still utilise the requests lib

self.encoding = get_encoding_from_headers(self.headers)
if self.encoding is None:
     self.encoding = ""
self.encoding = get_encoding_from_headers(self.headers) or ""
scottARob commented 10 months ago

https://github.com/locustio/locust/pull/2451/files