2012-03-24 01:02:41 +00:00
|
|
|
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
|
2012-03-24 21:56:45 +00:00
|
|
|
|
2012-03-24 01:02:41 +00:00
|
|
|
def test_get_view_func(self):
|
|
|
|
f = cv.get_view_func(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_HEX,
|
2012-03-24 01:02:41 +00:00
|
|
|
flow.ODictCaseless(),
|
|
|
|
"foo"
|
|
|
|
)
|
|
|
|
assert f is cv.view_hex
|
|
|
|
|
|
|
|
f = cv.get_view_func(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
flow.ODictCaseless(),
|
|
|
|
"foo"
|
|
|
|
)
|
|
|
|
assert f is cv.view_raw
|
|
|
|
|
|
|
|
f = cv.get_view_func(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
flow.ODictCaseless(
|
|
|
|
[["content-type", "text/html"]],
|
|
|
|
),
|
|
|
|
"foo"
|
|
|
|
)
|
|
|
|
assert f is cv.view_xmlish
|
|
|
|
|
|
|
|
f = cv.get_view_func(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
flow.ODictCaseless(
|
|
|
|
[["content-type", "text/flibble"]],
|
|
|
|
),
|
|
|
|
"foo"
|
|
|
|
)
|
|
|
|
assert f is cv.view_raw
|
|
|
|
|
|
|
|
f = cv.get_view_func(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
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")
|
|
|
|
|
2012-03-24 21:56:45 +00:00
|
|
|
def test_view_javascript(self):
|
|
|
|
assert cv.view_javascript([], "[1, 2, 3]")
|
|
|
|
assert cv.view_javascript([], "[1, 2, 3")
|
|
|
|
assert cv.view_javascript([], "function(a){[1, 2, 3]}")
|
|
|
|
|
2012-03-24 01:02:41 +00:00
|
|
|
def test_view_raw(self):
|
|
|
|
assert cv.view_hex([], "foo")
|
|
|
|
|
2012-03-25 22:26:02 +00:00
|
|
|
def test_view_image(self):
|
|
|
|
assert cv.view_image([], file("data/image.png").read())
|
|
|
|
assert cv.view_image([], file("data/image.gif").read())
|
|
|
|
assert cv.view_image([], file("data/image-err1.jpg").read())
|
|
|
|
assert cv.view_image([], file("data/image.ico").read())
|
|
|
|
assert not cv.view_image([], "flibble")
|
|
|
|
|
2012-03-24 21:10:48 +00:00
|
|
|
def test_view_multipart(self):
|
|
|
|
v = """
|
|
|
|
--AaB03x
|
|
|
|
Content-Disposition: form-data; name="submit-name"
|
|
|
|
|
|
|
|
Larry
|
|
|
|
--AaB03x
|
|
|
|
""".strip()
|
|
|
|
h = flow.ODictCaseless(
|
|
|
|
[("Content-Type", "multipart/form-data; boundary=AaB03x")]
|
|
|
|
)
|
|
|
|
assert cv.view_multipart(h, v)
|
|
|
|
|
|
|
|
h = flow.ODictCaseless()
|
|
|
|
assert not cv.view_multipart(h, v)
|
|
|
|
|
|
|
|
h = flow.ODictCaseless(
|
|
|
|
[("Content-Type", "multipart/form-data")]
|
|
|
|
)
|
|
|
|
assert not cv.view_multipart(h, v)
|
|
|
|
|
|
|
|
h = flow.ODictCaseless(
|
|
|
|
[("Content-Type", "unparseable")]
|
|
|
|
)
|
|
|
|
assert not cv.view_multipart(h, v)
|
|
|
|
|
2012-03-24 01:02:41 +00:00
|
|
|
def test_get_content_view(self):
|
|
|
|
r = cv.get_content_view(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_RAW,
|
2012-03-24 01:02:41 +00:00
|
|
|
[["content-type", "application/json"]],
|
|
|
|
"[1, 2, 3]"
|
|
|
|
)
|
2012-04-01 22:30:35 +00:00
|
|
|
assert "Raw" in r[0]
|
2012-03-24 01:02:41 +00:00
|
|
|
|
|
|
|
r = cv.get_content_view(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
[["content-type", "application/json"]],
|
|
|
|
"[1, 2, 3]"
|
|
|
|
)
|
|
|
|
assert r[0] == "JSON"
|
|
|
|
|
|
|
|
r = cv.get_content_view(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
[["content-type", "application/json"]],
|
|
|
|
"[1, 2"
|
|
|
|
)
|
2012-04-02 01:24:51 +00:00
|
|
|
assert "Raw" in r[0]
|
2012-03-24 01:02:41 +00:00
|
|
|
|
|
|
|
r = cv.get_content_view(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_AUTO,
|
2012-03-24 01:02:41 +00:00
|
|
|
[
|
|
|
|
["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(
|
2012-04-01 22:30:35 +00:00
|
|
|
cv.VIEW_XML,
|
2012-03-24 01:02:41 +00:00
|
|
|
[
|
|
|
|
["content-type", "application/json"],
|
|
|
|
["content-encoding", "gzip"]
|
|
|
|
],
|
|
|
|
encoding.encode('gzip', "[1, 2, 3]")
|
|
|
|
)
|
|
|
|
assert "decoded gzip" in r[0]
|
2012-04-02 01:24:51 +00:00
|
|
|
assert "XML" in r[0]
|
2012-03-24 01:02:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests = [
|
|
|
|
uContentView()
|
|
|
|
]
|