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

Fix bytes/text confusion with response objects #762

Merged
merged 1 commit into from
Mar 26, 2018
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
4 changes: 2 additions & 2 deletions docs/writing-a-locustfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class::

response = self.client.get("/about")
print("Response status code:", response.status_code)
print("Response content:", response.content)
print("Response content:", response.text)

And here's an example making a POST request::

Expand All @@ -369,7 +369,7 @@ One can mark requests as failed, even when the response code is OK, by using the
*catch_response* argument and a with statement::

with client.get("/", catch_response=True) as response:
if response.content != "Success":
if response.content != b"Success":
response.failure("Got wrong response")

Just as one can mark requests with OK response codes as failures, one can also use **catch_response**
Expand Down
4 changes: 2 additions & 2 deletions locust/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def request(self, method, url, name=None, catch_response=False, **kwargs):
if kwargs.get("stream", False):
request_meta["content_size"] = int(response.headers.get("content-length") or 0)
else:
request_meta["content_size"] = len(response.content or "")
request_meta["content_size"] = len(response.content or b"")

if catch_response:
response.locust_request_meta = request_meta
Expand Down Expand Up @@ -233,7 +233,7 @@ def failure(self, exc):
Example::

with self.client.get("/", catch_response=True) as response:
if response.content == "":
if response.content == b"":
response.failure("No data")
"""
if isinstance(exc, six.string_types):
Expand Down
6 changes: 3 additions & 3 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_distribution_stats_csv(self):
stats.global_stats.log_request("GET", "/test2", 1200, 5612)
response = requests.get("http://127.0.0.1:%i/stats/distribution/csv" % self.web_port)
self.assertEqual(200, response.status_code)
rows = str(response.content.decode("utf-8")).split("\n")
rows = response.text.split("\n")
# check that /test2 is present in stats
row = rows[len(rows)-2].split(",")
self.assertEqual('"GET /test2"', row[0])
Expand All @@ -100,7 +100,7 @@ def test_request_stats_with_errors(self):
stats.global_stats.log_error("GET", "/", Exception("Error1337"))
response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port)
self.assertEqual(200, response.status_code)
self.assertIn("Error1337", str(response.content))
self.assertIn("Error1337", response.text)

def test_exceptions(self):
try:
Expand All @@ -112,7 +112,7 @@ def test_exceptions(self):

response = requests.get("http://127.0.0.1:%i/exceptions" % self.web_port)
self.assertEqual(200, response.status_code)
self.assertIn("A cool test exception", str(response.content))
self.assertIn("A cool test exception", response.text)

response = requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port)
self.assertEqual(200, response.status_code)
Expand Down