aboutsummaryrefslogtreecommitdiff
path: root/e2e-lanthan/server
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-04-03 21:44:42 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-04-04 18:43:56 +0900
commit6e6cd4c01e089781f06ee25d50766e79007ac172 (patch)
tree6bc843964522bd3aabfd153d37a31f3ce77e7b18 /e2e-lanthan/server
parentaf9662f86cce9adbb2a791efb1dd420ab8ba5c3f (diff)
Add integration tests for scroll
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
+}