diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-06-09 10:57:00 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-06-09 10:57:00 +1200 |
commit | e78b48ab20a7250ccb50c878183ae68ea1748c40 (patch) | |
tree | 19585f7ef5358a6060c74a54662f4d7a6937de05 | |
parent | 7a312546f3185a2cff36a4f422c852b15b182140 (diff) | |
download | mitmproxy-e78b48ab20a7250ccb50c878183ae68ea1748c40.tar.gz mitmproxy-e78b48ab20a7250ccb50c878183ae68ea1748c40.tar.bz2 mitmproxy-e78b48ab20a7250ccb50c878183ae68ea1748c40.zip |
Start conversion to nose.
RIP pry.
-rw-r--r-- | test/.gitignore | 1 | ||||
-rw-r--r-- | test/test_server.py | 1 | ||||
-rw-r--r-- | test/test_utils.py | 310 | ||||
-rw-r--r-- | test/tutils.py | 39 | ||||
-rw-r--r-- | todo | 3 |
5 files changed, 178 insertions, 176 deletions
diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 00000000..6350e986 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +.coverage diff --git a/test/test_server.py b/test/test_server.py index 3bfbab5b..efd269eb 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -1,6 +1,5 @@ import urllib, urllib2 import libpathod.test, requests -import libpry import tutils class uSanity(tutils.ProxTest): diff --git a/test/test_utils.py b/test/test_utils.py index 8bff0303..8a881b4e 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,202 +1,168 @@ import textwrap, re, json -import libpry from libmproxy import utils +from nose.tools import assert_raises +import tutils utils.CERT_SLEEP_TIME = 0 -class uformat_timestamp(libpry.AutoTree): - def test_simple(self): - assert utils.format_timestamp(utils.timestamp()) +def test_format_timestamp(): + assert utils.format_timestamp(utils.timestamp()) -class uisBin(libpry.AutoTree): - def test_simple(self): - assert not utils.isBin("testing\n\r") - assert utils.isBin("testing\x01") - assert utils.isBin("testing\x0e") - assert utils.isBin("testing\x7f") +def test_isBin(): + assert not utils.isBin("testing\n\r") + assert utils.isBin("testing\x01") + assert utils.isBin("testing\x0e") + assert utils.isBin("testing\x7f") -class uisXML(libpry.AutoTree): - def test_simple(self): - assert not utils.isXML("foo") - assert utils.isXML("<foo") - assert utils.isXML(" \n<foo") +def test_isXml(): + assert not utils.isXML("foo") + assert utils.isXML("<foo") + assert utils.isXML(" \n<foo") -class uhexdump(libpry.AutoTree): - def test_simple(self): - assert utils.hexdump("one\0"*10) +def test_hexdump(): + assert utils.hexdump("one\0"*10) -class udel_all(libpry.AutoTree): - def test_simple(self): - d = dict(a=1, b=2, c=3) - utils.del_all(d, ["a", "x", "b"]) - assert d.keys() == ["c"] +def test_del_all(): + d = dict(a=1, b=2, c=3) + utils.del_all(d, ["a", "x", "b"]) + 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" +def test_clean_hanging_newline(): + 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" - assert utils.pretty_size(1024) == "1kB" - assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB" - assert utils.pretty_size(1024*1024) == "1M" +def test_pretty_size(): + assert utils.pretty_size(100) == "100B" + assert utils.pretty_size(1024) == "1kB" + assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB" + assert utils.pretty_size(1024*1024) == "1M" -class uData(libpry.AutoTree): - def test_nonexistent(self): - assert utils.pkg_data.path("console") - libpry.raises("does not exist", utils.pkg_data.path, "nonexistent") +def test_pkg_data(): + assert utils.pkg_data.path("console") + tutils.raises("does not exist", utils.pkg_data.path, "nonexistent") -class upretty_json(libpry.AutoTree): - def test_one(self): - s = json.dumps({"foo": 1}) - assert utils.pretty_json(s) - assert not utils.pretty_json("moo") +def test_pretty_json(): + s = json.dumps({"foo": 1}) + assert utils.pretty_json(s) + assert not utils.pretty_json("moo") -class u_urldecode(libpry.AutoTree): - def test_one(self): - s = "one=two&three=four" - assert len(utils.urldecode(s)) == 2 +def test_urldecode(): + s = "one=two&three=four" + assert len(utils.urldecode(s)) == 2 -class uLRUCache(libpry.AutoTree): - def test_one(self): - class Foo: - ran = False - @utils.LRUCache(2) - def one(self, x): - self.ran = True - return x +def test_LRUCache(): + class Foo: + ran = False + @utils.LRUCache(2) + def one(self, x): + self.ran = True + return x - f = Foo() - assert f.one(1) == 1 - assert f.ran - f.ran = False - assert f.one(1) == 1 - assert not f.ran + f = Foo() + assert f.one(1) == 1 + assert f.ran + f.ran = False + assert f.one(1) == 1 + assert not f.ran - f.ran = False - assert f.one(1) == 1 - assert not f.ran - assert f.one(2) == 2 - assert f.one(3) == 3 - assert f.ran + f.ran = False + assert f.one(1) == 1 + assert not f.ran + assert f.one(2) == 2 + assert f.one(3) == 3 + assert f.ran - f.ran = False - assert f.one(1) == 1 - assert f.ran + f.ran = False + assert f.one(1) == 1 + assert f.ran + + assert len(f._cached_one) == 2 + assert len(f._cachelist_one) == 2 + + +def test_parse_proxy_spec(): + assert not utils.parse_proxy_spec("") + assert utils.parse_proxy_spec("http://foo.com:88") == ("http", "foo.com", 88) + assert utils.parse_proxy_spec("http://foo.com") == ("http", "foo.com", 80) + assert not utils.parse_proxy_spec("foo.com") + assert not utils.parse_proxy_spec("http://") + + +def test_unparse_url(): + assert utils.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" + assert utils.unparse_url("http", "foo.com", 80, "") == "http://foo.com" + assert utils.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" + assert utils.unparse_url("https", "foo.com", 443, "") == "https://foo.com" + + +def test_parse_url(): + assert not utils.parse_url("") + + u = "http://foo.com:8888/test" + s, h, po, pa = utils.parse_url(u) + assert s == "http" + assert h == "foo.com" + assert po == 8888 + assert pa == "/test" + + s, h, po, pa = utils.parse_url("http://foo/bar") + assert s == "http" + assert h == "foo" + assert po == 80 + assert pa == "/bar" + + s, h, po, pa = utils.parse_url("http://foo") + assert pa == "/" + + s, h, po, pa = utils.parse_url("https://foo") + assert po == 443 + + assert not utils.parse_url("https://foo:bar") + assert not utils.parse_url("https://foo:") + + +def test_parse_size(): + assert not utils.parse_size("") + assert utils.parse_size("1") == 1 + assert utils.parse_size("1k") == 1024 + assert utils.parse_size("1m") == 1024**2 + assert utils.parse_size("1g") == 1024**3 + tutils.raises(ValueError, utils.parse_size, "1f") + tutils.raises(ValueError, utils.parse_size, "ak") + + +def test_parse_content_type(): + p = utils.parse_content_type + assert p("text/html") == ("text", "html", {}) + assert p("text") == None + + v = p("text/html; charset=UTF-8") + assert v == ('text', 'html', {'charset': 'UTF-8'}) + + +def test_cleanBin(): + assert utils.cleanBin("one") == "one" + assert utils.cleanBin("\00ne") == ".ne" + assert utils.cleanBin("\nne") == "\nne" + assert utils.cleanBin("\nne", True) == ".ne" + + +def test_safe_subn(): + assert utils.safe_subn("foo", u"bar", "\xc2foo") + + +def test_urlencode(): + assert utils.urlencode([('foo','bar')]) - assert len(f._cached_one) == 2 - assert len(f._cachelist_one) == 2 - - -class u_parse_proxy_spec(libpry.AutoTree): - def test_simple(self): - assert not utils.parse_proxy_spec("") - assert utils.parse_proxy_spec("http://foo.com:88") == ("http", "foo.com", 88) - assert utils.parse_proxy_spec("http://foo.com") == ("http", "foo.com", 80) - assert not utils.parse_proxy_spec("foo.com") - assert not utils.parse_proxy_spec("http://") - - -class u_unparse_url(libpry.AutoTree): - def test_simple(self): - assert utils.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99" - assert utils.unparse_url("http", "foo.com", 80, "") == "http://foo.com" - assert utils.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80" - assert utils.unparse_url("https", "foo.com", 443, "") == "https://foo.com" - - -class u_parse_url(libpry.AutoTree): - def test_simple(self): - assert not utils.parse_url("") - - u = "http://foo.com:8888/test" - s, h, po, pa = utils.parse_url(u) - assert s == "http" - assert h == "foo.com" - assert po == 8888 - assert pa == "/test" - - s, h, po, pa = utils.parse_url("http://foo/bar") - assert s == "http" - assert h == "foo" - assert po == 80 - assert pa == "/bar" - - s, h, po, pa = utils.parse_url("http://foo") - assert pa == "/" - - s, h, po, pa = utils.parse_url("https://foo") - assert po == 443 - - assert not utils.parse_url("https://foo:bar") - assert not utils.parse_url("https://foo:") - - -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 - assert utils.parse_size("1g") == 1024**3 - libpry.raises(ValueError, utils.parse_size, "1f") - libpry.raises(ValueError, utils.parse_size, "ak") - - -class u_parse_content_type(libpry.AutoTree): - def test_simple(self): - p = utils.parse_content_type - assert p("text/html") == ("text", "html", {}) - assert p("text") == None - - v = p("text/html; charset=UTF-8") - assert v == ('text', 'html', {'charset': 'UTF-8'}) - - -class u_cleanBin(libpry.AutoTree): - def test_simple(self): - assert utils.cleanBin("one") == "one" - assert utils.cleanBin("\00ne") == ".ne" - assert utils.cleanBin("\nne") == "\nne" - assert utils.cleanBin("\nne", True) == ".ne" - - -class u_safe_subn(libpry.AutoTree): - def test_simple(self): - assert utils.safe_subn("foo", u"bar", "\xc2foo") - - -tests = [ - u_safe_subn(), - u_cleanBin(), - u_parse_content_type(), - uformat_timestamp(), - uisBin(), - uisXML(), - uhexdump(), - upretty_size(), - uData(), - upretty_json(), - u_urldecode(), - udel_all(), - uLRUCache(), - u_parse_url(), - u_parse_proxy_spec(), - u_unparse_url(), - u_parse_size(), - uclean_hanging_newline() -] diff --git a/test/tutils.py b/test/tutils.py index 8ebb1ecd..c46221a9 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -133,3 +133,42 @@ class ProxTest(libpry.AutoTree): pthread = self.findAttr("proxy") return pthread.tmaster.log + + +def raises(exc, obj, *args, **kwargs): + """ + Assert that a callable raises a specified exception. + + :exc An exception class or a string. If a class, assert that an + exception of this type is raised. If a string, assert that the string + occurs in the string representation of the exception, based on a + case-insenstivie match. + + :obj A callable object. + + :args Arguments to be passsed to the callable. + + :kwargs Arguments to be passed to the callable. + """ + try: + apply(obj, args, kwargs) + except Exception, v: + if isinstance(exc, basestring): + if exc.lower() in str(v).lower(): + return + else: + raise AssertionError( + "Expected %s, but caught %s"%( + repr(str(exc)), v + ) + ) + else: + if isinstance(v, exc): + return + else: + raise AssertionError( + "Expected %s, but caught %s %s"%( + exc.__name__, v.__class__.__name__, str(v) + ) + ) + raise AssertionError("No exception raised.") @@ -14,9 +14,6 @@ Targeted for 0.9: Future: - - Rewrite the core to be asynchronous. I've done some research, and - although it's a bit of a bloated monster, it looks like Twisted is the way - to go. - Add some "workspace" features to mitmproxy: - Flow comments - Copying/duplicating flows |