mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Send server identification
This commit is contained in:
committed by
Joachim Van Herwegen
parent
273626a799
commit
4965b476c9
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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('/')
|
||||
|
||||
Reference in New Issue
Block a user