Skip to content

Commit

Permalink
Python 3: port tests in clear-site-data etc. (#24081)
Browse files Browse the repository at this point in the history
It includes porting tests for the following directories:
  clear-site-data
  signed-exchange
  web-animation
  scroll-to-text-fragment
  preload
  • Loading branch information
ziransun committed Jun 11, 2020
1 parent 59b44d7 commit bfa5d6b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 42 deletions.
4 changes: 2 additions & 2 deletions clear-site-data/support/controlled-endpoint.py
@@ -1,3 +1,3 @@
def main(request, response):
return ([("Content-Type", "text/html")],
"FROM_NETWORK")
return ([(b"Content-Type", b"text/html")],
u"FROM_NETWORK")
8 changes: 4 additions & 4 deletions clear-site-data/support/echo-clear-site-data.py
@@ -1,6 +1,6 @@
import json

RESPONSE = """
RESPONSE = u"""
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -34,7 +34,7 @@
# embedder whether the data deletion succeeded.
def main(request, response):
types = [key for key in request.GET.keys()]
header = ",".join("\"" + type + "\"" for type in types)
return ([("Clear-Site-Data", header),
("Content-Type", "text/html")],
header = b",".join(b"\"" + type + b"\"" for type in types)
return ([(b"Clear-Site-Data", header),
(b"Content-Type", b"text/html")],
RESPONSE)
10 changes: 5 additions & 5 deletions preload/resources/cross-origin-module.py
@@ -1,9 +1,9 @@
def main(request, response):
headers = [
("Content-Type", "text/javascript"),
("Access-Control-Allow-Origin", request.headers.get("Origin")),
("Timing-Allow-Origin", request.headers.get("Origin")),
("Access-Control-Allow-Credentials", "true")
(b"Content-Type", b"text/javascript"),
(b"Access-Control-Allow-Origin", request.headers.get(b"Origin")),
(b"Timing-Allow-Origin", request.headers.get(b"Origin")),
(b"Access-Control-Allow-Credentials", b"true")
]

return headers, "// Cross-origin module, nothing to see here"
return headers, u"// Cross-origin module, nothing to see here"
10 changes: 5 additions & 5 deletions scroll-to-text-fragment/stash.py
@@ -1,13 +1,13 @@
import time

def main(request, response):
key = request.GET.first("key")
key = request.GET.first(b"key")

if request.method == "POST":
if request.method == u"POST":
# Received result data from target page
request.server.stash.put(key, request.body, '/scroll-to-text-fragment/')
return "ok"
request.server.stash.put(key, request.body, u'/scroll-to-text-fragment/')
return u"ok"
else:
# Request for result data from test page
value = request.server.stash.take(key, '/scroll-to-text-fragment/')
value = request.server.stash.take(key, u'/scroll-to-text-fragment/')
return value
14 changes: 8 additions & 6 deletions signed-exchange/resources/check-cert-request.py
@@ -1,11 +1,13 @@
import os

from wptserve.utils import isomorphic_decode

def main(request, response):
CertChainMimeType = "application/cert-chain+cbor"
CertChainMimeType = b"application/cert-chain+cbor"

if request.headers.get("Accept") != CertChainMimeType:
return 400, [], "Bad Request"
if request.headers.get(b"Accept") != CertChainMimeType:
return 400, [], u"Bad Request"

path = os.path.join(os.path.dirname(__file__), "127.0.0.1.sxg.pem.cbor")
body = open(path, "rb").read()
return 200, [("Content-Type", CertChainMimeType)], body
path = os.path.join(os.path.dirname(isomorphic_decode(__file__)), u"127.0.0.1.sxg.pem.cbor")
body = open(path, u"rb").read()
return 200, [(b"Content-Type", CertChainMimeType)], body
17 changes: 9 additions & 8 deletions signed-exchange/resources/prefetch-test-cert.py
@@ -1,17 +1,18 @@
import os

from wptserve.utils import isomorphic_decode

def main(request, response):
stash_id = request.GET.first("id")
stash_id = request.GET.first(b"id")
if request.server.stash.take(stash_id) is not None:
response.status = (404, "Not Found")
response.headers.set("Content-Type", "text/plain")
return "not found"
response.status = (404, u"Not Found")
response.headers.set(b"Content-Type", b"text/plain")
return u"not found"
request.server.stash.put(stash_id, True)

path = os.path.join(os.path.dirname(__file__), "127.0.0.1.sxg.pem.cbor")
body = open(path, "rb").read()
path = os.path.join(os.path.dirname(isomorphic_decode(__file__)), u"127.0.0.1.sxg.pem.cbor")
body = open(path, u"rb").read()

response.headers.set("Content-Type", "application/cert-chain+cbor")
response.headers.set("Cache-Control", "public, max-age=600")
response.headers.set(b"Content-Type", b"application/cert-chain+cbor")
response.headers.set(b"Cache-Control", b"public, max-age=600")
return body
21 changes: 11 additions & 10 deletions signed-exchange/resources/prefetch-test-sxg.py
@@ -1,19 +1,20 @@
import os

from wptserve.utils import isomorphic_decode

def main(request, response):
stash_id = request.GET.first("id")
stash_id = request.GET.first(b"id")
if request.server.stash.take(stash_id) is not None:
response.status = (404, "Not Found")
response.headers.set("Content-Type", "text/plain")
return "not found"
response.status = (404, u"Not Found")
response.headers.set(b"Content-Type", b"text/plain")
return u"not found"
request.server.stash.put(stash_id, True)

path = os.path.join(os.path.dirname(__file__), "sxg", "sxg-prefetch-test.sxg")
body = open(path, "rb").read()
path = os.path.join(os.path.dirname(isomorphic_decode(__file__)), u"sxg", u"sxg-prefetch-test.sxg")
body = open(path, u"rb").read()

response.headers.set("Content-Type", "application/signed-exchange;v=b3")
response.headers.set("X-Content-Type-Options", "nosniff")
response.headers.set("Cache-Control", "public, max-age=600")
response.headers.set(b"Content-Type", b"application/signed-exchange;v=b3")
response.headers.set(b"X-Content-Type-Options", b"nosniff")
response.headers.set(b"Cache-Control", b"public, max-age=600")

return body.replace('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', stash_id)
return body.replace(b'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', stash_id)
4 changes: 2 additions & 2 deletions web-animations/resources/xhr-doc.py
@@ -1,5 +1,5 @@
def main(request, response):
headers = [("Content-type", "text/html;charset=utf-8")]
content = "<!doctype html><div id=test></div>"
headers = [(b"Content-type", b"text/html;charset=utf-8")]
content = u"<!doctype html><div id=test></div>"

return headers, content

0 comments on commit bfa5d6b

Please sign in to comment.