Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 6fdb04a

Browse files
andreasttjgraham
authored andcommitted
webdriver: refactor document inlining helper; #webdriver
The inline() function has a bug where the mime keyword argument is ignored: if the doctype is recognised, the media type is overridden with the default for that document type. This patch's primary motivation is to fix that bug, so that the media type may be configured individually from the document type. The result of this ended in a larger refactoring of the inlining helper so that the character set may also be individually configured. The result of the combined changes is hopefully that the code is easier to understand, along with its new documentation. bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1570302 gecko-commit: e595a5b014135c515721b20d39176a70d1a09839 gecko-integration-branch: mozilla-inbound
1 parent 747ffaf commit 6fdb04a

File tree

1 file changed

+69
-34
lines changed

1 file changed

+69
-34
lines changed

webdriver/tests/support/inline.py

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,89 @@
1-
import urllib
1+
"""Helpers for inlining extracts of documents in tests."""
22

3+
import urllib
34

4-
def inline(doc,
5-
doctype="html",
6-
mime="text/html;charset=utf-8",
7-
**kwargs):
8-
from .fixtures import server_config, url
9-
build_url = url(server_config())
105

11-
if doctype == "html":
12-
mime = "text/html;charset=utf-8"
13-
elif doctype == "xhtml":
14-
mime = "application/xhtml+xml"
15-
doc = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
6+
BOILERPLATES = {
7+
"html": "<!doctype html>\n<meta charset={charset}>\n{src}",
8+
"xhtml": """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
169
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1710
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1811
<head>
1912
<title>XHTML might be the future</title>
2013
</head>
2114
2215
<body>
23-
{}
16+
{src}
2417
</body>
25-
</html>""".format(doc)
26-
elif doctype == "xml":
27-
mime = "text/xml"
28-
doc = """<?xml version="1.0" encoding="UTF-8"?>{}""".format(doc)
18+
</html>""",
19+
"xml": """<?xml version="1.0" encoding="{charset}"?>\n{src}""",
20+
}
21+
MIME_TYPES = {
22+
"html": "text/html",
23+
"xhtml": "application/xhtml+xml",
24+
"xml": "text/xml",
25+
}
2926

30-
query = {"doc": doc}
31-
if mime != "text/html;charset=utf8":
32-
query["content-type"] = mime
27+
def inline(src, doctype="html", mime=None, charset=None, **kwargs):
28+
"""
29+
Takes a source extract and produces well-formed documents.
3330
34-
return build_url("/webdriver/tests/support/inline.py",
35-
query=urllib.urlencode(query),
36-
**kwargs)
31+
Based on the desired document type, the extract is embedded with
32+
predefined boilerplate in order to produce well-formed documents.
33+
The media type and character set may also be individually configured.
3734
35+
This helper function originally used data URLs, but since these
36+
are not universally supported (or indeed standardised!) across
37+
browsers, it now delegates the serving of the document to wptserve.
38+
This file also acts as a wptserve handler (see the main function
39+
below) which configures the HTTP response using query parameters.
40+
41+
This function returns a URL to the wptserve handler, which in turn
42+
will serve an HTTP response with the requested source extract
43+
inlined in a well-formed document, and the Content-Type header
44+
optionally configured using the desired media type and character set.
45+
46+
Any additional keyword arguments are passed on to the build_url
47+
function.
48+
"""
49+
from .fixtures import server_config, url
50+
build_url = url(server_config())
3851

39-
def iframe(doc, **kwargs):
40-
return "<iframe src='%s'></iframe>" % inline(doc, **kwargs)
52+
if mime is None:
53+
mime = MIME_TYPES[doctype]
54+
if charset is None:
55+
charset = "UTF-8"
56+
doc = BOILERPLATES[doctype].format(charset=charset, src=src)
57+
58+
query = {"doc": doc, "mime": mime, "charset": charset}
59+
return build_url(
60+
"/webdriver/tests/support/inline.py",
61+
query=urllib.urlencode(query),
62+
**kwargs)
63+
64+
65+
def iframe(src, **kwargs):
66+
"""Inlines document extract as the source document of an <iframe>."""
67+
return "<iframe src='{}'></iframe>".format(inline(src, **kwargs))
4168

4269

4370
def main(request, response):
4471
doc = request.GET.first("doc", None)
45-
content_type = request.GET.first("content-type", "text/html;charset=utf8")
72+
mime = request.GET.first("mime", None)
73+
charset = request.GET.first("charset", None)
74+
4675
if doc is None:
47-
rv = 404, [("Content-Type", "text/plain")], "Missing doc parameter in query"
48-
else:
49-
response.headers.update([
50-
("Content-Type", content_type),
51-
("X-XSS-Protection", "0")
52-
])
53-
rv = doc
54-
return rv
76+
return 404, [("Content-Type",
77+
"text/plain")], "Missing doc parameter in query"
78+
79+
content_type = []
80+
if mime is not None:
81+
content_type.append(mime)
82+
if charset is not None:
83+
content_type.append("charset={}".format(charset))
84+
85+
headers = {"X-XSS-Protection": "0"}
86+
if len(content_type) > 0:
87+
headers["Content-Type"] = ";".join(content_type)
88+
89+
return 200, headers.items(), doc

0 commit comments

Comments
 (0)