Skip to content

Commit

Permalink
Merge pull request #2416 from locustio/make-FastHttpUser-use-requests…
Browse files Browse the repository at this point in the history
…-encoding-detection

Make FastHttpUser use requests encoding detection
  • Loading branch information
cyberw authored Oct 7, 2023
2 parents 2cde086 + 5978f02 commit 17d020e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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

0 comments on commit 17d020e

Please sign in to comment.