(url, method, body, token)
| 16 | } |
| 17 | |
| 18 | function request(url, method, body, token) { |
| 19 | return new Promise((resolve, reject) => { |
| 20 | const u = new URL(url); |
| 21 | const payload = body ? JSON.stringify(body) : ''; |
| 22 | const headers = { |
| 23 | 'Content-Type': 'application/json', |
| 24 | 'Content-Length': Buffer.byteLength(payload), |
| 25 | }; |
| 26 | if (token) headers['Authorization'] = 'Bearer ' + token; |
| 27 | const req = http.request({ |
| 28 | hostname: u.hostname, |
| 29 | port: u.port, |
| 30 | path: u.pathname + u.search, |
| 31 | method, |
| 32 | headers, |
| 33 | }, (res) => { |
| 34 | const chunks = []; |
| 35 | res.on('data', c => chunks.push(c)); |
| 36 | res.on('end', () => { |
| 37 | const raw = Buffer.concat(chunks).toString(); |
| 38 | try { resolve({ status: res.statusCode, body: JSON.parse(raw) }); } |
| 39 | catch { resolve({ status: res.statusCode, body: raw }); } |
| 40 | }); |
| 41 | }); |
| 42 | req.on('error', reject); |
| 43 | if (payload) req.write(payload); |
| 44 | req.end(); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | describe('ProxyHttpServer', () => { |
| 49 | let store, server, baseUrl, dataDir, serverToken; |
no test coverage detected