From 953f9aa64166451a07502f05c15db47c053e6081 Mon Sep 17 00:00:00 2001 From: Krzysztof Bielicki Date: Mon, 16 Mar 2015 10:23:50 +0100 Subject: Added tests --- test/test_protocol_http.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 16870777..23c3f469 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -1,3 +1,4 @@ +from mock import MagicMock from libmproxy.protocol.http import * from cStringIO import StringIO import tutils, tservers @@ -112,6 +113,26 @@ class TestHTTPRequest: r = tutils.treq() assert repr(r) + def test_get_form_for_urlencoded(self): + r = tutils.treq() + r.headers.add("content-type", "application/x-www-form-urlencoded") + r.get_form_urlencoded = MagicMock() + + r.get_form() + + assert r.get_form_urlencoded.called + + def test_get_form_for_multipart(self): + r = tutils.treq() + r.headers.add("content-type", "multipart/form-data") + r.get_form_multipart = MagicMock() + + r.get_form() + + assert r.get_form_multipart.called + + + class TestHTTPResponse: def test_read_from_stringio(self): -- cgit v1.2.3 From 6852eb9d0af5cf7196da5cad2ccf3b036e348226 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 10 Apr 2015 14:59:38 +0200 Subject: fix #553 --- test/test_protocol_http.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 23c3f469..c6fad508 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -55,6 +55,14 @@ class TestHTTPRequest: r.update_host_header() assert "Host" in r.headers + def test_expect_header(self): + s = StringIO("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" + assert r.content == "foo" + assert s.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) -- cgit v1.2.3 From 923503260ed48510da63e4dd1d6b36b4b859b110 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 10 Apr 2015 19:42:32 +0200 Subject: add tests --- test/test_protocol_http.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index c6fad508..e3593ae3 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -121,6 +121,20 @@ class TestHTTPRequest: r = tutils.treq() assert repr(r) + def test_pretty_host(self): + r = tutils.treq() + assert r.pretty_host(True) == "address" + assert r.pretty_host(False) == "address" + r.headers["host"] = ["other"] + assert r.pretty_host(True) == "other" + assert r.pretty_host(False) == "address" + r.host = None + assert r.pretty_host(True) == "other" + assert r.pretty_host(False) is None + del r.headers["host"] + assert r.pretty_host(True) is None + assert r.pretty_host(False) is None + def test_get_form_for_urlencoded(self): r = tutils.treq() r.headers.add("content-type", "application/x-www-form-urlencoded") -- cgit v1.2.3 From bea0bd236a60e3e6c80027448e51b7802394304a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 14 Apr 2015 11:58:10 +1200 Subject: Housekeeping and cleanups - No output to stdout on load in examples - they muck up the test suite. - Use the odict module directly, rather than aliasing it. The small convenience this gives to scripters is not worth it. - Move the cookie tests from the flow test module to the protocol_http test module. --- test/test_protocol_http.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 23c3f469..08ed114c 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -1,6 +1,10 @@ +from cStringIO import StringIO + from mock import MagicMock + from libmproxy.protocol.http import * -from cStringIO import StringIO +from netlib import odict + import tutils, tservers @@ -131,6 +135,41 @@ class TestHTTPRequest: assert r.get_form_multipart.called + def test_get_cookies_none(self): + h = odict.ODictCaseless() + r = tutils.treq() + r.headers = h + assert r.get_cookies() is None + + def test_get_cookies_single(self): + h = odict.ODictCaseless() + h["Cookie"] = ["cookiename=cookievalue"] + r = tutils.treq() + r.headers = h + result = r.get_cookies() + assert len(result)==1 + assert result['cookiename']==('cookievalue',{}) + + def test_get_cookies_double(self): + h = odict.ODictCaseless() + h["Cookie"] = ["cookiename=cookievalue;othercookiename=othercookievalue"] + r = tutils.treq() + r.headers = h + result = r.get_cookies() + assert len(result)==2 + assert result['cookiename']==('cookievalue',{}) + assert result['othercookiename']==('othercookievalue',{}) + + def test_get_cookies_withequalsign(self): + h = odict.ODictCaseless() + h["Cookie"] = ["cookiename=coo=kievalue;othercookiename=othercookievalue"] + r = tutils.treq() + r.headers = h + result = r.get_cookies() + assert len(result)==2 + assert result['cookiename']==('coo=kievalue',{}) + assert result['othercookiename']==('othercookievalue',{}) + -- cgit v1.2.3 From e17eacd8d77c78daa88d8f89ace990463378d98d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 14 Apr 2015 13:45:38 +1200 Subject: New get_cookie and set_cookie implementations for HTTPRequest --- test/test_protocol_http.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 6b949ce3..11ab503b 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -161,7 +161,7 @@ class TestHTTPRequest: h = odict.ODictCaseless() r = tutils.treq() r.headers = h - assert r.get_cookies() is None + assert len(r.get_cookies()) == 0 def test_get_cookies_single(self): h = odict.ODictCaseless() @@ -170,7 +170,7 @@ class TestHTTPRequest: r.headers = h result = r.get_cookies() assert len(result)==1 - assert result['cookiename']==('cookievalue',{}) + assert result['cookiename']==['cookievalue'] def test_get_cookies_double(self): h = odict.ODictCaseless() @@ -179,8 +179,8 @@ class TestHTTPRequest: r.headers = h result = r.get_cookies() assert len(result)==2 - assert result['cookiename']==('cookievalue',{}) - assert result['othercookiename']==('othercookievalue',{}) + assert result['cookiename']==['cookievalue'] + assert result['othercookiename']==['othercookievalue'] def test_get_cookies_withequalsign(self): h = odict.ODictCaseless() @@ -189,10 +189,18 @@ class TestHTTPRequest: r.headers = h result = r.get_cookies() assert len(result)==2 - assert result['cookiename']==('coo=kievalue',{}) - assert result['othercookiename']==('othercookievalue',{}) - + assert result['cookiename']==['coo=kievalue'] + assert result['othercookiename']==['othercookievalue'] + def test_set_cookies(self): + h = odict.ODictCaseless() + h["Cookie"] = ["cookiename=cookievalue"] + r = tutils.treq() + r.headers = h + result = r.get_cookies() + result["cookiename"] = ["foo"] + r.set_cookies(result) + assert r.get_cookies()["cookiename"] == ["foo"] class TestHTTPResponse: -- cgit v1.2.3 From ab7e2857cc9095c4cee8ca9b569c16516aa520ba Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 14 Apr 2015 15:14:36 +1200 Subject: New get_cookies for HttpResponse --- test/test_protocol_http.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 11ab503b..0276cab7 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -230,6 +230,62 @@ class TestHTTPResponse: assert "foo" in repr(r) assert repr(tutils.tresp(content=CONTENT_MISSING)) + def test_get_cookies_none(self): + h = odict.ODictCaseless() + resp = tutils.tresp() + resp.headers = h + assert not resp.get_cookies() + + def test_get_cookies_simple(self): + h = odict.ODictCaseless() + h["Set-Cookie"] = ["cookiename=cookievalue"] + resp = tutils.tresp() + resp.headers = h + result = resp.get_cookies() + assert len(result)==1 + assert "cookiename" in result + assert result["cookiename"][0] == ["cookievalue", odict.ODict()] + + def test_get_cookies_with_parameters(self): + h = odict.ODictCaseless() + h["Set-Cookie"] = ["cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly"] + resp = tutils.tresp() + resp.headers = h + result = resp.get_cookies() + assert len(result)==1 + assert "cookiename" in result + assert result["cookiename"][0][0] == "cookievalue" + attrs = result["cookiename"][0][1] + assert len(attrs)==4 + assert attrs["domain"] == ["example.com"] + assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"] + assert attrs["path"] == ["/"] + assert attrs["httponly"] == [None] + + def test_get_cookies_no_value(self): + h = odict.ODictCaseless() + h["Set-Cookie"] = ["cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"] + resp = tutils.tresp() + resp.headers = h + result = resp.get_cookies() + assert len(result)==1 + assert "cookiename" in result + assert result["cookiename"][0][0] == "" + assert len(result["cookiename"][0][1])==2 + + def test_get_cookies_twocookies(self): + h = odict.ODictCaseless() + h["Set-Cookie"] = ["cookiename=cookievalue","othercookie=othervalue"] + resp = tutils.tresp() + resp.headers = h + result = resp.get_cookies() + assert len(result)==2 + assert "cookiename" in result + assert result["cookiename"][0] == ["cookievalue", odict.ODict()] + assert "othercookie" in result + assert result["othercookie"][0] == ["othervalue", odict.ODict()] + + class TestHTTPFlow(object): def test_repr(self): -- cgit v1.2.3 From c335c2b5330865ccab176c6213db63151383a142 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 14 Apr 2015 16:23:51 +1200 Subject: Add set_cookies method to HTTPResponse --- test/test_protocol_http.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 0276cab7..28c63430 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -285,6 +285,17 @@ class TestHTTPResponse: assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()] + def test_set_cookies(self): + h = odict.ODictCaseless() + resp = tutils.tresp() + v = resp.get_cookies() + v.add("foo", ["bar", odict.ODictCaseless()]) + resp.set_cookies(v) + + v = resp.get_cookies() + assert len(v) == 1 + assert v["foo"] == [["bar", odict.ODictCaseless()]] + class TestHTTPFlow(object): -- cgit v1.2.3 From 0f269f742339fa0703b8583641d76516fa0fba1e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 15 Apr 2015 09:14:20 +1200 Subject: Whitespace, formatting --- test/test_protocol_http.py | 56 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 28c63430..c39f9abb 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -5,7 +5,8 @@ from mock import MagicMock from libmproxy.protocol.http import * from netlib import odict -import tutils, tservers +import tutils +import tservers def test_HttpAuthenticationError(): @@ -169,28 +170,32 @@ class TestHTTPRequest: r = tutils.treq() r.headers = h result = r.get_cookies() - assert len(result)==1 - assert result['cookiename']==['cookievalue'] + assert len(result) == 1 + assert result['cookiename'] == ['cookievalue'] def test_get_cookies_double(self): h = odict.ODictCaseless() - h["Cookie"] = ["cookiename=cookievalue;othercookiename=othercookievalue"] + h["Cookie"] = [ + "cookiename=cookievalue;othercookiename=othercookievalue" + ] r = tutils.treq() r.headers = h result = r.get_cookies() - assert len(result)==2 - assert result['cookiename']==['cookievalue'] - assert result['othercookiename']==['othercookievalue'] + assert len(result) == 2 + assert result['cookiename'] == ['cookievalue'] + assert result['othercookiename'] == ['othercookievalue'] def test_get_cookies_withequalsign(self): h = odict.ODictCaseless() - h["Cookie"] = ["cookiename=coo=kievalue;othercookiename=othercookievalue"] + h["Cookie"] = [ + "cookiename=coo=kievalue;othercookiename=othercookievalue" + ] r = tutils.treq() r.headers = h result = r.get_cookies() - assert len(result)==2 - assert result['cookiename']==['coo=kievalue'] - assert result['othercookiename']==['othercookievalue'] + assert len(result) == 2 + assert result['cookiename'] == ['coo=kievalue'] + assert result['othercookiename'] == ['othercookievalue'] def test_set_cookies(self): h = odict.ODictCaseless() @@ -218,10 +223,14 @@ class TestHTTPResponse: assert HTTPResponse.from_stream(s, "GET").code == 204 s = StringIO(_s) - r = HTTPResponse.from_stream(s, "HEAD") # HEAD must not have content by spec. We should leave it on the pipe. + # HEAD must not have content by spec. We should leave it on the pipe. + r = HTTPResponse.from_stream(s, "HEAD") assert r.code == 200 assert r.content == "" - tutils.raises("Invalid server response: 'content", HTTPResponse.from_stream, s, "GET") + tutils.raises( + "Invalid server response: 'content", + HTTPResponse.from_stream, s, "GET" + ) def test_repr(self): r = tutils.tresp() @@ -242,7 +251,7 @@ class TestHTTPResponse: resp = tutils.tresp() resp.headers = h result = resp.get_cookies() - assert len(result)==1 + assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] @@ -252,11 +261,11 @@ class TestHTTPResponse: resp = tutils.tresp() resp.headers = h result = resp.get_cookies() - assert len(result)==1 + assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "cookievalue" attrs = result["cookiename"][0][1] - assert len(attrs)==4 + assert len(attrs) == 4 assert attrs["domain"] == ["example.com"] assert attrs["expires"] == ["Wed Oct 21 16:29:41 2015"] assert attrs["path"] == ["/"] @@ -264,29 +273,30 @@ class TestHTTPResponse: def test_get_cookies_no_value(self): h = odict.ODictCaseless() - h["Set-Cookie"] = ["cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"] + h["Set-Cookie"] = [ + "cookiename=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/" + ] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() - assert len(result)==1 + assert len(result) == 1 assert "cookiename" in result assert result["cookiename"][0][0] == "" - assert len(result["cookiename"][0][1])==2 + assert len(result["cookiename"][0][1]) == 2 def test_get_cookies_twocookies(self): h = odict.ODictCaseless() - h["Set-Cookie"] = ["cookiename=cookievalue","othercookie=othervalue"] + h["Set-Cookie"] = ["cookiename=cookievalue", "othercookie=othervalue"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() - assert len(result)==2 + assert len(result) == 2 assert "cookiename" in result assert result["cookiename"][0] == ["cookievalue", odict.ODict()] assert "othercookie" in result assert result["othercookie"][0] == ["othervalue", odict.ODict()] def test_set_cookies(self): - h = odict.ODictCaseless() resp = tutils.tresp() v = resp.get_cookies() v.add("foo", ["bar", odict.ODictCaseless()]) @@ -297,7 +307,6 @@ class TestHTTPResponse: assert v["foo"] == [["bar", odict.ODictCaseless()]] - class TestHTTPFlow(object): def test_repr(self): f = tutils.tflow(resp=True, err=True) @@ -306,6 +315,7 @@ class TestHTTPFlow(object): class TestInvalidRequests(tservers.HTTPProxTest): ssl = True + def test_double_connect(self): p = self.pathoc() r = p.request("connect:'%s:%s'" % ("127.0.0.1", self.server2.port)) -- cgit v1.2.3 From a05a70d8168a07c92b2a3ecbbb1958d85532efe3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 30 May 2015 12:03:28 +1200 Subject: Add coding style check, reformat. --- test/test_protocol_http.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index c39f9abb..884a528e 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -61,7 +61,8 @@ class TestHTTPRequest: assert "Host" in r.headers def test_expect_header(self): - s = StringIO("GET / HTTP/1.1\r\nContent-Length: 3\r\nExpect: 100-continue\r\n\r\nfoobar") + s = StringIO( + "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" @@ -84,7 +85,8 @@ class TestHTTPRequest: 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" + assert r.assemble( + ) == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\nContent-Length: 0\r\n\r\n" def test_http_options_relative_form_in(self): """ @@ -105,10 +107,10 @@ class TestHTTPRequest: r.host = 'address' r.port = 80 r.scheme = "http" - assert r.assemble() == ("OPTIONS http://address:80/secret/resource HTTP/1.1\r\n" - "Host: address\r\n" - "Content-Length: 0\r\n\r\n") - + assert r.assemble() == ( + "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() @@ -257,7 +259,8 @@ class TestHTTPResponse: def test_get_cookies_with_parameters(self): h = odict.ODictCaseless() - h["Set-Cookie"] = ["cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly"] + h["Set-Cookie"] = [ + "cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly"] resp = tutils.tresp() resp.headers = h result = resp.get_cookies() -- cgit v1.2.3 From 7890450b0c9d0cd95a2e5f507a9a8247702051be Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 12 Jun 2015 16:00:16 +1200 Subject: Handle invalid IDNA encoding in hostnames Fixes #622 --- test/test_protocol_http.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/test_protocol_http.py') diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 884a528e..d8489d4d 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -142,6 +142,10 @@ class TestHTTPRequest: assert r.pretty_host(True) is None assert r.pretty_host(False) is None + # Invalid IDNA + r.headers["host"] = [".disqus.com"] + assert r.pretty_host(True) == ".disqus.com" + def test_get_form_for_urlencoded(self): r = tutils.treq() r.headers.add("content-type", "application/x-www-form-urlencoded") -- cgit v1.2.3