File size: 1,365 Bytes
bad179a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // shared helpers for the review tests
const http = require('http');
exports.sleep = ms => new Promise(r => setTimeout(r, ms));
exports.client = port => {
const req = (method, p, body, headers = {}) => new Promise((resolve, reject) => {
const data = body === undefined ? null : typeof body === 'string' ? body : JSON.stringify(body);
const r = http.request({ host: '127.0.0.1', port, method, path: p, agent: false, headers: { ...(data != null && { 'Content-Type': 'application/json', 'X-SM': '1' }), ...headers } }, res => {
let s = ''; res.setEncoding('utf8').on('data', d => s += d).on('end', () => { let json; try { json = JSON.parse(s); } catch {} resolve({ status: res.statusCode, headers: res.headers, text: s, json }); });
});
r.on('error', reject); r.end(data);
});
const post = async (p, body) => { const r = await req('POST', '/api/' + p, body); if (r.status !== 200) throw new Error(`${p}: ${r.status} ${r.text}`); return r.json; };
const state = async () => (await req('GET', '/api/state')).json;
return { req, post, state };
};
let failed = 0;
exports.check = (name, ok, info = '') => { console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${info ? ' — ' + String(info).slice(0, 300) : ''}`); if (!ok) ++failed; };
exports.done = () => console.log(failed ? `${failed} FAILED` : 'all passed');
|