mitmproxy/test/test_protocol_http.py

57 lines
1.6 KiB
Python
Raw Normal View History

2015-09-16 16:45:22 +00:00
from io import BytesIO
from netlib.exceptions import HttpSyntaxException
from netlib.http import http1
2015-09-16 16:45:22 +00:00
from netlib.tutils import treq, raises
2015-04-14 21:14:20 +00:00
import tutils
import tservers
2014-02-07 02:56:57 +00:00
2014-02-07 02:56:57 +00:00
class TestHTTPResponse:
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())
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"))
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):
def test_repr(self):
f = tutils.tflow(resp=True, err=True)
assert repr(f)
2014-02-07 06:08:59 +00:00
class TestInvalidRequests(tservers.HTTPProxTest):
ssl = True
2015-04-14 21:14:20 +00:00
def test_double_connect(self):
p = self.pathoc()
r = p.request("connect:'%s:%s'" % ("127.0.0.1", self.server2.port))
assert r.status_code == 400
2015-08-18 12:15:08 +00:00
assert "Invalid HTTP request form" in r.body
def test_relative_request(self):
p = self.pathoc_raw()
p.connect()
r = p.request("get:/p/200")
assert r.status_code == 400
2015-07-22 11:04:45 +00:00
assert "Invalid HTTP request form" in r.body