Huge cleanup of content viewers.

This commit is contained in:
Aldo Cortesi
2012-08-18 17:08:17 +12:00
parent 5c80450ce7
commit 11c63dcb9f
5 changed files with 362 additions and 343 deletions

View File

@@ -13,82 +13,91 @@ class TestContentView:
def test_get_view_func(self):
f = cv.get_view_func(
cv.VIEW_HEX,
cv.get("Hex"),
flow.ODictCaseless(),
"foo"
)
assert f is cv.view_hex
assert f.name == "Hex"
f = cv.get_view_func(
cv.VIEW_AUTO,
cv.get("Auto"),
flow.ODictCaseless(),
"foo"
)
assert f is cv.view_raw
assert f.name == "Raw"
f = cv.get_view_func(
cv.VIEW_AUTO,
cv.get("Auto"),
flow.ODictCaseless(
[["content-type", "text/html"]],
),
"foo"
)
assert f is cv.view_html
assert f.name == "HTML"
f = cv.get_view_func(
cv.VIEW_AUTO,
cv.get("Auto"),
flow.ODictCaseless(
[["content-type", "text/flibble"]],
),
"foo"
)
assert f is cv.view_raw
assert f.name == "Raw"
f = cv.get_view_func(
cv.VIEW_AUTO,
cv.get("Auto"),
flow.ODictCaseless(
[["content-type", "text/flibble"]],
),
"<xml></xml>"
)
assert f is cv.view_xml
assert f.name == "XML"
try:
import pyamf
f = cv.get_view_func(
cv.VIEW_AUTO,
cv.get("Auto"),
flow.ODictCaseless(
[["content-type", "application/x-amf"]],
),
""
)
assert f is cv.view_amf
assert f.name == "AMF"
except ImportError:
pass
def test_view_urlencoded(self):
d = utils.urlencode([("one", "two"), ("three", "four")])
assert cv.view_urlencoded([], d, 100)
assert not cv.view_urlencoded([], "foo", 100)
v = cv.ViewURLEncoded()
assert v([], d, 100)
assert not v([], "foo", 100)
def test_view_html(self):
v = cv.ViewHTML()
s = "<html><br><br></br><p>one</p></html>"
assert cv.view_html([], s, 1000)
assert v([], s, 1000)
s = "gobbledygook"
assert not cv.view_html([], s, 1000)
assert not v([], s, 1000)
def test_view_html_outline(self):
v = cv.ViewHTMLOutline()
s = "<html><br><br></br><p>one</p></html>"
assert v([], s, 1000)
def test_view_json(self):
cv.VIEW_CUTOFF = 100
assert cv.view_json([], "{}", 1000)
assert not cv.view_json([], "{", 1000)
assert cv.view_json([], "[" + ",".join(["0"]*cv.VIEW_CUTOFF) + "]", 1000)
assert cv.view_json([], "[1, 2, 3, 4, 5]", 5)
v = cv.ViewJSON()
assert v([], "{}", 1000)
assert not v([], "{", 1000)
assert v([], "[" + ",".join(["0"]*cv.VIEW_CUTOFF) + "]", 1000)
assert v([], "[1, 2, 3, 4, 5]", 5)
def test_view_xml(self):
assert cv.view_xml([], "<foo></foo>", 1000)
assert not cv.view_xml([], "<foo>", 1000)
v = cv.ViewXML()
assert v([], "<foo></foo>", 1000)
assert not v([], "<foo>", 1000)
s = """<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet title="XSL_formatting"?>
<rss
@@ -97,44 +106,49 @@ class TestContentView:
version="2.0">
</rss>
"""
assert cv.view_xml([], s, 1000)
assert v([], s, 1000)
def test_view_raw(self):
assert cv.view_raw([], "foo", 1000)
v = cv.ViewRaw()
assert v([], "foo", 1000)
def test_view_javascript(self):
assert cv.view_javascript([], "[1, 2, 3]", 100)
assert cv.view_javascript([], "[1, 2, 3", 100)
assert cv.view_javascript([], "function(a){[1, 2, 3]}", 100)
v = cv.ViewJavaScript()
assert v([], "[1, 2, 3]", 100)
assert v([], "[1, 2, 3", 100)
assert v([], "function(a){[1, 2, 3]}", 100)
def test_view_hex(self):
assert cv.view_hex([], "foo", 1000)
v = cv.ViewHex()
assert v([], "foo", 1000)
def test_view_image(self):
v = cv.ViewImage()
p = tutils.test_data.path("data/image.png")
assert cv.view_image([], file(p).read(), sys.maxint)
assert v([], file(p).read(), sys.maxint)
p = tutils.test_data.path("data/image.gif")
assert cv.view_image([], file(p).read(), sys.maxint)
assert v([], file(p).read(), sys.maxint)
p = tutils.test_data.path("data/image-err1.jpg")
assert cv.view_image([], file(p).read(), sys.maxint)
assert v([], file(p).read(), sys.maxint)
p = tutils.test_data.path("data/image.ico")
assert cv.view_image([], file(p).read(), sys.maxint)
assert v([], file(p).read(), sys.maxint)
assert not cv.view_image([], "flibble", sys.maxint)
assert not v([], "flibble", sys.maxint)
def test_view_amf(self):
try:
import pyamf
v = cv.ViewAMF()
p = tutils.test_data.path("data/test.amf")
assert cv.view_amf([], file(p).read(), sys.maxint)
assert v([], file(p).read(), sys.maxint)
except ImportError:
pass
def test_view_multipart(self):
view = cv.ViewMultipart()
v = """
--AaB03x
Content-Disposition: form-data; name="submit-name"
@@ -145,24 +159,24 @@ Larry
h = flow.ODictCaseless(
[("Content-Type", "multipart/form-data; boundary=AaB03x")]
)
assert cv.view_multipart(h, v, 1000)
assert view(h, v, 1000)
h = flow.ODictCaseless()
assert not cv.view_multipart(h, v, 1000)
assert not view(h, v, 1000)
h = flow.ODictCaseless(
[("Content-Type", "multipart/form-data")]
)
assert not cv.view_multipart(h, v, 1000)
assert not view(h, v, 1000)
h = flow.ODictCaseless(
[("Content-Type", "unparseable")]
)
assert not cv.view_multipart(h, v, 1000)
assert not view(h, v, 1000)
def test_get_content_view(self):
r = cv.get_content_view(
cv.VIEW_RAW,
cv.get("Raw"),
[["content-type", "application/json"]],
"[1, 2, 3]",
1000
@@ -170,7 +184,7 @@ Larry
assert "Raw" in r[0]
r = cv.get_content_view(
cv.VIEW_AUTO,
cv.get("Auto"),
[["content-type", "application/json"]],
"[1, 2, 3]",
1000
@@ -178,7 +192,7 @@ Larry
assert r[0] == "JSON"
r = cv.get_content_view(
cv.VIEW_AUTO,
cv.get("Auto"),
[["content-type", "application/json"]],
"[1, 2",
1000
@@ -186,7 +200,7 @@ Larry
assert "Raw" in r[0]
r = cv.get_content_view(
cv.VIEW_AUTO,
cv.get("Auto"),
[
["content-type", "application/json"],
["content-encoding", "gzip"]
@@ -198,7 +212,7 @@ Larry
assert "JSON" in r[0]
r = cv.get_content_view(
cv.VIEW_XML,
cv.get("XML"),
[
["content-type", "application/json"],
["content-encoding", "gzip"]