feat: Send server identification

This commit is contained in:
Ruben Verborgh
2020-09-02 16:04:36 +02:00
committed by Joachim Van Herwegen
parent 273626a799
commit 4965b476c9
2 changed files with 20 additions and 2 deletions

View File

@@ -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<void> => {
try {
await this.handler.handleSafe({ request, response });
@@ -31,6 +43,5 @@ export class ExpressHttpServer {
done();
}
});
return app.listen(port);
}
}

View File

@@ -42,6 +42,13 @@ describe('ExpressHttpServer', (): void => {
server.close();
});
it('sends server identification in the X-Powered-By header.', async(): Promise<void> => {
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<void> => {
const res = await request(server)
.options('/')