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

Add iter_lines Method to FastHttpSession Class #3024

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
beb1f5c
Update fasthttp.py
MasterKey-Pro Jan 6, 2025
1746ae4
Update fasthttp.py
MasterKey-Pro Jan 6, 2025
e2acf1d
Update fasthttp.py
MasterKey-Pro Jan 8, 2025
f2c1ced
Update fasthttp.py
MasterKey-Pro Jan 8, 2025
9d9c2e1
Format fasthttp.py to conform with coding standards
MasterKey-Pro Jan 8, 2025
eff35bf
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
b2d10f5
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
78753c5
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
d470be5
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
c736922
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
e1a29c4
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
b0f6c53
Merge branch 'locustio:master' into master
MasterKey-Pro Jan 9, 2025
da48849
Add test case for iter_lines method
MasterKey-Pro Jan 9, 2025
26dfd1c
Merge branch 'master' of https://github.com/MasterKey-Pro/locust
MasterKey-Pro Jan 9, 2025
9799eca
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
9d7de6b
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
96662d3
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
323de91
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
7f6f52a
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
49f3ee5
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
563dab3
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
21dd327
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
d288add
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
8d48e57
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
8a104da
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
5e57f9b
Add test case for iter_lines method
MasterKey-Pro Jan 10, 2025
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
11 changes: 11 additions & 0 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ def get(self, url: str, **kwargs: Unpack[RESTKwargs]) -> ResponseContextManager
"""Sends a GET request"""
return self.request("GET", url, **kwargs)

def iter_lines(self, url: str, **kwargs) -> Generator[str, None, None]:
"""Sends a iter_lines request"""
response = self.request("GET", url, stream=True, **kwargs)
response.raise_for_status()
buffer = ""
for chunk in response.iter_content(chunk_size=1024, decode_content=True):
buffer += chunk.decode("utf-8")
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
yield line

def head(self, url: str, **kwargs: Unpack[RESTKwargs]) -> ResponseContextManager | FastResponse:
"""Sends a HEAD request"""
return self.request("HEAD", url, **kwargs)
Expand Down
33 changes: 33 additions & 0 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import socket
import time
from tempfile import NamedTemporaryFile
from unittest.mock import MagicMock

import gevent
from geventhttpclient.client import HTTPClientPool
Expand Down Expand Up @@ -107,6 +108,38 @@ def test_streaming_response_catch_response(self):
self.assertGreaterEqual(stats.avg_response_time, 0)
self.assertLess(stats.avg_response_time, 250)

def test_iter_lines(self):
session = self.get_client()

url = "/streaming/10"

response_mock = MagicMock()
response_mock.iter_content = MagicMock(
return_value=iter(
[
b"<span>0</span>\n",
b"<span>1</span>\n",
b"<span>2</span>\n",
b"<span>3</span>\n",
b"<span>4</span>\n",
b"<span>5</span>\n",
b"<span>6</span>\n",
b"<span>7</span>\n",
b"<span>8</span>\n",
b"<span>9</span>\n",
]
)
)
response_mock.raise_for_status = MagicMock()

session.request = MagicMock(return_value=response_mock)

lines = list(session.iter_lines(url))

expected_lines = [f"<span>{i}</span>" for i in range(10)]

self.assertEqual(lines, expected_lines)

def test_slow_redirect(self):
s = self.get_client()
url = "/redirect?url=/redirect&delay=0.5"
Expand Down
Loading