diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-04-03 21:44:42 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-04-04 18:43:56 +0900 |
commit | 6e6cd4c01e089781f06ee25d50766e79007ac172 (patch) | |
tree | 6bc843964522bd3aabfd153d37a31f3ce77e7b18 /e2e-lanthan/server/MockServer.js | |
parent | af9662f86cce9adbb2a791efb1dd420ab8ba5c3f (diff) |
Add integration tests for scroll
Diffstat (limited to 'e2e-lanthan/server/MockServer.js')
-rw-r--r-- | e2e-lanthan/server/MockServer.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/e2e-lanthan/server/MockServer.js b/e2e-lanthan/server/MockServer.js new file mode 100644 index 0000000..131c177 --- /dev/null +++ b/e2e-lanthan/server/MockServer.js @@ -0,0 +1,58 @@ +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 |