Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FastHttpUser use requests encoding detection #2416

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from geventhttpclient.response import HTTPConnectionClosed, HTTPSocketPoolResponse
from geventhttpclient.header import Headers

# borrow requests's content-type header parsing
from requests.utils import get_encoding_from_headers

from locust.user import User
from locust.exception import LocustError, CatchResponseError, ResponseError
from locust.env import Environment
Expand Down Expand Up @@ -460,7 +463,7 @@ def text(self) -> Optional[str]:
if self.headers is None:
self.encoding = "utf-8"
else:
self.encoding = self.headers.get("content-type", "").partition("charset=")[2] or "utf-8"
self.encoding = get_encoding_from_headers(self.headers)
return str(self.content, self.encoding, errors="replace")

@property
Expand Down
10 changes: 10 additions & 0 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ class MyUser(FastHttpUser):
locust = MyUser(self.environment)
self.assertEqual(200, locust.client.head("/request_method").status_code)

def test_complex_content_type(self):
class MyUser(FastHttpUser):
host = "http://127.0.0.1:%i" % self.port

locust = MyUser(self.environment)

self.assertEqual("stuff", locust.client.get("/content_type_missing_charset").text)
self.assertEqual("stuff", locust.client.get("/content_type_regular").text)
self.assertEqual("stuff", locust.client.get("/content_type_with_extra_stuff").text)

def test_log_request_name_argument(self):
self.response = ""

Expand Down
21 changes: 21 additions & 0 deletions locust/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ def rest():
return request.json


@app.route("/content_type_missing_charset")
def content_type_missing_charset():
resp = make_response("stuff")
resp.headers["Content-Type"] = "Content-Type: application/json;"
return resp


@app.route("/content_type_regular")
def content_type_regular():
resp = make_response("stuff")
resp.headers["Content-Type"] = "Content-Type: application/json; charset=utf-8;"
return resp


@app.route("/content_type_with_extra_stuff")
def content_type_with_extra_stuff():
resp = make_response("stuff")
resp.headers["Content-Type"] = "Content-Type: application/json; charset=utf-8; api-version=3.0"
return resp


class LocustTestCase(unittest.TestCase):
"""
Test case class that restores locust.events.EventHook listeners on tearDown, so that it is
Expand Down
Loading