Bug 1640914 [wpt PR 23771] - Python 3: port tests in resource-timing, a=testonly

Automatic update from web-platform-tests
Python 3: port wptserve file handlers in resource-timing (#23771)

--

wpt-commits: 258a3a54cbc88e72d32490328557fc3bf3e19c44
wpt-pr: 23771
This commit is contained in:
ziransun 2020-06-10 11:35:45 +00:00 committed by moz-wptsync-bot
parent 8882be9cfc
commit eeb36cd745
11 changed files with 134 additions and 123 deletions

View File

@ -6,14 +6,14 @@ def main(request, response):
location - The resource to redirect to. location - The resource to redirect to.
""" """
status = 302 status = 302
if "status" in request.GET: if b"status" in request.GET:
try: try:
status = int(request.GET.first("status")) status = int(request.GET.first(b"status"))
except ValueError: except ValueError:
pass pass
response.status = status response.status = status
location = request.GET.first("location") location = request.GET.first(b"location")
response.headers.set("Location", location) response.headers.set(b"Location", location)

View File

@ -1,23 +1,28 @@
import urllib from six.moves.urllib.parse import unquote
from wptserve.utils import isomorphic_decode, isomorphic_encode
import sleep import sleep
def main(request, response): def main(request, response):
index = request.request_path.index("?") index = isomorphic_encode(request.request_path).index(b"?")
args = request.request_path[index+1:].split("&") args = isomorphic_encode(request.request_path[index+1:]).split(b"&")
headers = [] headers = []
statusSent = False statusSent = False
headersSent = False headersSent = False
for arg in args: for arg in args:
if arg.startswith("ignored"): if arg.startswith(b"ignored"):
continue continue
elif arg.endswith("ms"): elif arg.endswith(b"ms"):
sleep.sleep_at_least(float(arg[0:-2])) sleep.sleep_at_least(float(arg[0:-2]))
elif arg.startswith("redirect:"): elif arg.startswith(b"redirect:"):
return (302, "WEBPERF MARKETING"), [("Location", urllib.unquote(arg[9:]))], "TEST" return (302, u"WEBPERF MARKETING"), [(b"Location", unquote(isomorphic_decode(arg[9:])))], u"TEST"
elif arg.startswith("mime:"):
headers.append(("Content-Type", urllib.unquote(arg[5:]))) elif arg.startswith(b"mime:"):
elif arg.startswith("send:"): headers.append((b"Content-Type", unquote(isomorphic_decode(arg[5:]))))
text = urllib.unquote(arg[5:])
elif arg.startswith(b"send:"):
text = unquote(isomorphic_decode(arg[5:]))
if not statusSent: if not statusSent:
# Default to a 200 status code. # Default to a 200 status code.
@ -30,15 +35,15 @@ def main(request, response):
headersSent = True headersSent = True
response.writer.write_content(text) response.writer.write_content(text)
elif arg.startswith("status:"): elif arg.startswith(b"status:"):
code = int(urllib.unquote(arg[7:])) code = int(unquote(isomorphic_decode(arg[7:])))
response.writer.write_status(code) response.writer.write_status(code)
if code // 100 == 1: if code // 100 == 1:
# Terminate informational 1XX responses with an empty line. # Terminate informational 1XX responses with an empty line.
response.writer.end_headers() response.writer.end_headers()
else: else:
statusSent = True statusSent = True
elif arg == "flush": elif arg == b"flush":
response.writer.flush() response.writer.flush()
# else: # else:

View File

@ -1,51 +1,51 @@
def main(request, response): def main(request, response):
origin = request.headers['origin'] origin = request.headers[b'origin']
response.headers.set('Access-Control-Allow-Origin', origin) response.headers.set(b'Access-Control-Allow-Origin', origin)
tao = request.GET.first('tao') tao = request.GET.first(b'tao')
if tao == 'zero': if tao == b'zero':
# zero TAO value, fail # zero TAO value, fail
pass pass
elif tao == 'wildcard': elif tao == b'wildcard':
# wildcard, pass # wildcard, pass
response.headers.set('Timing-Allow-Origin', '*') response.headers.set(b'Timing-Allow-Origin', b'*')
elif tao == 'null': elif tao == b'null':
# null, fail unless it's an opaque origin # null, fail unless it's an opaque origin
response.headers.set('Timing-Allow-Origin', 'null') response.headers.set(b'Timing-Allow-Origin', b'null')
elif tao == 'Null': elif tao == b'Null':
# case-insentive null, fail # case-insentive null, fail
response.headers.set('Timing-Allow-Origin', 'Null') response.headers.set(b'Timing-Allow-Origin', b'Null')
elif tao == 'origin': elif tao == b'origin':
# case-sensitive match for origin, pass # case-sensitive match for origin, pass
response.headers.set('Timing-Allow-Origin', origin) response.headers.set(b'Timing-Allow-Origin', origin)
elif tao.startswith('origin_port'): elif tao.startswith(b'origin_port'):
# case-sensitive match for origin and port, pass # case-sensitive match for origin and port, pass
origin_parts = origin.split(':') origin_parts = origin.split(b':')
host = origin_parts[0] + ':' + origin_parts[1] host = origin_parts[0] + b':' + origin_parts[1]
port = tao.split('origin_port_')[1] port = tao.split(b'origin_port_')[1]
response.headers.set('Timing-Allow-Origin', host + ':' + port) response.headers.set(b'Timing-Allow-Origin', host + b':' + port)
elif tao == 'space': elif tao == b'space':
# space separated list of origin and wildcard, fail # space separated list of origin and wildcard, fail
response.headers.set('Timing-Allow-Origin', (origin + ' *')) response.headers.set(b'Timing-Allow-Origin', (origin + b' *'))
elif tao == 'multi': elif tao == b'multi':
# more than one TAO values, separated by comma, pass # more than one TAO values, separated by comma, pass
response.headers.set('Timing-Allow-Origin', origin) response.headers.set(b'Timing-Allow-Origin', origin)
response.headers.append('Timing-Allow-Origin', '*') response.headers.append(b'Timing-Allow-Origin', b'*')
elif tao == 'multi_wildcard': elif tao == b'multi_wildcard':
# multiple wildcards, separated by comma, pass # multiple wildcards, separated by comma, pass
response.headers.set('Timing-Allow-Origin', '*') response.headers.set(b'Timing-Allow-Origin', b'*')
response.headers.append('Timing-Allow-Origin', '*') response.headers.append(b'Timing-Allow-Origin', b'*')
elif tao == 'match_origin': elif tao == b'match_origin':
# contains a match of origin, separated by comma, pass # contains a match of origin, separated by comma, pass
response.headers.set('Timing-Allow-Origin', origin) response.headers.set(b'Timing-Allow-Origin', origin)
response.headers.append('Timing-Allow-Origin', "fake") response.headers.append(b'Timing-Allow-Origin', b"fake")
elif tao == 'match_wildcard': elif tao == b'match_wildcard':
# contains a wildcard, separated by comma, pass # contains a wildcard, separated by comma, pass
response.headers.set('Timing-Allow-Origin', "fake") response.headers.set(b'Timing-Allow-Origin', b"fake")
response.headers.append('Timing-Allow-Origin', '*') response.headers.append(b'Timing-Allow-Origin', b'*')
elif tao == 'uppercase': elif tao == b'uppercase':
# non-case-sensitive match for origin, fail # non-case-sensitive match for origin, fail
response.headers.set('Timing-Allow-Origin', origin.upper()) response.headers.set(b'Timing-Allow-Origin', origin.upper())
else: else:
pass pass

View File

@ -1,17 +1,19 @@
import os.path import os.path
def main(request, response): from wptserve.utils import isomorphic_decode
etag = "123abc"
if etag == request.headers.get("If-None-Match", None):
response.headers.set("X-HTTP-STATUS", 304)
response.status = (304, "Not Modified")
return ""
response.headers.set("Cache-Control", "public, max-age=86400") def main(request, response):
response.headers.set("Content-Type", "font/truetype") etag = b"123abc"
response.headers.set("Access-Control-Allow-Origin", "*") if etag == request.headers.get(b"If-None-Match", None):
response.headers.set("Timing-Allow-Origin", "*") response.headers.set(b"X-HTTP-STATUS", 304)
response.headers.set("ETag", etag) response.status = (304, u"Not Modified")
font = "../../fonts/Ahem.ttf" return u""
path = os.path.join(os.path.dirname(__file__), font)
response.content = open(path, "rb").read() response.headers.set(b"Cache-Control", b"public, max-age=86400")
response.headers.set(b"Content-Type", b"font/truetype")
response.headers.set(b"Access-Control-Allow-Origin", b"*")
response.headers.set(b"Timing-Allow-Origin", b"*")
response.headers.set(b"ETag", etag)
font = u"../../fonts/Ahem.ttf"
path = os.path.join(os.path.dirname(isomorphic_decode(__file__)), font)
response.content = open(path, u"rb").read()

View File

@ -1,3 +1,3 @@
def main(request, response): def main(request, response):
response.headers.set("Content-Type", "text/plain") response.headers.set(b"Content-Type", b"text/plain")
return "" return u""

View File

@ -1,3 +1,3 @@
def main(request, response): def main(request, response):
response.headers.set("Content-Type", "text/event-stream") response.headers.set(b"Content-Type", b"text/event-stream")
return "" return u""

View File

@ -1,26 +1,26 @@
# /xhr/resources/conditional.py -- to fake a 304 response # /xhr/resources/conditional.py -- to fake a 304 response
def main(request, response): def main(request, response):
tag = request.GET.first("tag", None) tag = request.GET.first(b"tag", None)
redirect = request.GET.first("redirect", None) redirect = request.GET.first(b"redirect", None)
match = request.headers.get("If-None-Match", None) match = request.headers.get(b"If-None-Match", None)
date = request.GET.first("date", "") date = request.GET.first(b"date", b"")
modified = request.headers.get("If-Modified-Since", None) modified = request.headers.get(b"If-Modified-Since", None)
response.headers.set("Access-Control-Allow-Origin", "*"); response.headers.set(b"Access-Control-Allow-Origin", b"*");
response.headers.set("Timing-Allow-Origin", "*"); response.headers.set(b"Timing-Allow-Origin", b"*");
if tag: if tag:
response.headers.set("ETag", '"%s"' % tag) response.headers.set(b"ETag", b'"%s"' % tag)
elif date: elif date:
response.headers.set("Last-Modified", date) response.headers.set(b"Last-Modified", date)
if redirect: if redirect:
response.headers.set("Location", redirect) response.headers.set(b"Location", redirect)
response.status = (302, "Moved") response.status = (302, u"Moved")
return "" return u""
if ((match is not None and match == tag) or if ((match is not None and match == tag) or
(modified is not None and modified == date)): (modified is not None and modified == date)):
response.status = (304, "SUPERCOOL") response.status = (304, u"SUPERCOOL")
return "" return u""
else: else:
response.headers.set("Content-Type", "text/plain") response.headers.set(b"Content-Type", b"text/plain")
return "MAYBE NOT" return u"MAYBE NOT"

View File

@ -1,20 +1,23 @@
import gzip as gzip_module import gzip as gzip_module
from cStringIO import StringIO
import os import os
from six import BytesIO
from wptserve.utils import isomorphic_decode
def main(request, response): def main(request, response):
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(isomorphic_decode(__file__)))
file_path = os.path.join(dir_path, 'resource_timing_test0.xml') file_path = os.path.join(dir_path, u'resource_timing_test0.xml')
f = open(file_path, 'r') f = open(file_path, u'rb')
output = f.read() output = f.read()
out = StringIO() out = BytesIO()
with gzip_module.GzipFile(fileobj=out, mode="w") as f: with gzip_module.GzipFile(fileobj=out, mode="w") as f:
f.write(output) f.write(output)
output = out.getvalue() output = out.getvalue()
headers = [("Content-type", "text/plain"), headers = [(b"Content-type", b"text/plain"),
("Content-Encoding", "gzip"), (b"Content-Encoding", b"gzip"),
("Content-Length", len(output))] (b"Content-Length", len(output))]
return headers, output return headers, output

View File

@ -1,3 +1,5 @@
from wptserve.utils import isomorphic_encode
def main(request, response): def main(request, response):
"""Handler that causes multiple redirections. Redirect chain is as follows: """Handler that causes multiple redirections. Redirect chain is as follows:
1. Initial URL containing multi-redirect.py 1. Initial URL containing multi-redirect.py
@ -14,44 +16,43 @@ def main(request, response):
Note that |step| is a parameter used internally for the multi-redirect. It's the step we're at in the redirect chain. Note that |step| is a parameter used internally for the multi-redirect. It's the step we're at in the redirect chain.
""" """
step = 1 step = 1
if "step" in request.GET: if b"step" in request.GET:
try: try:
step = int(request.GET.first("step")) step = int(request.GET.first(b"step"))
except ValueError: except ValueError:
pass pass
origin = request.url_parts.scheme + "://" + request.url_parts.hostname + ":" + str(request.url_parts.port) page_origin = request.GET.first(b"page_origin")
page_origin = request.GET.first("page_origin") cross_origin = request.GET.first(b"cross_origin")
cross_origin = request.GET.first("cross_origin") final_resource = request.GET.first(b"final_resource")
final_resource = request.GET.first("final_resource")
tao_value = "*"; tao_value = b"*"
if "tao_value" in request.GET: if b"tao_value" in request.GET:
tao_value = request.GET.first("tao_value") tao_value = request.GET.first(b"tao_value")
tao_steps = 0 tao_steps = 0
if "tao_steps" in request.GET: if b"tao_steps" in request.GET:
tao_steps = int(request.GET.first("tao_steps")) tao_steps = int(request.GET.first(b"tao_steps"))
next_tao_steps = tao_steps - 1 next_tao_steps = tao_steps - 1
redirect_url_path = "/resource-timing/resources/multi_redirect.py?" redirect_url_path = b"/resource-timing/resources/multi_redirect.py?"
redirect_url_path += "page_origin=" + page_origin redirect_url_path += b"page_origin=" + page_origin
redirect_url_path += "&cross_origin=" + cross_origin redirect_url_path += b"&cross_origin=" + cross_origin
redirect_url_path += "&final_resource=" + final_resource redirect_url_path += b"&final_resource=" + final_resource
redirect_url_path += "&tao_value=" + tao_value redirect_url_path += b"&tao_value=" + tao_value
redirect_url_path += "&tao_steps=" + str(next_tao_steps) redirect_url_path += b"&tao_steps=" + isomorphic_encode(str(next_tao_steps))
redirect_url_path += "&step=" redirect_url_path += b"&step="
if tao_steps > 0: if tao_steps > 0:
response.headers.set("timing-allow-origin", tao_value) response.headers.set(b"timing-allow-origin", tao_value)
if step == 1: if step == 1:
# On the first request, redirect to a cross origin URL # On the first request, redirect to a cross origin URL
redirect_url = cross_origin + redirect_url_path + "2" redirect_url = cross_origin + redirect_url_path + b"2"
elif step == 2: elif step == 2:
# On the second request, redirect to a same origin URL # On the second request, redirect to a same origin URL
redirect_url = page_origin + redirect_url_path + "3" redirect_url = page_origin + redirect_url_path + b"3"
else: else:
# On the third request, redirect to a static response # On the third request, redirect to a static response
redirect_url = page_origin + final_resource redirect_url = page_origin + final_resource
response.status = 302 response.status = 302
response.headers.set("Location", redirect_url) response.headers.set(b"Location", redirect_url)

View File

@ -1,8 +1,8 @@
def main(request, response): def main(request, response):
response.headers.set("Access-Control-Allow-Origin", "*"); response.headers.set(b"Access-Control-Allow-Origin", b"*");
response.headers.set("Access-Control-Max-Age", "0"); response.headers.set(b"Access-Control-Max-Age", b"0");
response.headers.set("Timing-Allow-Origin", "*"); response.headers.set(b"Timing-Allow-Origin", b"*");
# If this script is accessed with the header X-Require-Preflight then the # If this script is accessed with the header X-Require-Preflight then the
# browser will send a preflight request. Otherwise it won't. # browser will send a preflight request. Otherwise it won't.
if request.method == 'OPTIONS': if request.method == u'OPTIONS':
response.headers.set("Access-Control-Allow-Headers", "X-Require-Preflight"); response.headers.set(b"Access-Control-Allow-Headers", b"X-Require-Preflight");

View File

@ -1,4 +1,4 @@
def main(request, response): def main(request, response):
status = request.GET.first('status') status = request.GET.first(b'status')
response.status = (status, ""); response.status = (status, b"");