Return a named tuple from read_response

This commit is contained in:
Aldo Cortesi 2015-04-21 11:11:16 +12:00
parent 2c660d7633
commit dd7ea896f2
2 changed files with 20 additions and 6 deletions

View File

@ -314,6 +314,18 @@ def parse_response_line(line):
return (proto, code, msg)
Response = collections.namedtuple(
"Response",
[
"httpversion",
"code",
"msg",
"headers",
"content"
]
)
def read_response(rfile, request_method, body_size_limit, include_body=True):
"""
Return an (httpversion, code, msg, headers, content) tuple.
@ -352,7 +364,7 @@ def read_response(rfile, request_method, body_size_limit, include_body=True):
# if include_body==False then a None content means the body should be
# read separately
content = None
return httpversion, code, msg, headers, content
return Response(httpversion, code, msg, headers, content)
def read_http_body(*args, **kwargs):
@ -531,8 +543,8 @@ def read_request(rfile, include_body=True, body_size_limit=None, wfile=None):
if headers is None:
raise HttpError(400, "Invalid headers")
expect_header = headers.get_first("expect")
if expect_header and expect_header.lower() == "100-continue" and httpversion >= (1, 1):
expect_header = headers.get_first("expect", "").lower()
if expect_header == "100-continue" and httpversion >= (1, 1):
wfile.write(
'HTTP/1.1 100 Continue\r\n'
'\r\n'

View File

@ -254,15 +254,17 @@ class TestReadResponseNoContentLength(test.ServerTestBase):
def test_no_content_length(self):
c = tcp.TCPClient(("127.0.0.1", self.port))
c.connect()
httpversion, code, msg, headers, content = http.read_response(c.rfile, "GET", None)
assert content == "bar\r\n\r\n"
resp = http.read_response(c.rfile, "GET", None)
assert resp.content == "bar\r\n\r\n"
def test_read_response():
def tst(data, method, limit, include_body=True):
data = textwrap.dedent(data)
r = cStringIO.StringIO(data)
return http.read_response(r, method, limit, include_body = include_body)
return http.read_response(
r, method, limit, include_body = include_body
)
tutils.raises("server disconnect", tst, "", "GET", None)
tutils.raises("invalid server response", tst, "foo", "GET", None)