Merge pull request #1367 from Kriechi/fix-1366

fix-1366
This commit is contained in:
Aldo Cortesi 2016-07-17 15:06:58 +12:00 committed by GitHub
commit fb45d59c02
2 changed files with 24 additions and 7 deletions

View File

@ -244,7 +244,7 @@ def _read_request_line(rfile):
raise exceptions.HttpReadDisconnect("Client disconnected") raise exceptions.HttpReadDisconnect("Client disconnected")
try: try:
method, path, http_version = line.split(b" ") method, path, http_version = line.split()
if path == b"*" or path.startswith(b"/"): if path == b"*" or path.startswith(b"/"):
form = "relative" form = "relative"
@ -291,8 +291,7 @@ def _read_response_line(rfile):
raise exceptions.HttpReadDisconnect("Server disconnected") raise exceptions.HttpReadDisconnect("Server disconnected")
try: try:
parts = line.split(None, 2)
parts = line.split(b" ", 2)
if len(parts) == 2: # handle missing message gracefully if len(parts) == 2: # handle missing message gracefully
parts.append(b"") parts.append(b"")

View File

@ -1,6 +1,9 @@
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
from io import BytesIO from io import BytesIO
from mock import Mock from mock import Mock
import pytest
from netlib.exceptions import HttpException, HttpSyntaxException, HttpReadDisconnect, TcpDisconnect from netlib.exceptions import HttpException, HttpSyntaxException, HttpReadDisconnect, TcpDisconnect
from netlib.http import Headers from netlib.http import Headers
from netlib.http.http1.read import ( from netlib.http.http1.read import (
@ -23,11 +26,18 @@ def test_get_header_tokens():
assert get_header_tokens(headers, "foo") == ["bar", "voing", "oink"] assert get_header_tokens(headers, "foo") == ["bar", "voing", "oink"]
def test_read_request(): @pytest.mark.parametrize("input", [
rfile = BytesIO(b"GET / HTTP/1.1\r\n\r\nskip") b"GET / HTTP/1.1\r\n\r\nskip",
b"GET / HTTP/1.1\r\n\r\nskip",
b"GET / HTTP/1.1\r\n\r\nskip",
b"GET / HTTP/1.1 \r\n\r\nskip",
])
def test_read_request(input):
rfile = BytesIO(input)
r = read_request(rfile) r = read_request(rfile)
assert r.method == "GET" assert r.method == "GET"
assert r.content == b"" assert r.content == b""
assert r.http_version == "HTTP/1.1"
assert r.timestamp_end assert r.timestamp_end
assert rfile.read() == b"skip" assert rfile.read() == b"skip"
@ -50,11 +60,19 @@ def test_read_request_head():
assert rfile.read() == b"skip" assert rfile.read() == b"skip"
def test_read_response(): @pytest.mark.parametrize("input", [
b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody",
b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody",
b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody",
b"HTTP/1.1 418 I'm a teapot \r\n\r\nbody",
])
def test_read_response(input):
req = treq() req = treq()
rfile = BytesIO(b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody") rfile = BytesIO(input)
r = read_response(rfile, req) r = read_response(rfile, req)
assert r.http_version == "HTTP/1.1"
assert r.status_code == 418 assert r.status_code == 418
assert r.reason == "I'm a teapot"
assert r.content == b"body" assert r.content == b"body"
assert r.timestamp_end assert r.timestamp_end