parent
							
								
									5e72521b21
								
							
						
					
					
						commit
						8c9b1a7a7a
					
				| @ -0,0 +1,75 @@ | |||||||
|  | const http = require('http'); | ||||||
|  | const EventEmitter = require('events'); | ||||||
|  | 
 | ||||||
|  | class LocalLokiServer extends EventEmitter { | ||||||
|  |   /** | ||||||
|  |    * Creates an instance of LocalLokiServer. | ||||||
|  |    * Sends out a `message` event when a new message is received. | ||||||
|  |    */ | ||||||
|  |   constructor() { | ||||||
|  |     super(); | ||||||
|  |     this.server = http.createServer((req, res) => { | ||||||
|  |       let body = []; | ||||||
|  | 
 | ||||||
|  |       // Check endpoints
 | ||||||
|  |       if (req.method === 'POST') { | ||||||
|  |         req | ||||||
|  |           .on('error', () => { | ||||||
|  |             // Internal server error
 | ||||||
|  |             res.statusCode = 500; | ||||||
|  |             res.end(); | ||||||
|  |           }) | ||||||
|  |           .on('data', chunk => { | ||||||
|  |             body.push(chunk); | ||||||
|  |           }) | ||||||
|  |           .on('end', () => { | ||||||
|  |             body = Buffer.concat(body).toString(); | ||||||
|  | 
 | ||||||
|  |             // Check endpoints here
 | ||||||
|  |             if (req.url === '/store') { | ||||||
|  |               // body is a base64 encoded string
 | ||||||
|  |               this.emit('message', body); | ||||||
|  |               res.statusCode = 200; | ||||||
|  |               res.end(); | ||||||
|  |             } else { | ||||||
|  |               res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||||||
|  |               res.end('Invalid endpoint!'); | ||||||
|  |             } | ||||||
|  |           }); | ||||||
|  |       } else { | ||||||
|  |         // Method Not Allowed
 | ||||||
|  |         res.statusCode = 405; | ||||||
|  |         res.end(); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async start(port) { | ||||||
|  |     // Close the old server
 | ||||||
|  |     await this.close(); | ||||||
|  | 
 | ||||||
|  |     // Start a listening on new server
 | ||||||
|  |     return new Promise((res, rej) => { | ||||||
|  |       this.server.listen(port, err => { | ||||||
|  |         if (err) { | ||||||
|  |           rej(err); | ||||||
|  |         } else { | ||||||
|  |           res(port); | ||||||
|  |         } | ||||||
|  |       }); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // Async wrapper for http server close
 | ||||||
|  |   close() { | ||||||
|  |     if (this.server) { | ||||||
|  |       return new Promise(res => { | ||||||
|  |         this.server.close(() => res()); | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return Promise.resolve(); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | exports.LocalLokiServer = LocalLokiServer; | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | // For reference: https://github.com/airbnb/javascript
 | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  |   env: { | ||||||
|  |     node: true, | ||||||
|  |     mocha: true, | ||||||
|  |     browser: true, | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|  |   globals: { | ||||||
|  |     check: true, | ||||||
|  |     gen: true, | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|  |   parserOptions: { | ||||||
|  |     sourceType: 'module', | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|  |   rules: { | ||||||
|  |     // We still get the value of this rule, it just allows for dev deps
 | ||||||
|  |     'import/no-extraneous-dependencies': [ | ||||||
|  |       'error', | ||||||
|  |       { | ||||||
|  |         devDependencies: true, | ||||||
|  |       }, | ||||||
|  |     ], | ||||||
|  | 
 | ||||||
|  |     // We want to keep each test structured the same, even if its contents are tiny
 | ||||||
|  |     'arrow-body-style': 'off', | ||||||
|  |   }, | ||||||
|  | }; | ||||||
| @ -0,0 +1,77 @@ | |||||||
|  | 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; | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
					Loading…
					
					
				
		Reference in New Issue