diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-03-24 14:02:41 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-03-24 14:20:24 +1300 |
commit | 62e51018d0b7041d49e42b8e7d9b602ece356456 (patch) | |
tree | 59d798308ed40a20ac9489c35514a4298d61406f /test | |
parent | 0d05068f911adf619522b67c49c7a1fe24ecf70c (diff) | |
download | mitmproxy-62e51018d0b7041d49e42b8e7d9b602ece356456.tar.gz mitmproxy-62e51018d0b7041d49e42b8e7d9b602ece356456.tar.bz2 mitmproxy-62e51018d0b7041d49e42b8e7d9b602ece356456.zip |
Refactor pretty view mechanism.
Also start adding unit tests for this subsystem.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_console_contentview.py | 135 | ||||
-rw-r--r-- | test/test_flow.py | 5 |
2 files changed, 140 insertions, 0 deletions
diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py new file mode 100644 index 00000000..070094ff --- /dev/null +++ b/test/test_console_contentview.py @@ -0,0 +1,135 @@ +import libpry +import libmproxy.console.contentview as cv +from libmproxy import utils, flow, encoding + +class uContentView(libpry.AutoTree): + def test_trailer(self): + txt = [] + cv.trailer(5, txt) + assert not txt + cv.trailer(cv.VIEW_CUTOFF + 10, txt) + assert txt + + def test_get_view_func(self): + f = cv.get_view_func( + cv.VIEW_CONTENT_HEX, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + flow.ODictCaseless(), + "foo" + ) + assert f is cv.view_hex + + f = cv.get_view_func( + cv.VIEW_CONTENT_RAW, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + flow.ODictCaseless(), + "foo" + ) + assert f is cv.view_raw + + f = cv.get_view_func( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + flow.ODictCaseless( + [["content-type", "text/html"]], + ), + "foo" + ) + assert f is cv.view_xmlish + + f = cv.get_view_func( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + flow.ODictCaseless( + [["content-type", "text/flibble"]], + ), + "foo" + ) + assert f is cv.view_raw + + f = cv.get_view_func( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + flow.ODictCaseless( + [["content-type", "text/flibble"]], + ), + "<xml></xml>" + ) + assert f is cv.view_xmlish + + def test_view_urlencoded(self): + d = utils.urlencode([("one", "two"), ("three", "four")]) + assert cv.view_urlencoded([], d) + assert not cv.view_urlencoded([], "foo") + + def test_view_json(self): + cv.VIEW_CUTOFF = 100 + assert cv.view_json([], "{}") + assert not cv.view_urlencoded([], "{") + assert cv.view_json([], "[" + ",".join(["0"]*cv.VIEW_CUTOFF) + "]") + + def test_view_xmlish(self): + assert cv.view_xmlish([], "<foo></foo>") + assert cv.view_xmlish([], "<foo>") + + def test_view_raw(self): + assert cv.view_raw([], "foo") + + def test_view_raw(self): + assert cv.view_hex([], "foo") + + def test_get_content_view(self): + r = cv.get_content_view( + cv.VIEW_CONTENT_RAW, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + [["content-type", "application/json"]], + "[1, 2, 3]" + ) + assert r[0] == "Raw" + + r = cv.get_content_view( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + [["content-type", "application/json"]], + "[1, 2, 3]" + ) + assert r[0] == "JSON" + + + r = cv.get_content_view( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + [["content-type", "application/json"]], + "[1, 2" + ) + assert r[0] == "Raw" + + r = cv.get_content_view( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_AUTO, + [ + ["content-type", "application/json"], + ["content-encoding", "gzip"] + ], + encoding.encode('gzip', "[1, 2, 3]") + ) + assert "decoded gzip" in r[0] + assert "JSON" in r[0] + + + r = cv.get_content_view( + cv.VIEW_CONTENT_PRETTY, + cv.VIEW_CONTENT_PRETTY_TYPE_XML, + [ + ["content-type", "application/json"], + ["content-encoding", "gzip"] + ], + encoding.encode('gzip', "[1, 2, 3]") + ) + assert "decoded gzip" in r[0] + assert "forced" in r[0] + + +tests = [ + uContentView() +] diff --git a/test/test_flow.py b/test/test_flow.py index 8a7da05c..e44e2b0d 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -1001,6 +1001,11 @@ class uODict(libpry.AutoTree): ["two", "vun"], ] + def test_get(self): + self.od.add("one", "two") + assert self.od.get("one") == ["two"] + assert self.od.get("two") == None + class uODictCaseless(libpry.AutoTree): def setUp(self): |