Skip to content

Commit

Permalink
feat: Send server identification
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh authored and joachimvh committed Sep 2, 2020
1 parent 273626a commit 4965b47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/server/ExpressHttpServer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,14 +12,26 @@ 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
origin: (origin, callback): void => callback(null, (origin ?? '*') as any),
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 });
Expand All @@ -31,6 +43,5 @@ export class ExpressHttpServer {
done();
}
});
return app.listen(port);
}
}
7 changes: 7 additions & 0 deletions test/unit/server/ExpressHttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand Down

0 comments on commit 4965b47

Please sign in to comment.