Repository: solid/community-server
Pull request #342 (2020-11-20 13:50:52)

Add ExpressHttpServerFactory


Patch:
diff --git a/config/presets/http.json b/config/presets/http.json
index cc407d0d2..5640ea22f 100644
--- a/config/presets/http.json
+++ b/config/presets/http.json
@@ -2,9 +2,9 @@
   "@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld",
   "@graph": [
     {
-      "@id": "urn:solid-server:default:HttpServer",
-      "@type": "ExpressHttpServer",
-      "ExpressHttpServer:_handler": {
+      "@id": "urn:solid-server:default:ServerFactory",
+      "@type": "ExpressHttpServerFactory",
+      "ExpressHttpServerFactory:_handler": {
         "@id": "urn:solid-server:default:HttpHandler"
       }
     }
diff --git a/config/presets/setup.json b/config/presets/setup.json
index 3f19fe74e..81132d851 100644
--- a/config/presets/setup.json
+++ b/config/presets/setup.json
@@ -4,8 +4,8 @@
     {
       "@id": "urn:solid-server:default",
       "@type": "Setup",
-      "Setup:_httpServer": {
-        "@id": "urn:solid-server:default:HttpServer"
+      "Setup:_serverFactory": {
+        "@id": "urn:solid-server:default:ServerFactory"
       },
       "Setup:_store": {
         "@id": "urn:solid-server:default:ResourceStore_Patching"
diff --git a/index.ts b/index.ts
index 80b4b2458..dbdb5acb3 100644
--- a/index.ts
+++ b/index.ts
@@ -84,7 +84,7 @@ export * from './src/logging/VoidLoggerFactory';
 export * from './src/logging/WinstonLoggerFactory';
 
 // Server
-export * from './src/server/ExpressHttpServer';
+export * from './src/server/ExpressHttpServerFactory';
 export * from './src/server/HttpHandler';
 export * from './src/server/HttpRequest';
 export * from './src/server/HttpResponse';
diff --git a/src/init/Setup.ts b/src/init/Setup.ts
index 91b5e26f1..e2e6ce789 100644
--- a/src/init/Setup.ts
+++ b/src/init/Setup.ts
@@ -3,7 +3,7 @@ import type { AclManager } from '../authorization/AclManager';
 import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
 import type { LoggerFactory } from '../logging/LoggerFactory';
 import { getLoggerFor, setGlobalLoggerFactory } from '../logging/LogUtil';
-import type { ExpressHttpServer } from '../server/ExpressHttpServer';
+import type { ExpressHttpServerFactory } from '../server/ExpressHttpServerFactory';
 import type { ResourceStore } from '../storage/ResourceStore';
 import { TEXT_TURTLE } from '../util/ContentTypes';
 import { CONTENT_TYPE } from '../util/UriConstants';
@@ -13,7 +13,7 @@ import { CONTENT_TYPE } from '../util/UriConstants';
  */
 export class Setup {
   protected readonly logger = getLoggerFor(this);
-  private readonly httpServer: ExpressHttpServer;
+  private readonly serverFactory: ExpressHttpServerFactory;
   private readonly store: ResourceStore;
   private readonly aclManager: AclManager;
   private readonly loggerFactory: LoggerFactory;
@@ -21,14 +21,14 @@ export class Setup {
   private readonly port: number;
 
   public constructor(
-    httpServer: ExpressHttpServer,
+    serverFactory: ExpressHttpServerFactory,
     store: ResourceStore,
     aclManager: AclManager,
     loggerFactory: LoggerFactory,
     base: string,
     port: number,
   ) {
-    this.httpServer = httpServer;
+    this.serverFactory = serverFactory;
     this.store = store;
     this.aclManager = aclManager;
     this.loggerFactory = loggerFactory;
@@ -73,7 +73,7 @@ export class Setup {
     this.logger.debug('Setup default ACL settings');
     await aclSetup();
 
-    this.httpServer.listen(this.port);
+    this.serverFactory.startServer(this.port);
 
     return this.base;
   }
diff --git a/src/server/ExpressHttpServer.ts b/src/server/ExpressHttpServerFactory.ts
similarity index 90%
rename from src/server/ExpressHttpServer.ts
rename to src/server/ExpressHttpServerFactory.ts
index 9217d8aab..f346c8c1e 100644
--- a/src/server/ExpressHttpServer.ts
+++ b/src/server/ExpressHttpServerFactory.ts
@@ -4,8 +4,9 @@ import type { Express } from 'express';
 import express from 'express';
 import { getLoggerFor } from '../logging/LogUtil';
 import type { HttpHandler } from './HttpHandler';
+import type { HttpServerFactory } from './HttpServerFactory';
 
-export class ExpressHttpServer {
+export class ExpressHttpServerFactory implements HttpServerFactory {
   protected readonly logger = getLoggerFor(this);
 
   private readonly handler: HttpHandler;
@@ -14,7 +15,7 @@ export class ExpressHttpServer {
     this.handler = handler;
   }
 
-  public listen(port?: number): Server {
+  public startServer(port: number): Server {
     const app = express();
     this.setup(app);
     return app.listen(port);
diff --git a/src/server/HttpServerFactory.ts b/src/server/HttpServerFactory.ts
new file mode 100644
index 000000000..a0bd50f19
--- /dev/null
+++ b/src/server/HttpServerFactory.ts
@@ -0,0 +1,8 @@
+import type { Server } from 'http';
+
+/**
+ * A factory for HTTP servers
+ */
+export interface HttpServerFactory {
+  startServer: (port: number) => Server;
+}


Test patch:
diff --git a/test/unit/init/Setup.test.ts b/test/unit/init/Setup.test.ts
index 4c5c09695..724be4164 100644
--- a/test/unit/init/Setup.test.ts
+++ b/test/unit/init/Setup.test.ts
@@ -3,7 +3,7 @@ import type { ResourceIdentifier } from '../../../src/ldp/representation/Resourc
 import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory';
 
 describe('Setup', (): void => {
-  let httpServer: any;
+  let serverFactory: any;
   let store: any;
   let aclManager: any;
   let setup: Setup;
@@ -14,15 +14,15 @@ describe('Setup', (): void => {
     aclManager = {
       getAcl: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
     };
-    httpServer = {
-      listen: jest.fn(),
+    serverFactory = {
+      startServer: jest.fn(),
     };
-    setup = new Setup(httpServer, store, aclManager, new VoidLoggerFactory(), 'http://localhost:3000/', 3000);
+    setup = new Setup(serverFactory, store, aclManager, new VoidLoggerFactory(), 'http://localhost:3000/', 3000);
   });
 
   it('starts an HTTP server.', async(): Promise<void> => {
     await setup.setup();
-    expect(httpServer.listen).toHaveBeenCalledWith(3000);
+    expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
   });
 
   it('invokes ACL initialization.', async(): Promise<void> => {
diff --git a/test/unit/server/ExpressHttpServer.test.ts b/test/unit/server/ExpressHttpServerFactory.test.ts
similarity index 94%
rename from test/unit/server/ExpressHttpServer.test.ts
rename to test/unit/server/ExpressHttpServerFactory.test.ts
index 56c5e7406..eff416d19 100644
--- a/test/unit/server/ExpressHttpServer.test.ts
+++ b/test/unit/server/ExpressHttpServerFactory.test.ts
@@ -1,6 +1,6 @@
 import type { Server } from 'http';
 import request from 'supertest';
-import { ExpressHttpServer } from '../../../src/server/ExpressHttpServer';
+import { ExpressHttpServerFactory } from '../../../src/server/ExpressHttpServerFactory';
 import { HttpHandler } from '../../../src/server/HttpHandler';
 import type { HttpRequest } from '../../../src/server/HttpRequest';
 import type { HttpResponse } from '../../../src/server/HttpResponse';
@@ -17,7 +17,7 @@ class SimpleHttpHandler extends HttpHandler {
   }
 }
 
-describe('ExpressHttpServer', (): void => {
+describe('ExpressHttpServerFactory', (): void => {
   let server: Server;
   let canHandleJest: jest.Mock<Promise<void>, []>;
   let handleJest: jest.Mock<Promise<void>, [any]>;
@@ -37,8 +37,8 @@ describe('ExpressHttpServer', (): void => {
     handler.canHandle = canHandleJest;
     handler.handle = handleJest;
 
-    const expressServer = new ExpressHttpServer(handler);
-    server = expressServer.listen();
+    const factory = new ExpressHttpServerFactory(handler);
+    server = factory.startServer(5555);
   });
 
   afterEach(async(): Promise<void> => {