"""Helpers for inlining extracts of documents in tests.""" from urllib.parse import urlencode BOILERPLATES = { "html": "\n\n{src}", "xhtml": """ XHTML might be the future {src} """, "xml": """\n{src}""", } MIME_TYPES = { "html": "text/html", "xhtml": "application/xhtml+xml", "xml": "text/xml", } def build_inline(build_url, src, doctype="html", mime=None, charset=None, **kwargs): if mime is None: mime = MIME_TYPES[doctype] if charset is None: charset = "UTF-8" doc = BOILERPLATES[doctype].format(charset=charset, src=src) query = {"doc": doc, "mime": mime, "charset": charset} return build_url( "/webdriver/tests/support/inline.py", query=urlencode(query), **kwargs) def main(request, response): doc = request.GET.first(b"doc", None) mime = request.GET.first(b"mime", None) charset = request.GET.first(b"charset", None) if doc is None: return 404, [(b"Content-Type", b"text/plain")], b"Missing doc parameter in query" content_type = [] if mime is not None: content_type.append(mime) if charset is not None: content_type.append(b"charset=%s" % charset) headers = {b"X-XSS-Protection": b"0"} if len(content_type) > 0: headers[b"Content-Type"] = b";".join(content_type) return 200, headers.items(), doc