aboutsummaryrefslogtreecommitdiff
path: root/e2e-lanthan/server
diff options
context:
space:
mode:
Diffstat (limited to 'e2e-lanthan/server')
-rw-r--r--e2e-lanthan/server/MockServer.js58
-rw-r--r--e2e-lanthan/server/handlers.js17
2 files changed, 75 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
diff --git a/e2e-lanthan/server/handlers.js b/e2e-lanthan/server/handlers.js
new file mode 100644
index 0000000..979b4be
--- /dev/null
+++ b/e2e-lanthan/server/handlers.js
@@ -0,0 +1,17 @@
+const handleText = (body) => {
+ return (req, res) => {
+ res.writeHead(200, {'Content-Type': 'text/plane'});
+ res.end(body);
+ }
+}
+
+const handleHtml = (body) => {
+ return (req, res) => {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.end(body);
+ }
+}
+
+module.exports = {
+ handleText, handleHtml
+}