gecko-dev/testing/web-platform/tests/mixed-content/generic/expect.py
Hiroshige Hayashizaki 46ee10e74a Bug 1526275 [wpt PR 15262] - [WPT] Merge wpt/{referrer-policy,mixed-content}/generic/common.js, a=testonly
Automatic update from web-platform-tests
[WPT] Merge wpt/{referrer-policy,mixed-content}/generic/common.js

To merge wpt/{referrer-policy,mixed-content} test frameworks,
this CL merges their common.js.

The new common.js is based on mixed-content's common.js,
with some aspects imported from referrer-policy's common.js:
- Passes results from subresource payloads to resolved promises,
  converting if necessary using wrapResult().
  This is for referrer-policy tests that rely on subresource
  payload to get referrer request headers, while
  mixed-content tests don't use the payload information at all.
- Accepts `additionalAttributes` arguments (to be used to set
  referrer-policy-related attributes to elements).
- Extends bindEvents() to clean up event listeners on completion
  (which is done for some request types in
  referrer-policy's common.js).
- Imports queryImage() (with renaming to
  requestViaImageForReferrerPolicy) from referrer-policy's common.js
  (this should be merged with mixed-content version of
  image requests, but not now).

On mixed-content side:
- expect.py's response for script requests is modified
  because postMessage() is required by referrer-policy's common.js.

On referrer-policy side:
- Move referrer-policy-specific code from common.js
  to referrer-policy-test-case.js, including wrapResult().
- All tests (except for two [1][2]) are converted to promise-based,
  to handle errors correctly.
- Bugs in the remaining two tests [1][2] are fixed.

[1] referrer-policy/generic/iframe-inheritance.html
[2] referrer-policy/generic/sandboxed-iframe-with-opaque-origin.html

Then now
wpt/{referrer-policy,mixed-content}/generic/common.js
are the same.

They are duplicated (i.e. not moved/merged to a single file)
just to avoid mass modification of a large number of
generated files for each step of refactoring,
as these file names are hard-coded there.

Bug: 906850
Change-Id: I39f19d08d658c1a898fc453b621d82a2faaaaf6b
Reviewed-on: https://chromium-review.googlesource.com/c/1455745
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#633917}

--

wpt-commits: 767f361425c20ded79c35630f709fc7addf7f80a
wpt-pr: 15262
2019-03-16 12:13:16 +00:00

106 lines
4.4 KiB
Python

import json, os, urllib, urlparse
def redirect(url, response):
response.add_required_headers = False
response.writer.write_status(301)
response.writer.write_header("access-control-allow-origin", "*")
response.writer.write_header("location", url)
response.writer.end_headers()
response.writer.write("")
def create_redirect_url(request, swap_scheme = False):
parsed = urlparse.urlsplit(request.url)
destination_netloc = parsed.netloc
scheme = parsed.scheme
if swap_scheme:
scheme = "http" if parsed.scheme == "https" else "https"
hostname = parsed.netloc.split(':')[0]
port = request.server.config["ports"][scheme][0]
destination_netloc = ":".join([hostname, str(port)])
# Remove "redirection" from query to avoid redirect loops.
parsed_query = dict(urlparse.parse_qsl(parsed.query))
assert "redirection" in parsed_query
del parsed_query["redirection"]
destination_url = urlparse.urlunsplit(urlparse.SplitResult(
scheme = scheme,
netloc = destination_netloc,
path = parsed.path,
query = urllib.urlencode(parsed_query),
fragment = None))
return destination_url
def main(request, response):
if "redirection" in request.GET:
redirection = request.GET["redirection"]
if redirection == "no-redirect":
pass
elif redirection == "keep-scheme-redirect":
redirect(create_redirect_url(request, swap_scheme=False), response)
return
elif redirection == "swap-scheme-redirect":
redirect(create_redirect_url(request, swap_scheme=True), response)
return
else:
raise ValueError ("Invalid redirect type: %s" % redirection)
content_type = "text/plain"
response_data = ""
if "action" in request.GET:
action = request.GET["action"]
if "content_type" in request.GET:
content_type = request.GET["content_type"]
key = request.GET["key"]
stash = request.server.stash
path = request.GET.get("path", request.url.split('?'))[0]
if action == "put":
value = request.GET["value"]
stash.take(key=key, path=path)
stash.put(key=key, value=value, path=path)
response_data = json.dumps({"status": "success", "result": key})
elif action == "purge":
value = stash.take(key=key, path=path)
if content_type == "image/png":
response_data = open(os.path.join(request.doc_root,
"images",
"smiley.png"), "rb").read()
elif content_type == "audio/wav":
response_data = open(os.path.join(request.doc_root,
"webaudio", "resources", "sin_440Hz_-6dBFS_1s.wav"), "rb").read()
elif content_type == "video/ogg":
response_data = open(os.path.join(request.doc_root,
"media",
"movie_5.ogv"), "rb").read()
elif content_type == "application/javascript":
response_data = open(os.path.join(request.doc_root,
"mixed-content",
"generic",
"worker.js"), "rb").read()
elif content_type == "text/javascript":
response_data = open(os.path.join(request.doc_root,
"mixed-content",
"generic",
"script.js"), "rb").read()
else:
response_data = "/* purged */"
elif action == "take":
value = stash.take(key=key, path=path)
if value is None:
status = "allowed"
else:
status = "blocked"
response_data = json.dumps({"status": status, "result": value})
response.add_required_headers = False
response.writer.write_status(200)
response.writer.write_header("content-type", content_type)
response.writer.write_header("cache-control", "no-cache; must-revalidate")
response.writer.end_headers()
response.writer.write(response_data)