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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
from wptserve.utils import isomorphic_encode
def main(request, response):
"""Handler that causes multiple redirections. Redirect chain is as follows:
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.
"""
step = 1
if "step" in request.GET:
if b"step" in request.GET:
try:
step = int(request.GET.first("step"))
step = int(request.GET.first(b"step"))
except ValueError:
pass
origin = request.url_parts.scheme + "://" + request.url_parts.hostname + ":" + str(request.url_parts.port)
page_origin = request.GET.first("page_origin")
cross_origin = request.GET.first("cross_origin")
final_resource = request.GET.first("final_resource")
page_origin = request.GET.first(b"page_origin")
cross_origin = request.GET.first(b"cross_origin")
final_resource = request.GET.first(b"final_resource")
tao_value = "*";
if "tao_value" in request.GET:
tao_value = request.GET.first("tao_value")
tao_value = b"*"
if b"tao_value" in request.GET:
tao_value = request.GET.first(b"tao_value")
tao_steps = 0
if "tao_steps" in request.GET:
tao_steps = int(request.GET.first("tao_steps"))
if b"tao_steps" in request.GET:
tao_steps = int(request.GET.first(b"tao_steps"))
next_tao_steps = tao_steps - 1
redirect_url_path = "/resource-timing/resources/multi_redirect.py?"
redirect_url_path += "page_origin=" + page_origin
redirect_url_path += "&cross_origin=" + cross_origin
redirect_url_path += "&final_resource=" + final_resource
redirect_url_path += "&tao_value=" + tao_value
redirect_url_path += "&tao_steps=" + str(next_tao_steps)
redirect_url_path += "&step="
redirect_url_path = b"/resource-timing/resources/multi_redirect.py?"
redirect_url_path += b"page_origin=" + page_origin
redirect_url_path += b"&cross_origin=" + cross_origin
redirect_url_path += b"&final_resource=" + final_resource
redirect_url_path += b"&tao_value=" + tao_value
redirect_url_path += b"&tao_steps=" + isomorphic_encode(str(next_tao_steps))
redirect_url_path += b"&step="
if tao_steps > 0:
response.headers.set("timing-allow-origin", tao_value)
response.headers.set(b"timing-allow-origin", tao_value)
if step == 1:
# 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:
# 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:
# On the third request, redirect to a static response
redirect_url = page_origin + final_resource
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):
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Max-Age", "0");
response.headers.set("Timing-Allow-Origin", "*");
response.headers.set(b"Access-Control-Allow-Origin", b"*");
response.headers.set(b"Access-Control-Max-Age", b"0");
response.headers.set(b"Timing-Allow-Origin", b"*");
# If this script is accessed with the header X-Require-Preflight then the
# browser will send a preflight request. Otherwise it won't.
if request.method == 'OPTIONS':
response.headers.set("Access-Control-Allow-Headers", "X-Require-Preflight");
if request.method == u'OPTIONS':
response.headers.set(b"Access-Control-Allow-Headers", b"X-Require-Preflight");

View File

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