You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

58 lines
1.2 KiB

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