This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/e2e-lanthan/server/MockServer.js
2019-04-04 18:43:56 +09:00

58 lines
1.2 KiB
JavaScript

var http = require('http');
var url = require('url');
var handlers = require('./handlers');
class MockServer {
constructor() {
this.handlers = [];
this.server = undefined;
}
start() {
if (this.server) {
throw new Error('Server is already started');
}
let listener = (req, res) => {
if (req.method !== 'GET') {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found')
return
}
let u = url.parse(req.url);
let handler = this.handlers.find(h => u.pathname == h.pathname);
if (!handler) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found')
return
}
handler.handler(req, res);
}
this.server = http.createServer(listener);
this.server.listen();
}
stop() {
if (!this.server) {
throw new Error('Server is not started');
}
this.server.close();
this.server = undefined;
}
port() {
if (!this.server) {
throw new Error('Server is not started');
}
return this.server.address().port
}
on(pathname, handler) {
this.handlers.push({ pathname, handler });
}
}
module.exports = MockServer