Skip to content

Commit

Permalink
feat: Add ConstantMetadataWriter.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh authored and joachimvh committed Jan 26, 2021
1 parent e752927 commit fe3957f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from './init/ServerInitializer';

// LDP/HTTP/Metadata
export * from './ldp/http/metadata/BasicMetadataExtractor';
export * from './ldp/http/metadata/ConstantMetadataWriter';
export * from './ldp/http/metadata/ContentTypeParser';
export * from './ldp/http/metadata/LinkRelMetadataWriter';
export * from './ldp/http/metadata/LinkTypeParser';
Expand Down
21 changes: 21 additions & 0 deletions src/ldp/http/metadata/ConstantMetadataWriter.ts
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);
}
}
}
12 changes: 6 additions & 6 deletions src/ldp/http/metadata/MappedMetadataWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { MetadataWriter } from './MetadataWriter';
* The header value(s) will be the same as the corresponding object value(s).
*/
export class MappedMetadataWriter extends MetadataWriter {
private readonly headerMap: Record<string, string>;
private readonly headerMap: [string, string][];

public constructor(headerMap: Record<string, string>) {
super();
this.headerMap = headerMap;
this.headerMap = Object.entries(headerMap);
}

public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise<void> {
for (const key of Object.keys(this.headerMap)) {
const values = input.metadata.getAll(key).map((term): string => term.value);
if (values.length > 0) {
addHeader(input.response, this.headerMap[key], values);
for (const [ predicate, header ] of this.headerMap) {
const terms = input.metadata.getAll(predicate);
if (terms.length > 0) {
addHeader(input.response, header, terms.map((term): string => term.value));
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/unit/ldp/http/metadata/ConstantMetadataWriter.test.ts
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' ]});
});
});

0 comments on commit fe3957f

Please sign in to comment.