|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +constnet=require('net'); |
| 4 | +consthttp=require('http'); |
| 5 | +constassert=require('assert'); |
| 6 | + |
| 7 | +functionlogRequest(logs,req){ |
| 8 | +logs.push({ |
| 9 | +method: req.method, |
| 10 | +url: req.url, |
| 11 | +headers: { ...req.headers}, |
| 12 | +}); |
| 13 | +} |
| 14 | + |
| 15 | +// This creates a minimal proxy server that logs the requests it gets |
| 16 | +// to an array before performing proxying. |
| 17 | +exports.createProxyServer=function(){ |
| 18 | +constlogs=[]; |
| 19 | + |
| 20 | +constproxy=http.createServer(); |
| 21 | +proxy.on('request',(req,res)=>{ |
| 22 | +logRequest(logs,req); |
| 23 | +const[hostname,port]=req.headers.host.split(':'); |
| 24 | +consttargetPort=port||80; |
| 25 | + |
| 26 | +constoptions={ |
| 27 | +hostname: hostname, |
| 28 | +port: targetPort, |
| 29 | +path: req.url, |
| 30 | +method: req.method, |
| 31 | +headers: req.headers, |
| 32 | +}; |
| 33 | + |
| 34 | +constproxyReq=http.request(options,(proxyRes)=>{ |
| 35 | +res.writeHead(proxyRes.statusCode,proxyRes.headers); |
| 36 | +proxyRes.pipe(res,{end: true}); |
| 37 | +}); |
| 38 | + |
| 39 | +proxyReq.on('error',(err)=>{ |
| 40 | +logs.push({error: err,source: 'proxy request'}); |
| 41 | +res.writeHead(500); |
| 42 | +res.end('Proxy error: '+err.message); |
| 43 | +}); |
| 44 | + |
| 45 | +req.pipe(proxyReq,{end: true}); |
| 46 | +}); |
| 47 | + |
| 48 | +proxy.on('connect',(req,res,head)=>{ |
| 49 | +logRequest(logs,req); |
| 50 | + |
| 51 | +const[hostname,port]=req.url.split(':'); |
| 52 | +constproxyReq=net.connect(port,hostname,()=>{ |
| 53 | +res.write( |
| 54 | +'HTTP/1.1 200 Connection Established\r\n'+ |
| 55 | +'Proxy-agent: Node.js-Proxy\r\n'+ |
| 56 | +'\r\n', |
| 57 | +); |
| 58 | +proxyReq.write(head); |
| 59 | +res.pipe(proxyReq); |
| 60 | +proxyReq.pipe(res); |
| 61 | +}); |
| 62 | + |
| 63 | +proxyReq.on('error',(err)=>{ |
| 64 | +logs.push({error: err,source: 'proxy request'}); |
| 65 | +res.write('HTTP/1.1 500 Connection Error\r\n\r\n'); |
| 66 | +res.end('Proxy error: '+err.message); |
| 67 | +}); |
| 68 | +}); |
| 69 | + |
| 70 | +proxy.on('error',(err)=>{ |
| 71 | +logs.push({error: err,source: 'proxy server'}); |
| 72 | +}); |
| 73 | + |
| 74 | +return{ proxy, logs }; |
| 75 | +}; |
| 76 | + |
| 77 | +exports.checkProxiedRequest=asyncfunction(envExtension,expectation){ |
| 78 | +const{ spawnPromisified }=require('./'); |
| 79 | +constfixtures=require('./fixtures'); |
| 80 | +const{ code, signal, stdout, stderr }=awaitspawnPromisified( |
| 81 | +process.execPath, |
| 82 | +['--no-warnings',fixtures.path('fetch-and-log.mjs')],{ |
| 83 | +env: { |
| 84 | + ...process.env, |
| 85 | + ...envExtension, |
| 86 | +}, |
| 87 | +}); |
| 88 | + |
| 89 | +assert.deepStrictEqual({ |
| 90 | +stderr: stderr.trim(), |
| 91 | +stdout: stdout.trim(), |
| 92 | + code, |
| 93 | + signal, |
| 94 | +},{ |
| 95 | +stderr: '', |
| 96 | +code: 0, |
| 97 | +signal: null, |
| 98 | + ...expectation, |
| 99 | +}); |
| 100 | +}; |
0 commit comments