CustomCorsResponse.py (1343B)
1 import json 2 3 def main(request, response): 4 '''Handler for getting an HTTP response customised by the given query 5 parameters. 6 7 The returned response will have 8 - HTTP headers defined by the 'headers' query parameter 9 - Must be a serialized JSON dictionary mapping header names to header 10 values 11 - HTTP status code defined by the 'status' query parameter 12 - Must be a positive serialized JSON integer like the string '200' 13 - Response content defined by the 'content' query parameter 14 - Must be a serialized JSON string representing the desired response body 15 ''' 16 def query_parameter_or_default(param, default): 17 return request.GET.first(param) if param in request.GET else default 18 19 headers = json.loads(query_parameter_or_default(b'headers', b'"{}"')) 20 for k, v in headers.items(): 21 response.headers.set(k, v) 22 23 # Note that, in order to have out-of-the-box support for tests that don't call 24 # setup({'allow_uncaught_exception': true}) 25 # we return a no-op JS payload. This approach will avoid syntax errors in 26 # script resources that would otherwise cause the test harness to fail. 27 response.content = json.loads(query_parameter_or_default(b'content', 28 b'"/* CustomCorsResponse.py content */"')) 29 response.status_code = json.loads(query_parameter_or_default(b'status', 30 b'200'))