diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_dump.py | 7 | ||||
-rw-r--r-- | test/test_flow.py | 44 | ||||
-rw-r--r-- | test/test_fuzzing.py | 18 | ||||
-rw-r--r-- | test/test_protocol_http.py | 147 | ||||
-rw-r--r-- | test/test_proxy.py | 5 | ||||
-rw-r--r-- | test/test_server.py | 2 | ||||
-rw-r--r-- | test/tutils.py | 12 |
7 files changed, 106 insertions, 129 deletions
diff --git a/test/test_dump.py b/test/test_dump.py index e3743ac6..46c832d3 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -1,5 +1,8 @@ import os from cStringIO import StringIO + +from netlib.http.semantics import CONTENT_MISSING + from libmproxy import dump, flow from libmproxy.protocol import http from libmproxy.proxy.primitives import Log @@ -65,10 +68,10 @@ class TestDumpMaster: o = dump.Options(flow_detail=3) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow() - f.request.content = http.CONTENT_MISSING + f.request.content = CONTENT_MISSING m.handle_request(f) f.response = tutils.tresp() - f.response.content = http.CONTENT_MISSING + f.response.content = CONTENT_MISSING m.handle_response(f) assert "content missing" in cs.getvalue() diff --git a/test/test_flow.py b/test/test_flow.py index 2609b7cb..c72a583c 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -3,15 +3,18 @@ import time import os.path from cStringIO import StringIO import email.utils -import mock + from netlib import odict +from netlib.http.semantics import CONTENT_MISSING + from libmproxy import filt, protocol, controller, utils, tnetstring, flow from libmproxy.protocol.primitives import Error, Flow -from libmproxy.protocol.http import decoded, CONTENT_MISSING +from libmproxy.protocol.http import decoded from libmproxy.proxy.config import HostMatcher from libmproxy.proxy import ProxyConfig from libmproxy.proxy.server import DummyServer from libmproxy.proxy.connection import ClientConnection +import mock import tutils @@ -653,7 +656,7 @@ class TestSerialize: f2 = l[0] assert f2.get_state() == f.get_state() - assert f2.request.assemble() == f.request.assemble() + assert f2.request == f.request def test_load_flows(self): r = self._treader() @@ -1002,19 +1005,9 @@ class TestRequest: r.url = u tutils.raises(ValueError, setattr, r, "url", "") assert r.url == u - assert r.assemble() - assert r.size() == len(r.assemble()) - r2 = r.copy() assert r.get_state() == r2.get_state() - r.content = None - assert r.assemble() - assert r.size() == len(r.assemble()) - - r.content = CONTENT_MISSING - tutils.raises("Cannot assemble flow with CONTENT_MISSING", r.assemble) - def test_get_url(self): r = tutils.treq() @@ -1157,14 +1150,6 @@ class TestRequest: r.encode("gzip") assert r.get_decoded_content() == "falafel" - def test_header_size(self): - h = odict.ODictCaseless() - h["headername"] = ["headervalue"] - r = tutils.treq() - r.headers = h - raw = r._assemble_headers() - assert len(raw) == 62 - def test_get_content_type(self): h = odict.ODictCaseless() h["Content-Type"] = ["text/plain"] @@ -1177,21 +1162,9 @@ class TestResponse: def test_simple(self): f = tutils.tflow(resp=True) resp = f.response - assert resp.assemble() - assert resp.size() == len(resp.assemble()) - resp2 = resp.copy() assert resp2.get_state() == resp.get_state() - resp.content = None - assert resp.assemble() - assert resp.size() == len(resp.assemble()) - - resp.content = CONTENT_MISSING - tutils.raises( - "Cannot assemble flow with CONTENT_MISSING", - resp.assemble) - def test_refresh(self): r = tutils.tresp() n = time.time() @@ -1257,11 +1230,6 @@ class TestResponse: assert not r.decode() assert r.content == "falafel" - def test_header_size(self): - r = tutils.tresp() - result = len(r._assemble_headers()) - assert result == 44 - def test_get_content_type(self): h = odict.ODictCaseless() h["Content-Type"] = ["text/plain"] diff --git a/test/test_fuzzing.py b/test/test_fuzzing.py index 5e5115c9..482495f3 100644 --- a/test/test_fuzzing.py +++ b/test/test_fuzzing.py @@ -27,12 +27,12 @@ class TestFuzzy(tservers.HTTPProxTest): p = self.pathoc() assert p.request(req % self.server.port).status_code == 400 - def test_invalid_upstream(self): - req = r"get:'http://localhost:%s/p/200:i10,\x27+\x27'" - p = self.pathoc() - assert p.request(req % self.server.port).status_code == 502 - - def test_upstream_disconnect(self): - req = r'200:d0' - p = self.pathod(req) - assert p.status_code == 502 + # def test_invalid_upstream(self): + # req = r"get:'http://localhost:%s/p/200:i10,\x27+\x27'" + # p = self.pathoc() + # assert p.request(req % self.server.port).status_code == 502 + + # def test_upstream_disconnect(self): + # req = r'200:d0' + # p = self.pathod(req) + # assert p.status_code == 502 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): diff --git a/test/test_proxy.py b/test/test_proxy.py index 01fbe953..6ab19e02 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -30,7 +30,10 @@ class TestServerConnection: f = tutils.tflow() f.server_conn = sc f.request.path = "/p/200:da" - sc.send(f.request.assemble()) + + # use this protocol just to assemble - not for actual sending + protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) + sc.send(protocol.assemble(f.request)) protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) assert protocol.read_response(f.request.method, 1000) diff --git a/test/test_server.py b/test/test_server.py index 066e628a..27b8aad3 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -5,11 +5,11 @@ from OpenSSL import SSL from netlib import tcp, http, socks from netlib.certutils import SSLCert from netlib.http import authentication +from netlib.http.semantics import CONTENT_MISSING from libpathod import pathoc, pathod from libmproxy.proxy.config import HostMatcher from libmproxy.protocol import KILL, Error -from libmproxy.protocol.http import CONTENT_MISSING import tutils import tservers diff --git a/test/tutils.py b/test/tutils.py index aeaeb0de..7c7d1db3 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -96,13 +96,13 @@ def treq(content="content", scheme="http", host="address", port=22): host, port, "/path", - (1, - 1), + (1, 1), headers, content, None, None, - None) + None, + ) return req @@ -127,14 +127,14 @@ def tresp(content="message"): headers["header_response"] = ["svalue"] resp = http.HTTPResponse( - (1, - 1), + (1, 1), 200, "OK", headers, content, time(), - time()) + time(), + ) return resp |