Skip to content

Commit

Permalink
test keep-alive chunked requests
Browse files Browse the repository at this point in the history
see #598
  • Loading branch information
t-8ch committed Apr 27, 2015
1 parent 548b79a commit c92fe45
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
21 changes: 17 additions & 4 deletions dummyserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

class Response(object):
def __init__(self, body='', status='200 OK', headers=None):
if not isinstance(body, bytes):
body = body.encode('utf8')

self.body = body
self.status = status
self.headers = headers or [("Content-type", "text/plain")]
Expand All @@ -34,7 +31,20 @@ def __call__(self, request_handler):
for header,value in self.headers:
request_handler.add_header(header,value)

request_handler.write(self.body)
# chunked
if isinstance(self.body, list):
for item in self.body:
if not isinstance(item, bytes):
item = item.encode('utf8')
request_handler.write(item)
request_handler.flush()
else:
body = self.body
if not isinstance(body, bytes):
body = body.encode('utf8')

request_handler.write(body)


RETRY_TEST_NAMES = collections.defaultdict(int)

Expand Down Expand Up @@ -208,6 +218,9 @@ def successful_retry(self, request):
else:
return Response("need to keep retrying!", status="418 I'm A Teapot")

def chunked(self, request):
return Response(['123'] * 4)

def shutdown(self, request):
sys.exit()

Expand Down
19 changes: 19 additions & 0 deletions test/with_dummyserver/test_connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,25 @@ def test_source_address_error(self):
self.assertRaises(ProtocolError,
pool.request, 'GET', '/source_address')

def test_stream_keepalive(self):
x = 2

for _ in range(x):
response = self.pool.request(
'GET',
'/chunked',
headers={
'Connection': 'keep-alive',
},
preload_content=False,
retries=0,
)
for chunk in response.stream():
self.assertEqual(chunk, b'123')

self.assertEqual(self.pool.num_connections, 1)
self.assertEqual(self.pool.num_requests, x)


class TestRetry(HTTPDummyServerTestCase):
def setUp(self):
Expand Down

0 comments on commit c92fe45

Please sign in to comment.