diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-02-10 15:04:20 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-02-10 15:04:20 +1300 |
commit | b14c29b25c4c5754edf568bcbb6bbf5b70b6c310 (patch) | |
tree | 21cf7e00219ee4a1a79c368a3abcd64caa947d0a | |
parent | 5326b7610a365d57ff06c0e72c739d2853b695f9 (diff) | |
download | mitmproxy-b14c29b25c4c5754edf568bcbb6bbf5b70b6c310.tar.gz mitmproxy-b14c29b25c4c5754edf568bcbb6bbf5b70b6c310.tar.bz2 mitmproxy-b14c29b25c4c5754edf568bcbb6bbf5b70b6c310.zip |
Expand test coverage.
-rw-r--r-- | libmproxy/filt.py | 16 | ||||
-rw-r--r-- | libmproxy/flow.py | 18 | ||||
-rw-r--r-- | test/test_dump.py | 9 | ||||
-rw-r--r-- | test/test_encoding.py | 1 | ||||
-rw-r--r-- | test/test_filt.py | 2 | ||||
-rw-r--r-- | test/test_flow.py | 44 | ||||
-rw-r--r-- | test/test_utils.py | 12 |
7 files changed, 79 insertions, 23 deletions
diff --git a/libmproxy/filt.py b/libmproxy/filt.py index b5af44de..5fef33ba 100644 --- a/libmproxy/filt.py +++ b/libmproxy/filt.py @@ -1,16 +1,16 @@ # Copyright (C) 2010 Aldo Cortesi -# +# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. @@ -19,7 +19,7 @@ ~q Request ~s Response - + Headers: Patterns are matched against "name: value" strings. Field names are @@ -37,7 +37,7 @@ ~m rex Method ~u rex URL ~c CODE Response code. - rex Equivalent to ~u rex + rex Equivalent to ~u rex """ import re, sys import contrib.pyparsing as pp @@ -69,7 +69,7 @@ class FResp(_Action): help = "Match response" def __call__(self, conn): return conn._is_response() - + class _Rex(_Action): def __init__(self, expr): @@ -84,7 +84,7 @@ def _check_content_type(expr, o): if val and re.search(expr, val[0]): return True return False - + class FContentType(_Rex): code = "t" @@ -126,7 +126,7 @@ class FHead(_Rex): if not val and o._is_response(): val = o.request.headers.match_re(self.expr) return val - + class FHeadRequest(_Rex): code = "hq" diff --git a/libmproxy/flow.py b/libmproxy/flow.py index beaa85ef..57a9310a 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -10,12 +10,6 @@ import controller, version HDR_FORM_URLENCODED = "application/x-www-form-urlencoded" -class RunException(Exception): - def __init__(self, msg, returncode, errout): - Exception.__init__(self, msg) - self.returncode = returncode - self.errout = errout - class ScriptContext: def __init__(self, master): @@ -398,10 +392,10 @@ class Request(HTTPMsg): if not 'host' in headers: headers["host"] = [self._hostport()] content = self.content - if content is not None: - headers["content-length"] = [str(len(content))] - else: + if content is None: content = "" + else: + headers["content-length"] = [str(len(content))] if self.close: headers["connection"] = ["close"] if not _proxy: @@ -555,10 +549,10 @@ class Response(HTTPMsg): ['proxy-connection', 'connection', 'keep-alive', 'transfer-encoding'] ) content = self.content - if content is not None: - headers["content-length"] = [str(len(content))] - else: + if content is None: content = "" + else: + headers["content-length"] = [str(len(content))] if self.request.client_conn.close: headers["connection"] = ["close"] proto = "HTTP/1.1 %s %s"%(self.code, str(self.msg)) diff --git a/test/test_dump.py b/test/test_dump.py index c5efed62..317d628a 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -58,7 +58,7 @@ class uDumpMaster(libpry.AutoTree): o = dump.Options(server_replay=p, kill=True) m = dump.DumpMaster(None, o, None, outfile=cs) - + self._cycle(m, "content") self._cycle(m, "content") @@ -80,6 +80,11 @@ class uDumpMaster(libpry.AutoTree): 0, None, "", verbosity=1, rfile="/nonexistent" ) + libpry.raises( + dump.DumpError, self._dummy_cycle, + 0, None, "", verbosity=1, rfile="test_dump.py" + ) + def test_options(self): o = dump.Options(verbosity = 2) assert o.verbosity == 2 @@ -106,7 +111,7 @@ class uDumpMaster(libpry.AutoTree): self._dummy_cycle, 1, None, - "", + "", wfile = "nonexistentdir/foo" ) diff --git a/test/test_encoding.py b/test/test_encoding.py index fe338da1..6ebe7172 100644 --- a/test/test_encoding.py +++ b/test/test_encoding.py @@ -7,6 +7,7 @@ class uidentity(libpry.AutoTree): def test_simple(self): assert "string" == encoding.decode("identity", "string") assert "string" == encoding.encode("identity", "string") + assert not encoding.encode("nonexistent", "string") def test_fallthrough(self): assert None == encoding.decode("nonexistent encoding", "string") diff --git a/test/test_filt.py b/test/test_filt.py index 6c11938e..4ff7e90e 100644 --- a/test/test_filt.py +++ b/test/test_filt.py @@ -178,6 +178,8 @@ class uMatching(libpry.AutoTree): assert self.q("~m get", q) assert not self.q("~m post", q) assert not self.q("~m get", s) + q.method = "" + assert not self.q("~m get", q) def test_url(self): q = self.req() diff --git a/test/test_flow.py b/test/test_flow.py index e4cd6da2..088a180f 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -15,6 +15,11 @@ class uStickyCookieState(libpry.AutoTree): s.handle_response(f) return s, f + def test_domain_match(self): + s = flow.StickyCookieState(filt.parse(".*")) + assert s.domain_match("www.google.com", ".google.com") + assert s.domain_match("google.com", ".google.com") + def test_handle_response(self): c = "SSID=mooo, FOO=bar; Domain=.google.com; Path=/; "\ "Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; " @@ -295,6 +300,15 @@ class uState(libpry.AutoTree): e = flow.Error(tutils.tflow().request, "message") assert not c.add_error(e) + c = flow.State() + req = tutils.treq() + f = c.add_request(req) + e = flow.Error(f.request, "message") + c.set_limit("~bs message") + assert not c.view + assert c.add_error(e) + #assert c.view + def test_set_limit(self): c = flow.State() @@ -577,6 +591,14 @@ class uRequest(libpry.AutoTree): r2 = r.copy() assert r == r2 + r.content = None + assert r._assemble() + + r.close = True + assert "connection: close" in r._assemble() + + assert r._assemble(True) + def test_getset_form_urlencoded(self): h = flow.Headers() h["content-type"] = [flow.HDR_FORM_URLENCODED] @@ -588,6 +610,8 @@ class uRequest(libpry.AutoTree): r.set_form_urlencoded(d) assert r.get_form_urlencoded() == d + r.headers["content-type"] = ["foo"] + assert not r.get_form_urlencoded() def test_getset_query(self): h = flow.Headers() @@ -652,6 +676,12 @@ class uRequest(libpry.AutoTree): assert not "foo" in r.content assert r.headers["boo"] == ["boo"] + def test_constrain_encoding(self): + r = tutils.treq() + r.headers["accept-encoding"] = ["gzip", "oink"] + r.constrain_encoding() + assert "oink" not in r.headers["accept-encoding"] + def test_decodeencode(self): r = tutils.treq() r.headers["content-encoding"] = ["identity"] @@ -661,6 +691,10 @@ class uRequest(libpry.AutoTree): assert r.content == "falafel" r = tutils.treq() + r.content = "falafel" + assert not r.decode() + + r = tutils.treq() r.headers["content-encoding"] = ["identity"] r.content = "falafel" r.encode("identity") @@ -690,6 +724,12 @@ class uResponse(libpry.AutoTree): resp2 = resp.copy() assert resp2 == resp + resp.content = None + assert resp._assemble() + + resp.request.client_conn.close = True + assert "connection: close" in resp._assemble() + def test_refresh(self): r = tutils.tresp() n = time.time() @@ -809,6 +849,10 @@ class uHeaders(libpry.AutoTree): def setUp(self): self.hd = flow.Headers() + def test_str_err(self): + h = flow.Headers() + libpry.raises(ValueError, h.__setitem__, "key", "foo") + def test_read_simple(self): data = """ Header: one diff --git a/test/test_utils.py b/test/test_utils.py index d9546993..65c76ac9 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -29,6 +29,7 @@ class uhexdump(libpry.AutoTree): def test_simple(self): assert utils.hexdump("one\0"*10) + class udel_all(libpry.AutoTree): def test_simple(self): d = dict(a=1, b=2, c=3) @@ -36,6 +37,13 @@ class udel_all(libpry.AutoTree): assert d.keys() == ["c"] +class uclean_hanging_newline(libpry.AutoTree): + def test_simple(self): + s = "foo\n" + assert utils.clean_hanging_newline(s) == "foo" + assert utils.clean_hanging_newline("foo") == "foo" + + class upretty_size(libpry.AutoTree): def test_simple(self): assert utils.pretty_size(100) == "100B" @@ -222,6 +230,7 @@ class u_parse_url(libpry.AutoTree): class u_parse_size(libpry.AutoTree): def test_simple(self): + assert not utils.parse_size("") assert utils.parse_size("1") == 1 assert utils.parse_size("1k") == 1024 assert utils.parse_size("1m") == 1024**2 @@ -245,5 +254,6 @@ tests = [ udummy_cert(), uLRUCache(), u_parse_url(), - u_parse_size() + u_parse_size(), + uclean_hanging_newline() ] |