Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('honors protocol provided by Express request', async () => {
await givenRunningAppWithClient({protocol: 'https'});
expect(app.restServer.url).to.startWith('https:');
// supertest@3 fails with Error: self signed certificate
// FIXME(bajtos) rework this code once we upgrade to supertest@4
// await client.get('/products').trustLocalhost().expect(200);
await httpsGetAsync(app.restServer.url + '/products');
expect(observedCtx.requestedProtocol).to.equal('https');
});
});
const server = await givenAServer({
rest: {
port: 0,
host: '::1',
protocol: 'https',
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
},
});
server.handler(dummyRequestHandler);
await server.start();
const serverUrl = server.getSync(RestBindings.URL);
// The `Location` header should be something like
// https://explorer.loopback.io?url=https://[::1]:58470/openapi.json
const res = await httpsGetAsync(serverUrl + '/explorer');
const location = res.headers['location'];
expect(location).to.match(/\[\:\:1\]\:\d+\/openapi.json/);
expect(location).to.equal(
`https://explorer.loopback.io?url=${serverUrl}/openapi.json`,
);
await server.stop();
});
it('supports HTTPS protocol with a pfx file', async () => {
const httpsServer: HttpServer = givenHttpsServer({usePfx: true});
await httpsServer.start();
const response = await httpsGetAsync(httpsServer.url);
expect(response.statusCode).to.equal(200);
});
it('supports HTTPS protocol with key and certificate files', async () => {
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const serverOptions = givenHttpServerConfig({
port: 0,
protocol: 'https',
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
});
const server = await givenAServer({rest: serverOptions});
server.handler(dummyRequestHandler);
await server.start();
const serverUrl = server.getSync(RestBindings.URL);
const res = await httpsGetAsync(serverUrl);
expect(res.statusCode).to.equal(200);
});
it('honors HTTPS config binding after instantiation', async () => {
const keyPath = path.join(FIXTURES, 'key.pem');
const certPath = path.join(FIXTURES, 'cert.pem');
const serverOptions = givenHttpServerConfig({
port: 0,
protocol: 'https',
key: undefined,
cert: undefined,
});
const server = await givenAServer({rest: serverOptions});
server.handler(dummyRequestHandler);
await server.start();
let serverUrl = server.getSync(RestBindings.URL);
await expect(httpsGetAsync(serverUrl)).to.be.rejectedWith(/EPROTO/);
await server.stop();
server.bind(RestBindings.HTTPS_OPTIONS).to({
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
});
await server.start();
serverUrl = server.getSync(RestBindings.URL);
const res = await httpsGetAsync(serverUrl);
expect(res.statusCode).to.equal(200);
await server.stop();
});
cert: undefined,
});
const server = await givenAServer({rest: serverOptions});
server.handler(dummyRequestHandler);
await server.start();
let serverUrl = server.getSync(RestBindings.URL);
await expect(httpsGetAsync(serverUrl)).to.be.rejectedWith(/EPROTO/);
await server.stop();
server.bind(RestBindings.HTTPS_OPTIONS).to({
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
});
await server.start();
serverUrl = server.getSync(RestBindings.URL);
const res = await httpsGetAsync(serverUrl);
expect(res.statusCode).to.equal(200);
await server.stop();
});