Skip to content

Commit

Permalink
feat: Store ETag in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Aug 28, 2023
1 parent 0245b31 commit b608080
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/http/ldp/GetOperationHandler.ts
@@ -1,6 +1,8 @@
import { getETag } from '../../storage/Conditions';
import type { ResourceStore } from '../../storage/ResourceStore';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { assertReadConditions } from '../../util/ResourceUtil';
import { HH } from '../../util/Vocabularies';
import { OkResponseDescription } from '../output/response/OkResponseDescription';
import type { ResponseDescription } from '../output/response/ResponseDescription';
import type { OperationHandlerInput } from './OperationHandler';
Expand Down Expand Up @@ -30,6 +32,10 @@ export class GetOperationHandler extends OperationHandler {
// Check whether the cached representation is still valid or it is necessary to send a new representation
assertReadConditions(body, operation.conditions);

// Add the ETag of the returned representation
const etag = getETag(body.metadata);
body.metadata.set(HH.terms.etag, etag);

return new OkResponseDescription(body.metadata, body.data);
}
}
6 changes: 6 additions & 0 deletions src/http/ldp/HeadOperationHandler.ts
@@ -1,6 +1,8 @@
import { getETag } from '../../storage/Conditions';
import type { ResourceStore } from '../../storage/ResourceStore';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { assertReadConditions } from '../../util/ResourceUtil';
import { HH } from '../../util/Vocabularies';
import { OkResponseDescription } from '../output/response/OkResponseDescription';
import type { ResponseDescription } from '../output/response/ResponseDescription';
import type { OperationHandlerInput } from './OperationHandler';
Expand Down Expand Up @@ -34,6 +36,10 @@ export class HeadOperationHandler extends OperationHandler {
// Generally it doesn't make much sense to use condition headers with a HEAD request, but it should be supported.
assertReadConditions(body, operation.conditions);

// Add the ETag of the returned representation
const etag = getETag(body.metadata);
body.metadata.set(HH.terms.etag, etag);

return new OkResponseDescription(body.metadata);
}
}
7 changes: 3 additions & 4 deletions src/http/output/metadata/ModifiedMetadataWriter.ts
@@ -1,7 +1,6 @@
import type { HttpResponse } from '../../../server/HttpResponse';
import { getETag } from '../../../storage/Conditions';
import { addHeader } from '../../../util/HeaderUtil';
import { DC } from '../../../util/Vocabularies';
import { DC, HH } from '../../../util/Vocabularies';
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import { MetadataWriter } from './MetadataWriter';

Expand All @@ -15,9 +14,9 @@ export class ModifiedMetadataWriter extends MetadataWriter {
const date = new Date(modified.value);
addHeader(input.response, 'Last-Modified', date.toUTCString());
}
const etag = getETag(input.metadata);
const etag = input.metadata.get(HH.terms.etag);
if (etag) {
addHeader(input.response, 'ETag', etag);
addHeader(input.response, 'ETag', etag.value);
}
}
}
1 change: 1 addition & 0 deletions src/util/Vocabularies.ts
Expand Up @@ -171,6 +171,7 @@ export const FOAF = createVocabulary('http://xmlns.com/foaf/0.1/',

export const HH = createVocabulary('http://www.w3.org/2011/http-headers#',
'content-length',
'etag',
);

export const HTTP = createVocabulary('http://www.w3.org/2011/http#',
Expand Down
8 changes: 7 additions & 1 deletion test/unit/http/ldp/GetOperationHandler.test.ts
Expand Up @@ -5,9 +5,12 @@ import { BasicRepresentation } from '../../../../src/http/representation/BasicRe
import type { Representation } from '../../../../src/http/representation/Representation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import { getETag } from '../../../../src/storage/Conditions';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
import { NotModifiedHttpError } from '../../../../src/util/errors/NotModifiedHttpError';
import { updateModifiedDate } from '../../../../src/util/ResourceUtil';
import { CONTENT_TYPE, HH } from '../../../../src/util/Vocabularies';

describe('A GetOperationHandler', (): void => {
let operation: Operation;
Expand All @@ -17,11 +20,13 @@ describe('A GetOperationHandler', (): void => {
let store: ResourceStore;
let handler: GetOperationHandler;
let data: Readable;
const metadata = new RepresentationMetadata();
let metadata: RepresentationMetadata;

beforeEach(async(): Promise<void> => {
operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
data = { destroy: jest.fn() } as any;
metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
updateModifiedDate(metadata);
store = {
getRepresentation: jest.fn(async(): Promise<Representation> =>
({ binary: false, data, metadata } as any)),
Expand All @@ -40,6 +45,7 @@ describe('A GetOperationHandler', (): void => {
const result = await handler.handle({ operation });
expect(result.statusCode).toBe(200);
expect(result.metadata).toBe(metadata);
expect(metadata.get(HH.terms.etag)?.value).toBe(getETag(metadata));
expect(result.data).toBe(data);
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenLastCalledWith(operation.target, preferences, conditions);
Expand Down
8 changes: 7 additions & 1 deletion test/unit/http/ldp/HeadOperationHandler.test.ts
Expand Up @@ -5,9 +5,12 @@ import { BasicRepresentation } from '../../../../src/http/representation/BasicRe
import type { Representation } from '../../../../src/http/representation/Representation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import { getETag } from '../../../../src/storage/Conditions';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
import { NotModifiedHttpError } from '../../../../src/util/errors/NotModifiedHttpError';
import { updateModifiedDate } from '../../../../src/util/ResourceUtil';
import { CONTENT_TYPE, HH } from '../../../../src/util/Vocabularies';

describe('A HeadOperationHandler', (): void => {
let operation: Operation;
Expand All @@ -17,11 +20,13 @@ describe('A HeadOperationHandler', (): void => {
let store: ResourceStore;
let handler: HeadOperationHandler;
let data: Readable;
const metadata = new RepresentationMetadata();
let metadata: RepresentationMetadata;

beforeEach(async(): Promise<void> => {
operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
data = { destroy: jest.fn() } as any;
metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
updateModifiedDate(metadata);
store = {
getRepresentation: jest.fn(async(): Promise<Representation> =>
({ binary: false, data, metadata } as any)),
Expand All @@ -42,6 +47,7 @@ describe('A HeadOperationHandler', (): void => {
const result = await handler.handle({ operation });
expect(result.statusCode).toBe(200);
expect(result.metadata).toBe(metadata);
expect(metadata.get(HH.terms.etag)?.value).toBe(getETag(metadata));
expect(result.data).toBeUndefined();
expect(data.destroy).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
Expand Down
7 changes: 3 additions & 4 deletions test/unit/http/output/metadata/ModifiedMetadataWriter.test.ts
Expand Up @@ -2,22 +2,21 @@ import { createResponse } from 'node-mocks-http';
import { ModifiedMetadataWriter } from '../../../../../src/http/output/metadata/ModifiedMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { HttpResponse } from '../../../../../src/server/HttpResponse';
import { getETag } from '../../../../../src/storage/Conditions';
import { updateModifiedDate } from '../../../../../src/util/ResourceUtil';
import { CONTENT_TYPE, DC } from '../../../../../src/util/Vocabularies';
import { DC, HH } from '../../../../../src/util/Vocabularies';

describe('A ModifiedMetadataWriter', (): void => {
const writer = new ModifiedMetadataWriter();

it('adds the Last-Modified and ETag header if there is dc:modified metadata.', async(): Promise<void> => {
const response = createResponse() as HttpResponse;
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
const metadata = new RepresentationMetadata({ [HH.etag]: '123456-turtle' });
updateModifiedDate(metadata);
const dateTime = metadata.get(DC.terms.modified)!.value;
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'last-modified': new Date(dateTime).toUTCString(),
etag: getETag(metadata),
etag: '123456-turtle',
});
});

Expand Down

0 comments on commit b608080

Please sign in to comment.