Bug 1646307 [wpt PR 24184] - Python3: port tests in eventsource, a=testonly
authorZiran Sun <zsun@igalia.com>
Mon, 22 Jun 2020 10:43:56 +0000
changeset 536842 a0aa8a4840605fbc403ca6c97469e82faf977170
parent 536841 8599abc6d8058dd072e9db65ca36957629f3f7da
child 536843 5a3f0059da33e1dd983c6ff0b2ee27a82b31001b
push id37533
push userdluca@mozilla.com
push dateTue, 23 Jun 2020 21:38:40 +0000
treeherdermozilla-central@d48aa0f0aa0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1646307, 24184
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1646307 [wpt PR 24184] - Python3: port tests in eventsource, a=testonly Automatic update from web-platform-tests Python3: port tests in eventsource -- Correct type of body -- Remove unused module call -- wpt-commits: 8ed59ad76d99061cefe6582f83f025bb0d932881, 1433b4ee9a30bad77397ef120b0c6cbc49ed1bd2, 4c57283bfb189d2a871420184a8f09c276225bd4 wpt-pr: 24184
testing/web-platform/tests/eventsource/resources/cors-cookie.py
testing/web-platform/tests/eventsource/resources/cors.py
testing/web-platform/tests/eventsource/resources/last-event-id.py
testing/web-platform/tests/eventsource/resources/message.py
testing/web-platform/tests/eventsource/resources/message2.py
testing/web-platform/tests/eventsource/resources/reconnect-fail.py
testing/web-platform/tests/eventsource/resources/status-error.py
testing/web-platform/tests/eventsource/resources/status-reconnect.py
--- a/testing/web-platform/tests/eventsource/resources/cors-cookie.py
+++ b/testing/web-platform/tests/eventsource/resources/cors-cookie.py
@@ -1,32 +1,31 @@
 from datetime import datetime
-from six import ensure_str
 
 def main(request, response):
-    last_event_id = ensure_str(request.headers.get("Last-Event-Id", ""))
-    ident = request.GET.first('ident', "test")
-    cookie = "COOKIE" if ident in request.cookies else "NO_COOKIE"
-    origin = request.GET.first('origin', request.headers["origin"])
-    credentials = request.GET.first('credentials', 'true')
+    last_event_id = request.headers.get(b"Last-Event-Id", b"")
+    ident = request.GET.first(b'ident', b"test")
+    cookie = b"COOKIE" if ident in request.cookies else b"NO_COOKIE"
+    origin = request.GET.first(b'origin', request.headers[b"origin"])
+    credentials = request.GET.first(b'credentials', b'true')
 
     headers = []
 
-    if origin != 'none':
-        headers.append(("Access-Control-Allow-Origin", origin));
+    if origin != b'none':
+        headers.append((b"Access-Control-Allow-Origin", origin));
 
-    if credentials != 'none':
-        headers.append(("Access-Control-Allow-Credentials", credentials));
+    if credentials != b'none':
+        headers.append((b"Access-Control-Allow-Credentials", credentials));
 
-    if last_event_id == '':
-        headers.append(("Content-Type", "text/event-stream"))
-        response.set_cookie(ident, "COOKIE")
-        data = "id: 1\nretry: 200\ndata: first %s\n\n" % cookie
-    elif last_event_id == '1':
-        headers.append(("Content-Type", "text/event-stream"))
+    if last_event_id == b'':
+        headers.append((b"Content-Type", b"text/event-stream"))
+        response.set_cookie(ident, b"COOKIE")
+        data = b"id: 1\nretry: 200\ndata: first %s\n\n" % cookie
+    elif last_event_id == b'1':
+        headers.append((b"Content-Type", b"text/event-stream"))
         long_long_time_ago = datetime.now().replace(year=2001, month=7, day=27)
-        response.set_cookie(ident, "COOKIE", expires=long_long_time_ago)
-        data = "id: 2\ndata: second %s\n\n" % cookie
+        response.set_cookie(ident, b"COOKIE", expires=long_long_time_ago)
+        data = b"id: 2\ndata: second %s\n\n" % cookie
     else:
-        headers.append(("Content-Type", "stop"))
-        data = "data: " + last_event_id + cookie + "\n\n";
+        headers.append((b"Content-Type", b"stop"))
+        data = b"data: " + last_event_id + cookie + b"\n\n";
 
     return headers, data
--- a/testing/web-platform/tests/eventsource/resources/cors.py
+++ b/testing/web-platform/tests/eventsource/resources/cors.py
@@ -1,34 +1,36 @@
 import os
 from wptserve import pipes
 
+from wptserve.utils import isomorphic_decode
+
 def run_other(request, response, path):
     #This is a terrible hack
-    environ = {"__file__": path}
-    exec(compile(open(path, "r").read(), path, 'exec'), environ, environ)
-    rv = environ["main"](request, response)
+    environ = {u"__file__": path}
+    exec(compile(open(path, u"r").read(), path, u'exec'), environ, environ)
+    rv = environ[u"main"](request, response)
     return rv
 
 def main(request, response):
-    origin = request.GET.first("origin", request.headers["origin"])
-    credentials = request.GET.first("credentials", "true")
+    origin = request.GET.first(b"origin", request.headers[b"origin"])
+    credentials = request.GET.first(b"credentials", b"true")
 
-    response.headers.update([("Access-Control-Allow-Origin", origin),
-                             ("Access-Control-Allow-Credentials", credentials)])
+    response.headers.update([(b"Access-Control-Allow-Origin", origin),
+                             (b"Access-Control-Allow-Credentials", credentials)])
 
-    handler = request.GET.first('run')
-    if handler in ["status-reconnect",
-                   "message",
-                   "redirect",
-                   "cache-control"]:
-        if handler == "cache-control":
-            response.headers.set("Content-Type", "text/event-stream")
-            rv = open(os.path.join(request.doc_root, "eventsource", "resources", "cache-control.event_stream"), "r").read()
+    handler = request.GET.first(b'run')
+    if handler in [b"status-reconnect",
+                   b"message",
+                   b"redirect",
+                   b"cache-control"]:
+        if handler == b"cache-control":
+            response.headers.set(b"Content-Type", b"text/event-stream")
+            rv = open(os.path.join(request.doc_root, u"eventsource", u"resources", u"cache-control.event_stream"), u"r").read()
             response.content = rv
             pipes.sub(request, response)
             return
-        elif handler == "redirect":
-            return run_other(request, response, os.path.join(request.doc_root, "common", "redirect.py"))
+        elif handler == b"redirect":
+            return run_other(request, response, os.path.join(request.doc_root, u"common", u"redirect.py"))
         else:
-            return run_other(request, response, os.path.join(os.path.split(__file__)[0], handler + ".py"))
+            return run_other(request, response, os.path.join(os.path.split(isomorphic_decode(__file__))[0], isomorphic_decode(handler) + u".py"))
     else:
         return
--- a/testing/web-platform/tests/eventsource/resources/last-event-id.py
+++ b/testing/web-platform/tests/eventsource/resources/last-event-id.py
@@ -1,11 +1,9 @@
-from six import ensure_str
-
 def main(request, response):
-  response.headers.set("Content-Type", "text/event-stream")
+  response.headers.set(b"Content-Type", b"text/event-stream")
 
-  last_event_id = ensure_str(request.headers.get("Last-Event-ID", ""))
+  last_event_id = request.headers.get(b"Last-Event-ID", b"")
   if last_event_id:
-    return "data: " + last_event_id + "\n\n"
+    return b"data: " + last_event_id + b"\n\n"
   else:
-    idvalue = request.GET.first("idvalue", u"\u2026")
-    return "id: " + idvalue + "\nretry: 200\ndata: hello\n\n"
+    idvalue = request.GET.first(b"idvalue", u"\u2026".encode("utf-8"))
+    return b"id: " + idvalue + b"\nretry: 200\ndata: hello\n\n"
--- a/testing/web-platform/tests/eventsource/resources/message.py
+++ b/testing/web-platform/tests/eventsource/resources/message.py
@@ -1,14 +1,14 @@
 import time
 
 def main(request, response):
-    mime = request.GET.first("mime", "text/event-stream")
-    message = request.GET.first("message", "data: data");
-    newline = "" if request.GET.first("newline", None) == "none" else "\n\n";
-    sleep = int(request.GET.first("sleep", "0"))
+    mime = request.GET.first(b"mime", b"text/event-stream")
+    message = request.GET.first(b"message", b"data: data");
+    newline = b"" if request.GET.first(b"newline", None) == b"none" else b"\n\n";
+    sleep = int(request.GET.first(b"sleep", b"0"))
 
-    headers = [("Content-Type", mime)]
-    body = message + newline + "\n"
+    headers = [(b"Content-Type", mime)]
+    body = message + newline + b"\n"
     if sleep != 0:
         time.sleep(sleep/1000)
 
     return headers, body
--- a/testing/web-platform/tests/eventsource/resources/message2.py
+++ b/testing/web-platform/tests/eventsource/resources/message2.py
@@ -1,35 +1,35 @@
 import time
 
 def main(request, response):
-    response.headers.set('Content-Type', 'text/event-stream')
-    response.headers.set('Cache-Control', 'no-cache')
+    response.headers.set(b'Content-Type', b'text/event-stream')
+    response.headers.set(b'Cache-Control', b'no-cache')
 
     response.explicit_flush = True
     response.write_status_headers()
 
     while True:
-        response.writer.write("data:msg")
-        response.writer.write("\n")
-        response.writer.write("data: msg")
-        response.writer.write("\n\n")
+        response.writer.write(u"data:msg")
+        response.writer.write(u"\n")
+        response.writer.write(u"data: msg")
+        response.writer.write(u"\n\n")
 
-        response.writer.write(":")
-        response.writer.write("\n")
+        response.writer.write(u":")
+        response.writer.write(u"\n")
 
-        response.writer.write("falsefield:msg")
-        response.writer.write("\n\n")
+        response.writer.write(u"falsefield:msg")
+        response.writer.write(u"\n\n")
 
-        response.writer.write("falsefield:msg")
-        response.writer.write("\n")
+        response.writer.write(u"falsefield:msg")
+        response.writer.write(u"\n")
 
-        response.writer.write("Data:data")
-        response.writer.write("\n\n")
+        response.writer.write(u"Data:data")
+        response.writer.write(u"\n\n")
 
-        response.writer.write("data")
-        response.writer.write("\n\n")
+        response.writer.write(u"data")
+        response.writer.write(u"\n\n")
 
-        response.writer.write("data:end")
-        response.writer.write("\n\n")
+        response.writer.write(u"data:end")
+        response.writer.write(u"\n\n")
 
         response.writer.flush()
         time.sleep(2)
--- a/testing/web-platform/tests/eventsource/resources/reconnect-fail.py
+++ b/testing/web-platform/tests/eventsource/resources/reconnect-fail.py
@@ -1,24 +1,24 @@
 def main(request, response):
-    name = "recon_fail_" + request.GET.first("id")
+    name = b"recon_fail_" + request.GET.first(b"id")
 
-    headers = [("Content-Type", "text/event-stream")]
+    headers = [(b"Content-Type", b"text/event-stream")]
     cookie = request.cookies.first(name, None)
     state = cookie.value if cookie is not None else None
 
-    if state == 'opened':
-        status = (200, "RECONNECT")
-        response.set_cookie(name, "reconnected");
-        body = "data: reconnected\n\n";
+    if state == b'opened':
+        status = (200, b"RECONNECT")
+        response.set_cookie(name, b"reconnected");
+        body = b"data: reconnected\n\n";
 
-    elif state == 'reconnected':
-        status = (204, "NO CONTENT (CLOSE)")
+    elif state == b'reconnected':
+        status = (204, b"NO CONTENT (CLOSE)")
         response.delete_cookie(name);
-        body = "data: closed\n\n" # Will never get through
+        body = b"data: closed\n\n" # Will never get through
 
     else:
-        status = (200, "OPEN");
-        response.set_cookie(name, "opened");
-        body = "retry: 2\ndata: opened\n\n";
+        status = (200, b"OPEN");
+        response.set_cookie(name, b"opened");
+        body = b"retry: 2\ndata: opened\n\n";
 
     return status, headers, body
 
--- a/testing/web-platform/tests/eventsource/resources/status-error.py
+++ b/testing/web-platform/tests/eventsource/resources/status-error.py
@@ -1,15 +1,15 @@
 def main(request, response):
-  status = (request.GET.first("status", "404"), "HAHAHAHA")
-  headers = [("Content-Type", "text/event-stream")]
+  status = (request.GET.first(b"status", b"404"), b"HAHAHAHA")
+  headers = [(b"Content-Type", b"text/event-stream")]
 
   # According to RFC7231, HTTP responses bearing status code 204 or 205 must
   # not specify a body. The expected browser behavior for this condition is not
   # currently defined--see the following for further discussion:
   #
   # https://github.com/web-platform-tests/wpt/pull/5227
-  if status[0] in ["204", "205"]:
-      body = ""
+  if status[0] in [b"204", b"205"]:
+      body = b""
   else:
-      body = "data: data\n\n"
+      body = b"data: data\n\n"
 
   return status, headers, body
--- a/testing/web-platform/tests/eventsource/resources/status-reconnect.py
+++ b/testing/web-platform/tests/eventsource/resources/status-reconnect.py
@@ -1,21 +1,21 @@
 def main(request, response):
-    status_code = request.GET.first("status", "204")
-    name = request.GET.first("id", status_code)
+    status_code = request.GET.first(b"status", b"204")
+    name = request.GET.first(b"id", status_code)
 
-    headers = [("Content-Type", "text/event-stream")]
+    headers = [(b"Content-Type", b"text/event-stream")]
 
-    cookie_name = "request" + name
+    cookie_name = b"request" + name
 
-    if request.cookies.first(cookie_name, "") == status_code:
+    if request.cookies.first(cookie_name, b"") == status_code:
         status = 200
         response.delete_cookie(cookie_name)
-        body = "data: data\n\n"
+        body = b"data: data\n\n"
     else:
         response.set_cookie(cookie_name, status_code);
-        status = (int(status_code), "TEST")
-        body = "retry: 2\n"
-        if "ok_first" in request.GET:
-            body += "data: ok\n\n"
+        status = (int(status_code), b"TEST")
+        body = b"retry: 2\n"
+        if b"ok_first" in request.GET:
+            body += b"data: ok\n\n"
 
     return status, headers, body