aboutsummaryrefslogtreecommitdiff
path: root/e2e
diff options
context:
space:
mode:
authorShin'ya UEOKA <ueokande@i-beam.org>2019-09-27 10:02:00 +0000
committerShin'ya UEOKA <ueokande@i-beam.org>2019-09-27 10:02:00 +0000
commit9c40914bbacbc6eb0aaac3e6548d03fd7d071d4e (patch)
treec1b2b2da7a3e0ab29fbf73465a7c7cda7970cfee /e2e
parentffc20750e99ab2edbfc31748418dc5ecba69039e (diff)
Add TestServer
Diffstat (limited to 'e2e')
-rw-r--r--e2e/lib/TestServer.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/e2e/lib/TestServer.ts b/e2e/lib/TestServer.ts
new file mode 100644
index 0000000..ba9845b
--- /dev/null
+++ b/e2e/lib/TestServer.ts
@@ -0,0 +1,52 @@
+import * as http from 'http';
+import * as net from 'net'
+import express from 'express';
+
+type HandlerFunc = (req: express.Request, res: express.Response) => void;
+
+export default class TestServer {
+ private http?: http.Server;
+
+ private app: express.Application;
+
+ constructor(
+ private port = 0,
+ private address = '127.0.0.1',
+ ){
+ this.app = express();
+ }
+
+ handle(path: string, f: HandlerFunc): TestServer {
+ this.app.get(path, f);
+ return this;
+ }
+
+ receiveContent(path: string, content: string): TestServer {
+ this.app.get(path, (_req: express.Request, res: express.Response) => {
+ res.status(200).send(content)
+ });
+ return this;
+ }
+
+ url(path: string = '/'): string {
+ if (!this.http) {
+ throw new Error('http server not started');
+ }
+
+ let addr = this.http.address() as net.AddressInfo;
+ return `http://${addr.address}:${addr.port}${path}`
+ }
+
+ listen() {
+ this.http = http.createServer(this.app)
+ this.http.listen(this.port, this.address);
+ }
+
+ close(): void {
+ if (!this.http) {
+ return;
+ }
+ this.http.close();
+ this.http = undefined;
+ }
+}