aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_protocol_http.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2015-08-01 11:38:33 +1200
committerAldo Cortesi <aldo@corte.si>2015-08-01 11:38:33 +1200
commitc31b6c3c36f681d1dbc3ce11922741b7e1e41837 (patch)
treef63a0f56534d16437aaa5464cf585cac82f90985 /test/test_protocol_http.py
parentcdc84f52d213cb2b2b2a06a17378ebe757908865 (diff)
parent4f38c6b90e239d192863dee271e267b498c72206 (diff)
downloadmitmproxy-c31b6c3c36f681d1dbc3ce11922741b7e1e41837.tar.gz
mitmproxy-c31b6c3c36f681d1dbc3ce11922741b7e1e41837.tar.bz2
mitmproxy-c31b6c3c36f681d1dbc3ce11922741b7e1e41837.zip
Merge pull request #698 from Kriechi/http2-wip
[WIP] Protocol Refactoring for HTTP/2
Diffstat (limited to 'test/test_protocol_http.py')
-rw-r--r--test/test_protocol_http.py147
1 files changed, 75 insertions, 72 deletions
diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py
index 747fdc1e..75f0a7b9 100644
--- a/test/test_protocol_http.py
+++ b/test/test_protocol_http.py
@@ -1,13 +1,22 @@
+import cStringIO
from cStringIO import StringIO
from mock import MagicMock
from libmproxy.protocol.http import *
from netlib import odict
+from netlib.http import http1
+from netlib.http.semantics import CONTENT_MISSING
import tutils
import tservers
+def mock_protocol(data='', chunked=False):
+ rfile = cStringIO.StringIO(data)
+ wfile = cStringIO.StringIO()
+ return http1.HTTP1Protocol(rfile=rfile, wfile=wfile)
+
+
def test_HttpAuthenticationError():
x = HttpAuthenticationError({"foo": "bar"})
@@ -15,107 +24,100 @@ def test_HttpAuthenticationError():
assert "foo" in x.headers
-def test_stripped_chunked_encoding_no_content():
- """
- https://github.com/mitmproxy/mitmproxy/issues/186
- """
- r = tutils.tresp(content="")
- r.headers["Transfer-Encoding"] = ["chunked"]
- assert "Content-Length" in r._assemble_headers()
-
- r = tutils.treq(content="")
- r.headers["Transfer-Encoding"] = ["chunked"]
- assert "Content-Length" in r._assemble_headers()
-
+# TODO: move test to netlib
+# def test_stripped_chunked_encoding_no_content():
+# """
+# https://github.com/mitmproxy/mitmproxy/issues/186
+# """
+# r = tutils.tresp(content="")
+# r.headers["Transfer-Encoding"] = ["chunked"]
+# assert "Content-Length" in r._assemble_headers()
+#
+# r = tutils.treq(content="")
+# r.headers["Transfer-Encoding"] = ["chunked"]
+# assert "Content-Length" in r._assemble_headers()
+#
class TestHTTPRequest:
def test_asterisk_form_in(self):
- s = StringIO("OPTIONS * HTTP/1.1")
f = tutils.tflow(req=None)
- f.request = HTTPRequest.from_stream(s)
+ protocol = mock_protocol("OPTIONS * HTTP/1.1")
+ f.request = HTTPRequest.from_protocol(protocol)
+
assert f.request.form_in == "relative"
f.request.host = f.server_conn.address.host
f.request.port = f.server_conn.address.port
f.request.scheme = "http"
- assert f.request.assemble() == ("OPTIONS * HTTP/1.1\r\n"
- "Host: address:22\r\n"
- "Content-Length: 0\r\n\r\n")
+ assert protocol.assemble(f.request) == (
+ "OPTIONS * HTTP/1.1\r\n"
+ "Host: address:22\r\n"
+ "Content-Length: 0\r\n\r\n")
def test_relative_form_in(self):
- s = StringIO("GET /foo\xff HTTP/1.1")
- tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
- s = StringIO("GET /foo HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: h2c")
- r = HTTPRequest.from_stream(s)
- assert r.headers["Upgrade"] == ["h2c"]
-
- raw = r._assemble_headers()
- assert "Upgrade" not in raw
- assert "Host" not in raw
-
- r.url = "http://example.com/foo"
+ protocol = mock_protocol("GET /foo\xff HTTP/1.1")
+ tutils.raises("Bad HTTP request line", HTTPRequest.from_protocol, protocol)
- raw = r._assemble_headers()
- assert "Host" in raw
- assert not "Host" in r.headers
- r.update_host_header()
- assert "Host" in r.headers
+ protocol = mock_protocol("GET /foo HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: h2c")
+ r = HTTPRequest.from_protocol(protocol)
+ assert r.headers["Upgrade"] == ["h2c"]
def test_expect_header(self):
- s = StringIO(
+ protocol = mock_protocol(
"GET / HTTP/1.1\r\nContent-Length: 3\r\nExpect: 100-continue\r\n\r\nfoobar")
- w = StringIO()
- r = HTTPRequest.from_stream(s, wfile=w)
- assert w.getvalue() == "HTTP/1.1 100 Continue\r\n\r\n"
+ r = HTTPRequest.from_protocol(protocol)
+ assert protocol.tcp_handler.wfile.getvalue() == "HTTP/1.1 100 Continue\r\n\r\n"
assert r.content == "foo"
- assert s.read(3) == "bar"
+ assert protocol.tcp_handler.rfile.read(3) == "bar"
def test_authority_form_in(self):
- s = StringIO("CONNECT oops-no-port.com HTTP/1.1")
- tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
- s = StringIO("CONNECT address:22 HTTP/1.1")
- r = HTTPRequest.from_stream(s)
+ protocol = mock_protocol("CONNECT oops-no-port.com HTTP/1.1")
+ tutils.raises("Bad HTTP request line", HTTPRequest.from_protocol, protocol)
+
+ protocol = mock_protocol("CONNECT address:22 HTTP/1.1")
+ r = HTTPRequest.from_protocol(protocol)
r.scheme, r.host, r.port = "http", "address", 22
- assert r.assemble() == ("CONNECT address:22 HTTP/1.1\r\n"
- "Host: address:22\r\n"
- "Content-Length: 0\r\n\r\n")
+ assert protocol.assemble(r) == (
+ "CONNECT address:22 HTTP/1.1\r\n"
+ "Host: address:22\r\n"
+ "Content-Length: 0\r\n\r\n")
assert r.pretty_url(False) == "address:22"
def test_absolute_form_in(self):
- s = StringIO("GET oops-no-protocol.com HTTP/1.1")
- tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
- s = StringIO("GET http://address:22/ HTTP/1.1")
- r = HTTPRequest.from_stream(s)
- assert r.assemble(
- ) == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\nContent-Length: 0\r\n\r\n"
+ protocol = mock_protocol("GET oops-no-protocol.com HTTP/1.1")
+ tutils.raises("Bad HTTP request line", HTTPRequest.from_protocol, protocol)
+
+ protocol = mock_protocol("GET http://address:22/ HTTP/1.1")
+ r = HTTPRequest.from_protocol(protocol)
+ assert protocol.assemble(r) == (
+ "GET http://address:22/ HTTP/1.1\r\n"
+ "Host: address:22\r\n"
+ "Content-Length: 0\r\n\r\n")
def test_http_options_relative_form_in(self):
"""
Exercises fix for Issue #392.
"""
- s = StringIO("OPTIONS /secret/resource HTTP/1.1")
- r = HTTPRequest.from_stream(s)
+ protocol = mock_protocol("OPTIONS /secret/resource HTTP/1.1")
+ r = HTTPRequest.from_protocol(protocol)
r.host = 'address'
r.port = 80
r.scheme = "http"
- assert r.assemble() == ("OPTIONS /secret/resource HTTP/1.1\r\n"
- "Host: address\r\n"
- "Content-Length: 0\r\n\r\n")
+ assert protocol.assemble(r) == (
+ "OPTIONS /secret/resource HTTP/1.1\r\n"
+ "Host: address\r\n"
+ "Content-Length: 0\r\n\r\n")
def test_http_options_absolute_form_in(self):
- s = StringIO("OPTIONS http://address/secret/resource HTTP/1.1")
- r = HTTPRequest.from_stream(s)
+ protocol = mock_protocol("OPTIONS http://address/secret/resource HTTP/1.1")
+ r = HTTPRequest.from_protocol(protocol)
r.host = 'address'
r.port = 80
r.scheme = "http"
- assert r.assemble() == (
+ assert protocol.assemble(r) == (
"OPTIONS http://address:80/secret/resource HTTP/1.1\r\n"
"Host: address\r\n"
"Content-Length: 0\r\n\r\n")
- def test_assemble_unknown_form(self):
- r = tutils.treq()
- tutils.raises("Invalid request form", r.assemble, "antiauthority")
-
def test_set_url(self):
r = tutils.treq_absolute()
r.url = "https://otheraddress:42/ORLY"
@@ -216,26 +218,27 @@ class TestHTTPRequest:
class TestHTTPResponse:
def test_read_from_stringio(self):
- _s = "HTTP/1.1 200 OK\r\n" \
+ s = "HTTP/1.1 200 OK\r\n" \
"Content-Length: 7\r\n" \
"\r\n"\
"content\r\n" \
"HTTP/1.1 204 OK\r\n" \
"\r\n"
- s = StringIO(_s)
- r = HTTPResponse.from_stream(s, "GET")
- assert r.code == 200
+
+ protocol = mock_protocol(s)
+ r = HTTPResponse.from_protocol(protocol, "GET")
+ assert r.status_code == 200
assert r.content == "content"
- assert HTTPResponse.from_stream(s, "GET").code == 204
+ assert HTTPResponse.from_protocol(protocol, "GET").status_code == 204
- s = StringIO(_s)
+ protocol = mock_protocol(s)
# HEAD must not have content by spec. We should leave it on the pipe.
- r = HTTPResponse.from_stream(s, "HEAD")
- assert r.code == 200
+ r = HTTPResponse.from_protocol(protocol, "HEAD")
+ assert r.status_code == 200
assert r.content == ""
tutils.raises(
"Invalid server response: 'content",
- HTTPResponse.from_stream, s, "GET"
+ HTTPResponse.from_protocol, protocol, "GET"
)
def test_repr(self):