-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e752927
commit fe3957f
Showing
4 changed files
with
51 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { HttpResponse } from '../../../server/HttpResponse'; | ||
import { addHeader } from '../../../util/HeaderUtil'; | ||
import { MetadataWriter } from './MetadataWriter'; | ||
|
||
/** | ||
* A {@link MetadataWriter} that takes a constant map of header names and values. | ||
*/ | ||
export class ConstantMetadataWriter extends MetadataWriter { | ||
private readonly headers: [string, string][]; | ||
|
||
public constructor(headers: Record<string, string>) { | ||
super(); | ||
this.headers = Object.entries(headers); | ||
} | ||
|
||
public async handle({ response }: { response: HttpResponse }): Promise<void> { | ||
for (const [ key, value ] of this.headers) { | ||
addHeader(response, key, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/unit/ldp/http/metadata/ConstantMetadataWriter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createResponse } from 'node-mocks-http'; | ||
import { ConstantMetadataWriter } from '../../../../../src/ldp/http/metadata/ConstantMetadataWriter'; | ||
|
||
describe('A ConstantMetadataWriter', (): void => { | ||
const writer = new ConstantMetadataWriter({ 'custom-Header': 'X', other: 'Y' }); | ||
|
||
it('adds new headers.', async(): Promise<void> => { | ||
const response = createResponse(); | ||
|
||
await expect(writer.handle({ response })).resolves.toBeUndefined(); | ||
|
||
expect(response.getHeaders()).toEqual({ 'custom-header': 'X', other: 'Y' }); | ||
}); | ||
|
||
it('extends existing headers.', async(): Promise<void> => { | ||
const response = createResponse(); | ||
response.setHeader('Other', 'A'); | ||
|
||
await expect(writer.handle({ response })).resolves.toBeUndefined(); | ||
|
||
expect(response.getHeaders()).toEqual({ 'custom-header': 'X', other: [ 'A', 'Y' ]}); | ||
}); | ||
}); |