2015-09-16 16:45:22 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from netlib.exceptions import HttpSyntaxException
|
2015-07-27 09:46:49 +00:00
|
|
|
from netlib.http import http1
|
2015-09-28 08:59:10 +00:00
|
|
|
from netlib.tcp import TCPClient
|
2015-09-16 16:45:22 +00:00
|
|
|
from netlib.tutils import treq, raises
|
2016-02-02 12:25:31 +00:00
|
|
|
from . import tutils, tservers
|
2014-02-07 02:56:57 +00:00
|
|
|
|
2015-07-27 09:46:49 +00:00
|
|
|
|
2014-02-07 02:56:57 +00:00
|
|
|
class TestHTTPResponse:
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2014-02-07 02:56:57 +00:00
|
|
|
def test_read_from_stringio(self):
|
2015-09-16 16:45:22 +00:00
|
|
|
s = (
|
|
|
|
b"HTTP/1.1 200 OK\r\n"
|
|
|
|
b"Content-Length: 7\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
b"content\r\n"
|
|
|
|
b"HTTP/1.1 204 OK\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
)
|
|
|
|
rfile = BytesIO(s)
|
|
|
|
r = http1.read_response(rfile, treq())
|
2015-07-27 09:46:49 +00:00
|
|
|
assert r.status_code == 200
|
2015-09-16 16:45:22 +00:00
|
|
|
assert r.content == b"content"
|
|
|
|
assert http1.read_response(rfile, treq()).status_code == 204
|
2014-02-07 02:56:57 +00:00
|
|
|
|
2015-09-16 16:45:22 +00:00
|
|
|
rfile = BytesIO(s)
|
2015-04-14 21:14:20 +00:00
|
|
|
# HEAD must not have content by spec. We should leave it on the pipe.
|
2015-09-16 16:45:22 +00:00
|
|
|
r = http1.read_response(rfile, treq(method=b"HEAD"))
|
2015-07-27 09:46:49 +00:00
|
|
|
assert r.status_code == 200
|
2015-09-16 16:45:22 +00:00
|
|
|
assert r.content == b""
|
|
|
|
|
|
|
|
with raises(HttpSyntaxException):
|
|
|
|
http1.read_response(rfile, treq())
|
2014-02-07 06:08:59 +00:00
|
|
|
|
2015-04-14 04:23:51 +00:00
|
|
|
|
2014-09-04 12:46:25 +00:00
|
|
|
class TestHTTPFlow(object):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2014-09-04 12:46:25 +00:00
|
|
|
def test_repr(self):
|
|
|
|
f = tutils.tflow(resp=True, err=True)
|
|
|
|
assert repr(f)
|
|
|
|
|
2014-02-07 06:08:59 +00:00
|
|
|
|
2014-02-07 17:14:15 +00:00
|
|
|
class TestInvalidRequests(tservers.HTTPProxTest):
|
|
|
|
ssl = True
|
2015-04-14 21:14:20 +00:00
|
|
|
|
2014-02-07 17:14:15 +00:00
|
|
|
def test_double_connect(self):
|
|
|
|
p = self.pathoc()
|
|
|
|
r = p.request("connect:'%s:%s'" % ("127.0.0.1", self.server2.port))
|
2014-03-10 01:32:27 +00:00
|
|
|
assert r.status_code == 400
|
2015-09-26 15:41:14 +00:00
|
|
|
assert "Invalid HTTP request form" in r.content
|
2014-02-07 17:14:15 +00:00
|
|
|
|
2014-03-08 14:47:09 +00:00
|
|
|
def test_relative_request(self):
|
2014-02-07 17:14:15 +00:00
|
|
|
p = self.pathoc_raw()
|
|
|
|
p.connect()
|
|
|
|
r = p.request("get:/p/200")
|
|
|
|
assert r.status_code == 400
|
2015-09-26 15:41:14 +00:00
|
|
|
assert "Invalid HTTP request form" in r.content
|
2015-09-28 08:59:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestExpectHeader(tservers.HTTPProxTest):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2015-09-28 08:59:10 +00:00
|
|
|
def test_simple(self):
|
|
|
|
client = TCPClient(("127.0.0.1", self.proxy.port))
|
|
|
|
client.connect()
|
|
|
|
|
|
|
|
# call pathod server, wait a second to complete the request
|
|
|
|
client.wfile.write(
|
|
|
|
b"POST http://localhost:%d/p/200 HTTP/1.1\r\n"
|
|
|
|
b"Expect: 100-continue\r\n"
|
|
|
|
b"Content-Length: 16\r\n"
|
|
|
|
b"\r\n" % self.server.port
|
|
|
|
)
|
|
|
|
client.wfile.flush()
|
|
|
|
|
|
|
|
assert client.rfile.readline() == "HTTP/1.1 100 Continue\r\n"
|
|
|
|
assert client.rfile.readline() == "\r\n"
|
|
|
|
|
|
|
|
client.wfile.write(b"0123456789abcdef\r\n")
|
|
|
|
client.wfile.flush()
|
|
|
|
|
|
|
|
resp = http1.read_response(client.rfile, treq())
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
2015-09-28 11:54:53 +00:00
|
|
|
client.finish()
|
2016-01-02 14:12:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestHeadContentLength(tservers.HTTPProxTest):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2016-01-02 14:12:36 +00:00
|
|
|
def test_head_content_length(self):
|
|
|
|
p = self.pathoc()
|
|
|
|
resp = p.request("""head:'%s/p/200:h"Content-Length"="42"'""" % self.server.urlbase)
|
2016-01-27 09:12:18 +00:00
|
|
|
assert resp.headers["Content-Length"] == "42"
|