Skip to content

Commit

Permalink
feat: Make HeaderHandler customizable.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Nov 29, 2020
1 parent 023ff80 commit d6c0f89
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
8 changes: 7 additions & 1 deletion config/presets/middleware.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
"@type": "CorsHandler"
},
{
"@type": "HeaderHandler"
"@type": "HeaderHandler",
"HeaderHandler:_headers": [
{
"HeaderHandler:_headers_key": "X-Powered-By",
"HeaderHandler:_headers_value": "Community Solid Server"
}
]
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions src/server/middleware/HeaderHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
20 changes: 13 additions & 7 deletions test/unit/server/middleware/HeaderHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
const request = guardStream(createRequest());
const response = createResponse();
await handler.handleSafe({ request, response });

expect(response.getHeaders()).toEqual({});
});

it('returns an X-Powered-By header.', async(): Promise<void> => {
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({
'x-powered-by': 'Community Solid Server',
}));

expect(response.getHeaders()).toEqual(expect.objectContaining(headers));
});
});

0 comments on commit d6c0f89

Please sign in to comment.