diff options
author | Maximilian Hils <git@maximilianhils.com> | 2014-02-07 03:56:57 +0100 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2014-02-07 03:56:57 +0100 |
commit | d07029d575532122f3d1f81141710ba36ca307e0 (patch) | |
tree | 3295115b4b556999be7885dce414abe1377ae72d /test | |
parent | 9f5f2b707165d14cbf0ace9a55ec2d1dc44d9802 (diff) | |
download | mitmproxy-d07029d575532122f3d1f81141710ba36ca307e0.tar.gz mitmproxy-d07029d575532122f3d1f81141710ba36ca307e0.tar.bz2 mitmproxy-d07029d575532122f3d1f81141710ba36ca307e0.zip |
coverage++
Diffstat (limited to 'test')
-rw-r--r-- | test/test_flow.py | 8 | ||||
-rw-r--r-- | test/test_protocol_http.py | 86 | ||||
-rw-r--r-- | test/test_script.py | 3 | ||||
-rw-r--r-- | test/tutils.py | 12 |
4 files changed, 104 insertions, 5 deletions
diff --git a/test/test_flow.py b/test/test_flow.py index 006dfe51..65e153ea 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -1047,7 +1047,7 @@ class TestResponse: r = tutils.tresp() r.headers["content-encoding"] = ["identity"] r.content = "falafel" - r.decode() + assert r.decode() assert not r.headers["content-encoding"] assert r.content == "falafel" @@ -1064,10 +1064,14 @@ class TestResponse: r.encode("gzip") assert r.headers["content-encoding"] == ["gzip"] assert r.content != "falafel" - r.decode() + assert r.decode() assert not r.headers["content-encoding"] assert r.content == "falafel" + r.headers["content-encoding"] = ["gzip"] + assert not r.decode() + assert r.content == "falafel" + def test_header_size(self): r = tutils.tresp() result = len(r._assemble_headers()) diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py new file mode 100644 index 00000000..9e043049 --- /dev/null +++ b/test/test_protocol_http.py @@ -0,0 +1,86 @@ +from libmproxy import proxy # FIXME: Remove +from libmproxy.protocol.http import * +from cStringIO import StringIO +import tutils + + +def test_HttpAuthenticationError(): + x = HttpAuthenticationError({"foo": "bar"}) + assert str(x) + assert "foo" in x.auth_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() + + +class TestHTTPRequest: + def test_asterisk_form(self): + s = StringIO("OPTIONS * HTTP/1.1") + f = tutils.tflow_noreq() + f.request = HTTPRequest.from_stream(s) + assert f.request.form_in == "asterisk" + x = f.request._assemble() + assert f.request._assemble() == "OPTIONS * HTTP/1.1\r\nHost: address:22\r\n\r\n" + + def test_origin_form(self): + s = StringIO("GET /foo\xff HTTP/1.1") + tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) + + def test_authority_form(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) + assert r._assemble() == "CONNECT address:22 HTTP/1.1\r\nHost: address:22\r\n\r\n" + + + def test_absolute_form(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\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.set_url("https://otheraddress:42/ORLY") + assert r.scheme == "https" + assert r.host == "otheraddress" + assert r.port == 42 + assert r.path == "/ORLY" + + +class TestHTTPResponse: + def test_read_from_stringio(self): + _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 + assert r.content == "content" + assert HTTPResponse.from_stream(s, "GET").code == 204 + + s = StringIO(_s) + r = HTTPResponse.from_stream(s, "HEAD") + assert r.code == 200 + assert r.content == "" + tutils.raises("Invalid server response: 'content", HTTPResponse.from_stream, s, "GET")
\ No newline at end of file diff --git a/test/test_script.py b/test/test_script.py index 2e48081b..2999a910 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -75,9 +75,6 @@ class TestScript: # Two instantiations assert m.call_count == 2 assert (time.time() - t_start) < 0.09 - time.sleep(0.3 - (time.time() - t_start)) - # Plus two invocations - assert m.call_count == 4 def test_concurrent2(self): s = flow.State() diff --git a/test/tutils.py b/test/tutils.py index ad2960d9..75fb7c0b 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -39,6 +39,14 @@ def tserver_conn(): return c +def treq_absolute(conn=None, content="content"): + r = treq(conn, content) + r.form_in = r.form_out = "absolute" + r.host = "address" + r.port = 22 + r.scheme = "http" + return r + def treq(conn=None, content="content"): if not conn: conn = tclient_conn() @@ -78,6 +86,10 @@ def terr(req=None): f.error.reply = controller.DummyReply() return f.error +def tflow_noreq(): + f = tflow() + f.request = None + return f def tflow(req=None): if not req: |