You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
const axios = require('axios');
|
|
const { assert } = require('chai');
|
|
const { LocalLokiServer } = require('../../local_loki_server');
|
|
|
|
describe('LocalLokiServer', () => {
|
|
before(async () => {
|
|
this.server = new LocalLokiServer();
|
|
await this.server.start(8000);
|
|
});
|
|
|
|
after(() => {
|
|
this.server.close();
|
|
});
|
|
|
|
it('should return 405 if not a POST request', async () => {
|
|
try {
|
|
await axios.get('http://localhost:8000');
|
|
assert.fail('Got a successful response');
|
|
} catch (error) {
|
|
if (error.response) {
|
|
assert.equal(405, error.response.status);
|
|
return;
|
|
}
|
|
assert.isNotOk(error, 'Another error was receieved');
|
|
}
|
|
});
|
|
|
|
it('should return 404 if no endpoint provided', async () => {
|
|
try {
|
|
await axios.post('http://localhost:8000', { name: 'Test' });
|
|
assert.fail('Got a successful response');
|
|
} catch (error) {
|
|
if (error.response) {
|
|
assert.equal(404, error.response.status);
|
|
return;
|
|
}
|
|
assert.isNotOk(error, 'Another error was receieved');
|
|
}
|
|
});
|
|
|
|
it('should return 404 and a string if invalid enpoint is provided', async () => {
|
|
try {
|
|
await axios.post('http://localhost:8000/invalid', { name: 'Test' });
|
|
assert.fail('Got a successful response');
|
|
} catch (error) {
|
|
if (error.response) {
|
|
assert.equal(404, error.response.status);
|
|
assert.equal('Invalid endpoint!', error.response.data);
|
|
return;
|
|
}
|
|
assert.isNotOk(error, 'Another error was receieved');
|
|
}
|
|
});
|
|
|
|
describe('/store', async () => {
|
|
it('should pass the POSTed data to the callback', async () => {
|
|
const server = new LocalLokiServer();
|
|
await server.start(8001);
|
|
|
|
const promise = new Promise(res => {
|
|
server.on('message', message => {
|
|
assert.equal(message, 'This is data');
|
|
server.close();
|
|
res();
|
|
});
|
|
});
|
|
|
|
try {
|
|
await axios.post('http://localhost:8001/store', 'This is data');
|
|
} catch (error) {
|
|
assert.isNotOk(error, 'Error occured');
|
|
}
|
|
|
|
return promise;
|
|
});
|
|
});
|
|
});
|