import base64 def maybeBoolToJavascriptLiteral(value): if value == None: return "undefined" if value == True: return "true" if value == False: return "false" raise ValueError("Expected bool or None") def main(request, response): destination = request.headers.get("sec-fetch-dest").decode("utf-8") gpcValue = request.headers.get("sec-gpc") == b'1' expectedGPCValue = request.GET.get(b"gpc") == b"true" inFrame = request.GET.get(b"framed") != None destinationDescription = "framed " + destination if inFrame else destination if destination == "document" or destination == "iframe": response.headers.set('Content-Type', 'text/html'); return f""" Sec-GPC {destination}
""" + (f""" """ if destination == "document" else "") + f""" """ elif destination == "image": if gpcValue == expectedGPCValue: return (200, [(b"Content-Type", b"image/png")], base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEUlEQVR42mP8nzaTAQQYYQwALssD/5ca+r8AAAAASUVORK5CYII=")) return (400, [], "") elif destination == "script": response.headers.set('Content-Type', 'application/javascript'); return f""" debugger; test(function(t) {{ assert_equals({maybeBoolToJavascriptLiteral(gpcValue)}, {maybeBoolToJavascriptLiteral(expectedGPCValue)}, "Expected Sec-GPC value ({maybeBoolToJavascriptLiteral(expectedGPCValue)}) is on the {destinationDescription} fetch"); }}, `Expected Sec-GPC value ({maybeBoolToJavascriptLiteral(expectedGPCValue)}) is on the {destinationDescription} fetch`); """ elif destination == "worker" or destination == "sharedworker" or destination == "serviceworker": response.headers.set('Content-Type', 'application/javascript'); return f""" importScripts("/resources/testharness.js"); test(function(t) {{ assert_equals({maybeBoolToJavascriptLiteral(gpcValue)}, {maybeBoolToJavascriptLiteral(expectedGPCValue)}, "Expected Sec-GPC value ({maybeBoolToJavascriptLiteral(expectedGPCValue)}) is on the {destinationDescription} fetch"); }}, `Expected Sec-GPC value ({maybeBoolToJavascriptLiteral(expectedGPCValue)}) is on the {destinationDescription} fetch`); done(); """ raise ValueError("Unexpected destination")