Fix tests for https server

pull/317/head
sachaaaaa 6 years ago
parent abdb232151
commit b6dc8b8a7e

@ -16,14 +16,21 @@ class LocalLokiServer extends EventEmitter {
* Creates an instance of LocalLokiServer. * Creates an instance of LocalLokiServer.
* Sends out a `message` event when a new message is received. * Sends out a `message` event when a new message is received.
*/ */
constructor(pems) { constructor(pems, options) {
super(); super();
const options = { const httpsOptions = {
key: pems.private, key: pems.private,
cert: pems.cert, cert: pems.cert,
}; };
this.upnpClient = natUpnp.createClient(); // eslint-disable-next-line no-param-reassign
this.server = https.createServer(options, (req, res) => { options = {
skipUpnp: false,
...options,
};
if (!options.skipUpnp) {
this.upnpClient = natUpnp.createClient();
}
this.server = https.createServer(httpsOptions, (req, res) => {
let body = []; let body = [];
const sendResponse = (statusCode, message = null) => { const sendResponse = (statusCode, message = null) => {
@ -89,7 +96,7 @@ class LocalLokiServer extends EventEmitter {
this.server.listen(port, ip, async (err) => { this.server.listen(port, ip, async (err) => {
if (err) { if (err) {
rej(err); rej(err);
} else { } else if (this.upnpClient) {
try { try {
const publicPort = await this.punchHole(); const publicPort = await this.punchHole();
res(publicPort); res(publicPort);
@ -98,6 +105,8 @@ class LocalLokiServer extends EventEmitter {
await this.close(); await this.close();
rej(e); rej(e);
} }
} else {
res(port);
} }
}); });
}); });
@ -144,13 +153,13 @@ class LocalLokiServer extends EventEmitter {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await p; await p;
this.publicPort = publicPort; this.publicPort = publicPort;
setTimeout(() => { this.timerHandler = setTimeout(() => {
try { try {
this.publicPort = this.punchHole(); this.publicPort = this.punchHole();
} catch (e) { } catch (e) {
this.close(); this.close();
} }
}, ttl); }, ttl * 1000);
return publicPort; return publicPort;
} catch (e) { } catch (e) {
throw new textsecure.HolePunchingError('Could not punch hole. Disabled upnp?', e); throw new textsecure.HolePunchingError('Could not punch hole. Disabled upnp?', e);
@ -161,7 +170,8 @@ class LocalLokiServer extends EventEmitter {
} }
// Async wrapper for http server close // Async wrapper for http server close
close() { close() {
if (this.publicPort) { clearInterval(this.timerHandler);
if (this.upnpClient) {
this.upnpClient.portUnmapping({ this.upnpClient.portUnmapping({
public: this.publicPort, public: this.publicPort,
}); });

@ -1,20 +1,39 @@
const axios = require('axios'); const axios = require('axios');
const { assert } = require('chai'); const { assert } = require('chai');
const LocalLokiServer = require('../../modules/local_loki_server'); const LocalLokiServer = require('../../modules/local_loki_server');
const selfsigned = require('selfsigned');
const https = require('https');
class HolePunchingError extends Error {
constructor(message, err) {
super(message);
this.name = 'HolePunchingError';
this.error = err;
}
}
describe('LocalLokiServer', () => { describe('LocalLokiServer', () => {
before(async () => { before(async () => {
this.server = new LocalLokiServer(); const attrs = [{ name: 'commonName', value: 'mypubkey' }];
const pems = selfsigned.generate(attrs, { days: 365 * 10 });
global.textsecure = {};
global.textsecure.HolePunchingError = HolePunchingError;
this.server = new LocalLokiServer(pems, { skipUpnp: true });
await this.server.start(8000); await this.server.start(8000);
this.axiosClient = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
}); });
after(() => { after(async () => {
this.server.close(); await this.server.close();
}); });
it('should return 405 if not a POST request', async () => { it('should return 405 if not a POST request', async () => {
try { try {
await axios.get('http://localhost:8000'); await this.axiosClient.get('https://localhost:8000');
assert.fail('Got a successful response'); assert.fail('Got a successful response');
} catch (error) { } catch (error) {
if (error.response) { if (error.response) {
@ -27,7 +46,7 @@ describe('LocalLokiServer', () => {
it('should return 404 if no endpoint provided', async () => { it('should return 404 if no endpoint provided', async () => {
try { try {
await axios.post('http://localhost:8000', { name: 'Test' }); await this.axiosClient.post('https://localhost:8000', { name: 'Test' });
assert.fail('Got a successful response'); assert.fail('Got a successful response');
} catch (error) { } catch (error) {
if (error.response) { if (error.response) {
@ -40,7 +59,7 @@ describe('LocalLokiServer', () => {
it('should return 404 and a string if invalid enpoint is provided', async () => { it('should return 404 and a string if invalid enpoint is provided', async () => {
try { try {
await axios.post('http://localhost:8000/invalid', { name: 'Test' }); await this.axiosClient.post('https://localhost:8000/invalid', { name: 'Test' });
assert.fail('Got a successful response'); assert.fail('Got a successful response');
} catch (error) { } catch (error) {
if (error.response) { if (error.response) {
@ -54,7 +73,9 @@ describe('LocalLokiServer', () => {
describe('/store', async () => { describe('/store', async () => {
it('should pass the POSTed data to the callback', async () => { it('should pass the POSTed data to the callback', async () => {
const server = new LocalLokiServer(); const attrs = [{ name: 'commonName', value: 'mypubkey' }];
const pems = selfsigned.generate(attrs, { days: 365 * 10 });
const server = new LocalLokiServer(pems, { skipUpnp: true });
await server.start(8001); await server.start(8001);
const messageData = { const messageData = {
method: 'store', method: 'store',
@ -74,7 +95,7 @@ describe('LocalLokiServer', () => {
}); });
try { try {
await axios.post('http://localhost:8001/storage_rpc/v1', messageData); await this.axiosClient.post('https://localhost:8001/storage_rpc/v1', messageData);
} catch (error) { } catch (error) {
assert.isNotOk(error, 'Error occured'); assert.isNotOk(error, 'Error occured');
} }

@ -32,7 +32,7 @@
"test-lib-view": "NODE_ENV=test-lib yarn run start", "test-lib-view": "NODE_ENV=test-lib yarn run start",
"test-loki-view": "NODE_ENV=test-loki yarn run start", "test-loki-view": "NODE_ENV=test-loki yarn run start",
"test-electron": "yarn grunt test", "test-electron": "yarn grunt test",
"test-node": "mocha --recursive test/app test/modules ts/test libloki/test/node", "test-node": "mocha --recursive --exit test/app test/modules ts/test libloki/test/node",
"test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test libloki/test/node", "test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test libloki/test/node",
"test-node-coverage-html": "nyc --reporter=lcov --reporter=html mocha --recursive test/app test/modules ts/test libloki/test/node", "test-node-coverage-html": "nyc --reporter=lcov --reporter=html mocha --recursive test/app test/modules ts/test libloki/test/node",
"eslint": "eslint .", "eslint": "eslint .",

Loading…
Cancel
Save