Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python 3: port tests in cors following migration guideline in RFC 49 (#…
- Loading branch information
Showing
8 changed files
with
122 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
#!/usr/bin/env python | ||
|
||
# A header used to correlate requests and responses | ||
state_header = "content-language" | ||
state_header = b"content-language" | ||
|
||
# Static ETag to use (and expect) | ||
etag = "abcdef" | ||
etag = b"abcdef" | ||
|
||
def error(msg): | ||
return (299, "Client Error"), [ | ||
('content-type', 'text/plain'), | ||
('access-control-allow-origin', "*"), | ||
('access-control-expose-headers', state_header), | ||
('cache-control', 'no-store') | ||
return (299, u"Client Error"), [ | ||
(b'content-type', b'text/plain'), | ||
(b'access-control-allow-origin', b"*"), | ||
(b'access-control-expose-headers', state_header), | ||
(b'cache-control', b'no-store') | ||
], msg | ||
|
||
def main(request, response): | ||
headers = [] | ||
|
||
inm = request.headers.get('if-none-match', None) | ||
inm = request.headers.get(b'if-none-match', None) | ||
raw_req_num = request.headers.get(state_header, None) | ||
if raw_req_num == None: | ||
return error("no req_num header in request") | ||
return error(u"no req_num header in request") | ||
else: | ||
req_num = int(raw_req_num) | ||
if req_num > 8: | ||
return error("req_num %s out of range" % req_num) | ||
return error(u"req_num %s out of range" % req_num) | ||
|
||
headers.append(("Access-Control-Expose-Headers", state_header)) | ||
headers.append((b"Access-Control-Expose-Headers", state_header)) | ||
headers.append((state_header, req_num)) | ||
headers.append(("A", req_num)) | ||
headers.append(("B", req_num)) | ||
headers.append((b"A", req_num)) | ||
headers.append((b"B", req_num)) | ||
|
||
if req_num % 2: # odd requests are the first in a test pair | ||
if inm: | ||
# what are you doing here? This should be a fresh request. | ||
return error("If-None-Match on first request") | ||
return error(u"If-None-Match on first request") | ||
else: | ||
status = 200, "OK" | ||
headers.append(("Access-Control-Allow-Origin", "*")) | ||
headers.append(("Content-Type", "text/plain")) | ||
headers.append(("Cache-Control", "private, max-age=3, must-revalidate")) | ||
headers.append(("ETag", etag)) | ||
return status, headers, "Success" | ||
status = 200, u"OK" | ||
headers.append((b"Access-Control-Allow-Origin", b"*")) | ||
headers.append((b"Content-Type", b"text/plain")) | ||
headers.append((b"Cache-Control", b"private, max-age=3, must-revalidate")) | ||
headers.append((b"ETag", etag)) | ||
return status, headers, u"Success" | ||
else: # even requests are the second in a pair, and should have a good INM. | ||
if inm != etag: | ||
# Bad browser. | ||
if inm == None: | ||
return error("If-None-Match missing") | ||
return error(u"If-None-Match missing") | ||
else: | ||
return error("If-None-Match '%s' mismatches") | ||
return error(u"If-None-Match '%s' mismatches") | ||
else: | ||
if req_num == 2: | ||
pass # basic, vanilla check | ||
elif req_num == 4: | ||
headers.append(("Access-Control-Expose-Headers", "a, b")) | ||
headers.append((b"Access-Control-Expose-Headers", b"a, b")) | ||
elif req_num == 6: | ||
headers.append(("Access-Control-Expose-Headers", "a")) | ||
headers.append((b"Access-Control-Expose-Headers", b"a")) | ||
elif req_num == 8: | ||
headers.append(("Access-Control-Allow-Origin", "other.origin.example:80")) | ||
status = 304, "Not Modified" | ||
return status, headers, "" | ||
headers.append((b"Access-Control-Allow-Origin", b"other.origin.example:80")) | ||
status = 304, u"Not Modified" | ||
return status, headers, u"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
def main(request, response): | ||
match = request.headers.get("If-None-Match", None) | ||
if match is not None and match == "mybestscript-v1": | ||
response.status = (304, "YEP") | ||
return "" | ||
response.headers.set("Access-Control-Allow-Origin", "*") | ||
response.headers.set("Cache-Control", "must-revalidate") | ||
response.headers.set("ETag", "mybestscript-v1") | ||
response.headers.set("Content-Type", "text/javascript") | ||
return "function hep() { }" | ||
match = request.headers.get(b"If-None-Match", None) | ||
if match is not None and match == b"mybestscript-v1": | ||
response.status = (304, u"YEP") | ||
return u"" | ||
response.headers.set(b"Access-Control-Allow-Origin", b"*") | ||
response.headers.set(b"Cache-Control", b"must-revalidate") | ||
response.headers.set(b"ETag", b"mybestscript-v1") | ||
response.headers.set(b"Content-Type", b"text/javascript") | ||
return u"function hep() { }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
def main(request, response): | ||
token = request.GET.first("token") | ||
token = request.GET.first(b"token") | ||
if request.server.stash.remove(token) is not None: | ||
return "1" | ||
return u"1" | ||
else: | ||
return "0" | ||
return u"0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
|
||
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") | ||
|
||
headers = [("Content-Type", "text/plain")] | ||
if origin != 'none': | ||
headers.append(("Access-Control-Allow-Origin", origin)) | ||
if credentials != 'none': | ||
headers.append(("Access-Control-Allow-Credentials", credentials)) | ||
headers = [(b"Content-Type", b"text/plain")] | ||
if origin != b'none': | ||
headers.append((b"Access-Control-Allow-Origin", origin)) | ||
if credentials != b'none': | ||
headers.append((b"Access-Control-Allow-Credentials", credentials)) | ||
|
||
ident = request.GET.first('ident', 'test') | ||
ident = request.GET.first(b'ident', b'test') | ||
|
||
if ident in request.cookies: | ||
body = request.cookies[ident].value | ||
response.delete_cookie(ident) | ||
else: | ||
response.set_cookie(ident, "COOKIE") | ||
body = "NO_COOKIE" | ||
response.set_cookie(ident, b"COOKIE") | ||
body = u"NO_COOKIE" | ||
|
||
return headers, body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,69 @@ | ||
import json | ||
|
||
from wptserve.utils import isomorphic_decode | ||
|
||
def main(request, response): | ||
origin = request.GET.first("origin", request.headers.get('origin')) | ||
origin = request.GET.first(b"origin", request.headers.get(b'origin')) | ||
|
||
if "check" in request.GET: | ||
token = request.GET.first("token") | ||
if b"check" in request.GET: | ||
token = request.GET.first(b"token") | ||
value = request.server.stash.take(token) | ||
if value is not None: | ||
if request.GET.first("check", None) == "keep": | ||
if request.GET.first(b"check", None) == b"keep": | ||
request.server.stash.put(token, value) | ||
body = "1" | ||
body = u"1" | ||
else: | ||
body = "0" | ||
return [("Content-Type", "text/plain")], body | ||
body = u"0" | ||
return [(b"Content-Type", b"text/plain")], body | ||
|
||
|
||
if origin != 'none': | ||
response.headers.set("Access-Control-Allow-Origin", origin) | ||
if 'origin2' in request.GET: | ||
response.headers.append("Access-Control-Allow-Origin", request.GET.first('origin2')) | ||
if origin != b'none': | ||
response.headers.set(b"Access-Control-Allow-Origin", origin) | ||
if b'origin2' in request.GET: | ||
response.headers.append(b"Access-Control-Allow-Origin", request.GET.first(b'origin2')) | ||
|
||
#Preflight | ||
if 'headers' in request.GET: | ||
response.headers.set("Access-Control-Allow-Headers", request.GET.first('headers')) | ||
if 'credentials' in request.GET: | ||
response.headers.set("Access-Control-Allow-Credentials", request.GET.first('credentials')) | ||
if 'methods' in request.GET: | ||
response.headers.set("Access-Control-Allow-Methods", request.GET.first('methods')) | ||
if b'headers' in request.GET: | ||
response.headers.set(b"Access-Control-Allow-Headers", request.GET.first(b'headers')) | ||
if b'credentials' in request.GET: | ||
response.headers.set(b"Access-Control-Allow-Credentials", request.GET.first(b'credentials')) | ||
if b'methods' in request.GET: | ||
response.headers.set(b"Access-Control-Allow-Methods", request.GET.first(b'methods')) | ||
|
||
code_raw = request.GET.first('code', None) | ||
code_raw = request.GET.first(b'code', None) | ||
if code_raw: | ||
code = int(code_raw) | ||
else: | ||
code = None | ||
if request.method == 'OPTIONS': | ||
if request.method == u'OPTIONS': | ||
#Override the response code if we're in a preflight and it's asked | ||
if 'preflight' in request.GET: | ||
code = int(request.GET.first('preflight')) | ||
if b'preflight' in request.GET: | ||
code = int(request.GET.first(b'preflight')) | ||
|
||
#Log that the preflight actually happened if we have an ident | ||
if 'token' in request.GET: | ||
request.server.stash.put(request.GET['token'], True) | ||
if b'token' in request.GET: | ||
request.server.stash.put(request.GET[b'token'], True) | ||
|
||
if 'location' in request.GET: | ||
if b'location' in request.GET: | ||
if code is None: | ||
code = 302 | ||
|
||
if code >= 300 and code < 400: | ||
response.headers.set("Location", request.GET.first('location')) | ||
response.headers.set(b"Location", request.GET.first(b'location')) | ||
|
||
headers = {} | ||
for name, values in request.headers.iteritems(): | ||
for name, values in request.headers.items(): | ||
if len(values) == 1: | ||
headers[name] = values[0] | ||
headers[isomorphic_decode(name)] = isomorphic_decode(values[0]) | ||
else: | ||
#I have no idea, really | ||
headers[name] = values | ||
|
||
headers['get_value'] = request.GET.first('get_value', '') | ||
headers[u'get_value'] = isomorphic_decode(request.GET.first(b'get_value', b'')) | ||
|
||
body = json.dumps(headers) | ||
|
||
if code: | ||
return (code, "StatusText"), [], body | ||
return (code, u"StatusText"), [], body | ||
else: | ||
return body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
def main(request, response): | ||
response.add_required_headers = False | ||
output = "HTTP/1.1 221 ALL YOUR BASE BELONG TO H1\r\n" | ||
output += "Access-Control-Allow-Origin: *\r\n" | ||
output += "BB-8: hey\r\n" | ||
output += "Content-Language: mkay\r\n" | ||
output += request.GET.first("expose") + "\r\n" | ||
output += "\r\n" | ||
output = b"HTTP/1.1 221 ALL YOUR BASE BELONG TO H1\r\n" | ||
output += b"Access-Control-Allow-Origin: *\r\n" | ||
output += b"BB-8: hey\r\n" | ||
output += b"Content-Language: mkay\r\n" | ||
output += request.GET.first(b"expose") + b"\r\n" | ||
output += b"\r\n" | ||
response.writer.write(output) | ||
response.close_connection = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
def main(request, response): | ||
headers = [("Content-Type", "text/plain")] | ||
headers = [(b"Content-Type", b"text/plain")] | ||
|
||
if "check" in request.GET: | ||
token = request.GET.first("token") | ||
if b"check" in request.GET: | ||
token = request.GET.first(b"token") | ||
value = request.server.stash.take(token) | ||
if value == None: | ||
body = "0" | ||
body = u"0" | ||
else: | ||
if request.GET.first("check", None) == "keep": | ||
if request.GET.first(b"check", None) == b"keep": | ||
request.server.stash.put(token, value) | ||
body = "1" | ||
body = u"1" | ||
|
||
return headers, body | ||
|
||
if request.method == "OPTIONS": | ||
if not "Access-Control-Request-Method" in request.headers: | ||
response.set_error(400, "No Access-Control-Request-Method header") | ||
return "ERROR: No access-control-request-method in preflight!" | ||
if request.method == u"OPTIONS": | ||
if not b"Access-Control-Request-Method" in request.headers: | ||
response.set_error(400, u"No Access-Control-Request-Method header") | ||
return u"ERROR: No access-control-request-method in preflight!" | ||
|
||
headers.append(("Access-Control-Allow-Methods", | ||
request.headers['Access-Control-Request-Method'])) | ||
headers.append((b"Access-Control-Allow-Methods", | ||
request.headers[b'Access-Control-Request-Method'])) | ||
|
||
if "max_age" in request.GET: | ||
headers.append(("Access-Control-Max-Age", request.GET['max_age'])) | ||
if b"max_age" in request.GET: | ||
headers.append((b"Access-Control-Max-Age", request.GET[b'max_age'])) | ||
|
||
if "token" in request.GET: | ||
request.server.stash.put(request.GET.first("token"), 1) | ||
if b"token" in request.GET: | ||
request.server.stash.put(request.GET.first(b"token"), 1) | ||
|
||
headers.append(("Access-Control-Allow-Origin", "*")) | ||
headers.append(("Access-Control-Allow-Headers", "x-print")) | ||
headers.append((b"Access-Control-Allow-Origin", b"*")) | ||
headers.append((b"Access-Control-Allow-Headers", b"x-print")) | ||
|
||
body = request.headers.get("x-print", "NO") | ||
body = request.headers.get(b"x-print", b"NO") | ||
|
||
return headers, body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
from wptserve.utils import isomorphic_encode | ||
|
||
def main(request, response): | ||
response.headers.set("Access-Control-Allow-Origin", request.headers.get("origin")) | ||
response.headers.set("Access-Control-Expose-Headers", "X-Request-Method") | ||
response.headers.set(b"Access-Control-Allow-Origin", request.headers.get(b"origin")) | ||
response.headers.set(b"Access-Control-Expose-Headers", b"X-Request-Method") | ||
|
||
if request.method == 'OPTIONS': | ||
response.headers.set("Access-Control-Allow-Methods", "GET, CHICKEN, HEAD, POST, PUT") | ||
if request.method == u'OPTIONS': | ||
response.headers.set(b"Access-Control-Allow-Methods", b"GET, CHICKEN, HEAD, POST, PUT") | ||
|
||
if 'headers' in request.GET: | ||
response.headers.set("Access-Control-Allow-Headers", request.GET.first('headers')) | ||
if b'headers' in request.GET: | ||
response.headers.set(b"Access-Control-Allow-Headers", request.GET.first(b'headers')) | ||
|
||
response.headers.set("X-Request-Method", request.method) | ||
response.headers.set(b"X-Request-Method", isomorphic_encode(request.method)) | ||
|
||
response.headers.set("X-A-C-Request-Method", request.headers.get("Access-Control-Request-Method", "")) | ||
response.headers.set(b"X-A-C-Request-Method", request.headers.get(b"Access-Control-Request-Method", b"")) | ||
|
||
|
||
#This should reasonably work for most response codes. | ||
try: | ||
code = int(request.GET.first("code", 200)) | ||
code = int(request.GET.first(b"code", 200)) | ||
except ValueError: | ||
code = 200 | ||
|
||
text = request.GET.first("text", "OMG") | ||
text = request.GET.first(b"text", b"OMG") | ||
|
||
if request.method == "OPTIONS" and "preflight" in request.GET: | ||
if request.method == u"OPTIONS" and b"preflight" in request.GET: | ||
try: | ||
code = int(request.GET.first('preflight')) | ||
code = int(request.GET.first(b'preflight')) | ||
except KeyError: | ||
pass | ||
|
||
status = code, text | ||
|
||
if "type" in request.GET: | ||
response.headers.set("Content-Type", request.GET.first('type')) | ||
if b"type" in request.GET: | ||
response.headers.set(b"Content-Type", request.GET.first(b'type')) | ||
|
||
body = request.GET.first('content', "") | ||
body = request.GET.first(b'content', b"") | ||
|
||
return status, [], body |