custom-cors-response.js (1301B)
1 const custom_cors_response = (payload, base_url) => { 2 base_url = base_url || new URL(location.href); 3 4 // Clone the given `payload` so that, as we modify it, we won't be mutating 5 // the caller's value in unexpected ways. 6 payload = Object.assign({}, payload); 7 payload.headers = payload.headers || {}; 8 // Note that, in order to have out-of-the-box support for tests that don't 9 // call `setup({'allow_uncaught_exception': true})` we return a no-op JS 10 // payload. This approach will avoid hitting syntax errors if the resource is 11 // interpreted as script. Without this workaround, the SyntaxError would be 12 // caught by the test harness and trigger a test failure. 13 payload.content = payload.content || '/* custom-cors-response.js content */'; 14 payload.status_code = payload.status_code || 200; 15 16 // Assume that we'll be doing a CORS-enabled fetch so we'll need to set ACAO. 17 const acao = "Access-Control-Allow-Origin"; 18 if (!(acao in payload.headers)) { 19 payload.headers[acao] = '*'; 20 } 21 22 if (!("Content-Type" in payload.headers)) { 23 payload.headers["Content-Type"] = "text/javascript"; 24 } 25 26 let ret = new URL("/common/CustomCorsResponse.py", base_url); 27 for (const key in payload) { 28 ret.searchParams.append(key, JSON.stringify(payload[key])); 29 } 30 31 return ret; 32 };