Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('errors on invalid message', async () => {
const server = Hapi.server();
await server.register({ plugin: Nes, options: { auth: false } });
const log = server.events.once('log');
await server.start();
const client = new Nes.Client('http://localhost:' + server.info.port);
client.onError = Hoek.ignore;
await client.connect();
const a = { id: 1, type: 'other' };
a.c = a; // Circular reference
server.plugins.nes._listener._sockets._forEach((socket) => {
socket._send(a, null, Hoek.ignore);
});
const [event] = await log;
expect(event.data).to.equal('other');
client.disconnect();
await server.stop();
});
it('fails to start server when registration incomplete', async () => {
const plugin = {
name: 'plugin',
register: Hoek.ignore
};
const server = Hapi.server();
server.register(plugin);
await expect(server.start()).to.reject('Cannot start server before plugins finished registration');
});
it('is a hapi plugin that does nothing and can be registered multiple times.', async () => {
const server = Hapi.server();
expect(Toys.noop.register).to.shallow.equal(Hoek.ignore);
await server.register(Toys.noop);
expect(server.registrations).to.only.contain('toys-noop');
await server.register(Toys.noop);
expect(server.registrations).to.only.contain('toys-noop');
});
it('throws when registering a method with nested name twice', () => {
const server = Hapi.server();
server.method('tools.add', Hoek.ignore);
expect(() => {
server.method('tools.add', Hoek.ignore);
}).to.throw('Server method function name already exists: tools.add');
});
it('errors on unknown channel', async () => {
const emitter = new Podium({ name: 'test', channels: ['a', 'b'] });
emitter.on('test', Hoek.ignore);
await expect(emitter.emit('test')).to.not.reject();
await expect(emitter.emit({ name: 'test', channel: 'a' })).to.not.reject();
await expect(emitter.emit({ name: 'test', channel: 'c' })).to.reject('Unknown c channel');
});
const server = Hapi.server();
server.auth.scheme('custom', internals.implementation);
server.auth.strategy('default', 'custom');
server.auth.default('default');
await server.register({ plugin: Nes, options: { auth: { type: 'direct', password } } });
server.subscription('/', { auth: { entity: 'app' } });
await server.start();
const client = new Nes.Client('http://localhost:' + server.info.port);
await client.connect({ auth: { headers: { authorization: 'Custom john' } } });
await expect(client.subscribe('/', Hoek.ignore)).to.reject('User credentials cannot be used on an application subscription');
expect(client.subscriptions()).to.equal([]);
client.disconnect();
await server.stop();
});
const onSubscribe = (socket, path, params) => {
const error = Boom.badRequest();
delete error.output.payload.message;
throw error;
};
await server.register({ plugin: Nes, options: { auth: false } });
server.subscription('/', { onSubscribe });
await server.start();
const client = new Nes.Client('http://localhost:' + server.info.port);
await client.connect();
await expect(client.subscribe('/', Hoek.ignore)).to.reject('Bad Request');
client.disconnect();
await server.stop();
});
});
expect(() => {
server.method('tools.add', Hoek.ignore);
}).to.throw('Server method function name already exists: tools.add');
});
const log = server.events.once('log');
await server.start();
const options = {
hostname: 'localhost',
port: server.info.port,
path: '/',
method: 'POST',
headers: {
'Content-Length': '10'
}
};
const req = Http.request(options, (res) => { });
req.on('error', Hoek.ignore);
req.write('Hello\n');
setTimeout(() => req.abort(), 50);
const [event] = await log;
expect(event.error.message).to.equal('Parse Error');
await server.stop({ timeout: 10 });
});
static get noop() {
return {
name: 'toys-noop',
multiple: true,
register: Hoek.ignore
};
}