From 4965b476c9eb6405932d8e0b51039ac64e983525 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Wed, 2 Sep 2020 16:04:36 +0200 Subject: [PATCH] feat: Send server identification --- src/server/ExpressHttpServer.ts | 15 +++++++++++++-- test/unit/server/ExpressHttpServer.test.ts | 7 +++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/server/ExpressHttpServer.ts b/src/server/ExpressHttpServer.ts index 3658ddf51..e2f64b346 100644 --- a/src/server/ExpressHttpServer.ts +++ b/src/server/ExpressHttpServer.ts @@ -1,6 +1,6 @@ import { Server } from 'http'; import cors from 'cors'; -import express from 'express'; +import express, { Express } from 'express'; import { HttpHandler } from './HttpHandler'; export class ExpressHttpServer { @@ -12,7 +12,18 @@ export class ExpressHttpServer { public listen(port?: number): Server { const app = express(); + this.setup(app); + return app.listen(port); + } + protected setup(app: Express): void { + // Set up server identification + app.use((request, response, done): void => { + response.setHeader('X-Powered-By', 'Community Solid Server'); + done(); + }); + + // Set up Cross-Origin Resource Sharing (CORS) app.use(cors({ // Based on https://github.com/solid/solid-spec/blob/master/recommendations-server.md#cors---cross-origin-resource-sharing // By default origin is always '*', this forces it to be the origin header if there is one @@ -20,6 +31,7 @@ export class ExpressHttpServer { methods: [ 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE' ], })); + // Delegate to the main handler app.use(async(request, response, done): Promise => { try { await this.handler.handleSafe({ request, response }); @@ -31,6 +43,5 @@ export class ExpressHttpServer { done(); } }); - return app.listen(port); } } diff --git a/test/unit/server/ExpressHttpServer.test.ts b/test/unit/server/ExpressHttpServer.test.ts index eeb0ed78b..a33a50a95 100644 --- a/test/unit/server/ExpressHttpServer.test.ts +++ b/test/unit/server/ExpressHttpServer.test.ts @@ -42,6 +42,13 @@ describe('ExpressHttpServer', (): void => { server.close(); }); + it('sends server identification in the X-Powered-By header.', async(): Promise => { + const res = await request(server).get('/'); + expect(res.header).toEqual(expect.objectContaining({ + 'x-powered-by': 'Community Solid Server', + })); + }); + it('returns CORS headers for an OPTIONS request.', async(): Promise => { const res = await request(server) .options('/')