(payload, additionalHeaders = {})
| 11 | const configPath = path.join(tempDir, "test-config.json"), |
| 12 | handlerPath = path.join(tempDir, "echo-handler.cjs"); |
| 13 | async function makeRequest(payload, additionalHeaders = {}) { |
| 14 | return new Promise((resolve, reject) => { |
| 15 | const data = JSON.stringify(payload), |
| 16 | headers = { "Content-Type": "application/json", Accept: "application/json, text/event-stream", "Content-Length": Buffer.byteLength(data), ...additionalHeaders }, |
| 17 | req = http.request({ hostname: "localhost", port: 4100, path: "/", method: "POST", headers }, res => { |
| 18 | let responseData = ""; |
| 19 | (res.on("data", chunk => { |
| 20 | responseData += chunk; |
| 21 | }), |
| 22 | res.on("end", () => { |
| 23 | try { |
| 24 | resolve({ status: res.statusCode, data: JSON.parse(responseData), headers: res.headers }); |
| 25 | } catch (e) { |
| 26 | reject(new Error(`Failed to parse response: ${responseData}`)); |
| 27 | } |
| 28 | })); |
| 29 | }); |
| 30 | (req.on("error", reject), req.write(data), req.end()); |
| 31 | }); |
| 32 | } |
| 33 | (fs.writeFileSync( |
| 34 | handlerPath, |
| 35 | 'let input = "";\nprocess.stdin.on("data", chunk => { input += chunk; });\nprocess.stdin.on("end", () => {\n const args = JSON.parse(input);\n const result = { echo: args.message || "empty", timestamp: Date.now() };\n console.log(JSON.stringify(result));\n});' |
no test coverage detected