feat: Make HeaderHandler customizable.

This commit is contained in:
Ruben Verborgh 2020-11-29 23:35:51 +01:00
parent 023ff80f48
commit d6c0f89cf5
3 changed files with 26 additions and 14 deletions

View File

@ -9,7 +9,13 @@
"@type": "CorsHandler"
},
{
"@type": "HeaderHandler"
"@type": "HeaderHandler",
"HeaderHandler:_headers": [
{
"HeaderHandler:_headers_key": "X-Powered-By",
"HeaderHandler:_headers_value": "Community Solid Server"
}
]
}
]
}

View File

@ -7,11 +7,11 @@ import type { HttpResponse } from '../HttpResponse';
export class HeaderHandler extends HttpHandler {
private readonly headers: Record<string, string>;
public constructor() {
// Not supported by Components.js yet
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
public constructor(headers: { [header: string]: string } = {}) {
super();
this.headers = {
'x-powered-by': 'Community Solid Server',
};
this.headers = { ...headers };
}
public async handle({ response }: { response: HttpResponse }): Promise<void> {

View File

@ -3,18 +3,24 @@ import { HeaderHandler } from '../../../../src/server/middleware/HeaderHandler';
import { guardStream } from '../../../../src/util/GuardedStream';
describe('a HeaderHandler', (): void => {
let handler: HeaderHandler;
it('adds no headers when none are configured.', async(): Promise<void> => {
const handler = new HeaderHandler();
beforeAll(async(): Promise<void> => {
handler = new HeaderHandler();
});
it('returns an X-Powered-By header.', async(): Promise<void> => {
const request = guardStream(createRequest());
const response = createResponse();
await handler.handleSafe({ request, response });
expect(response.getHeaders()).toEqual(expect.objectContaining({
'x-powered-by': 'Community Solid Server',
}));
expect(response.getHeaders()).toEqual({});
});
it('adds all configured headers.', async(): Promise<void> => {
const headers = { custom: 'Custom', other: 'Other' };
const handler = new HeaderHandler(headers);
const request = guardStream(createRequest());
const response = createResponse();
await handler.handleSafe({ request, response });
expect(response.getHeaders()).toEqual(expect.objectContaining(headers));
});
});