Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('returns not found on internal only route (external)', async () => {
const server = Hapi.server();
server.route({
method: 'GET',
path: '/some/route',
options: {
isInternal: true,
handler: () => 'ok'
}
});
await server.start();
const err = await expect(Wreck.get('http://localhost:' + server.info.port)).to.reject();
expect(err.data.res.statusCode).to.equal(404);
expect(err.data.payload.toString()).to.equal('{"statusCode":404,"error":"Not Found","message":"Not Found"}');
await server.stop();
});
it('uses a provided listener with manual listen', async () => {
const listener = Http.createServer();
const server = Hapi.server({ listener, autoListen: false });
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
const listen = () => {
return new Promise((resolve) => listener.listen(0, 'localhost', resolve));
};
await listen();
await server.start();
const { payload } = await Wreck.get('http://localhost:' + server.info.port + '/');
expect(payload.toString()).to.equal('ok');
await server.stop();
});
reporters: {
foo: [
new Reporters.Namer('foo'),
out1
],
bar: [
out2
]
}
});
await server.start();
monitor.start();
const err = await expect(Wreck.get(server.info.uri + '/?q=test')).to.reject();
expect(err.output.payload.statusCode).to.equal(500);
await Hoek.wait(50);
const res1 = out1.data;
const res2 = out2.data;
expect(res1).to.have.length(4);
expect(res1).to.part.contain([{
event: 'request',
tags: ['test-tag'],
data: 'log request data',
method: 'get',
path: '/',
config: { foo: 'bar' },
name: 'foo'
it('returns a gzip response on a get request when accept-encoding: * is requested', async () => {
const data = '{"test":"true"}';
const server = Hapi.server({ compression: { minBytes: 1 } });
server.route({ method: 'GET', path: '/', handler: () => data });
await server.start();
const uri = 'http://localhost:' + server.info.port;
const { payload } = await Wreck.get(uri, { headers: { 'accept-encoding': '*' } });
expect(payload.toString()).to.equal(data);
await server.stop();
});
it('refuses to handle new incoming requests on persistent connections', async () => {
const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();
const agent = new Http.Agent({ keepAlive: true, maxSockets: 1 });
const first = Wreck.get('http://localhost:' + server.info.port + '/', { agent });
const second = Wreck.get('http://localhost:' + server.info.port + '/', { agent });
const { res, payload } = await first;
const stop = server.stop();
await expect(second).to.reject();
await stop;
expect(res.headers.connection).to.equal('keep-alive');
expect(payload.toString()).to.equal('ok');
expect(server._core.started).to.equal(false);
});
it('refuses to handle new incoming requests on persistent connections', async () => {
const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();
const agent = new Http.Agent({ keepAlive: true, maxSockets: 1 });
const first = Wreck.get('http://localhost:' + server.info.port + '/', { agent });
const second = Wreck.get('http://localhost:' + server.info.port + '/', { agent });
const { res, payload } = await first;
const stop = server.stop();
await expect(second).to.reject();
await stop;
expect(res.headers.connection).to.equal('keep-alive');
expect(payload.toString()).to.equal('ok');
expect(server._core.started).to.equal(false);
});
++readTimes;
if (readTimes > chunkTimes) {
return this.push(null);
}
this.push(block);
};
return fileStream;
};
const server = Hapi.server();
server.route({ method: 'GET', path: '/', handler });
await server.start();
const { payload } = await Wreck.get('http://localhost:' + server.info.port);
expect(payload.toString()).to.equal(expectedBody);
await server.stop();
});
'x-forwarded-host': 'example.com'
};
return {
uri: `http://127.0.0.1:${upstream.info.port}/`,
headers
};
};
const server = Hapi.server({ host: '127.0.0.1' });
await server.register(H2o2);
server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } });
await server.start();
const response = await Wreck.get('http://127.0.0.1:' + server.info.port + '/');
expect(response.res.statusCode).to.equal(200);
const result = JSON.parse(response.payload);
let expectedClientAddress = '127.0.0.1';
if (Net.isIPv6(server.listener.address().address)) {
expectedClientAddress = '::ffff:127.0.0.1';
}
expect(result['x-forwarded-for']).to.equal('testhost,' + expectedClientAddress);
expect(result['x-forwarded-port']).to.equal('1337');
expect(result['x-forwarded-proto']).to.equal('https');
expect(result['x-forwarded-host']).to.equal('example.com');
await upstream.stop();
await server.stop();
const moonInPhase = async (request, h) => {
const timestamp = parseInt(Date.now() / 1000);
const url = `http://api.farmsense.net/v1/moonphases/?d=${timestamp}`;
console.log(`Getting ${url}`);
const { payload } = await Wreck.get(url, { json: 'force' });
if (payload[0].Phase !== 'Waning Cresent') {
throw Boom.forbidden(`${payload[0].Phase} is the wrong moon phase for admin night.`);
}
return h.continue;
};