mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Store ETag in metadata
This commit is contained in:
parent
0245b31e0c
commit
b608080d5f
@ -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';
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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';
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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';
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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#',
|
||||
|
@ -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;
|
||||
@ -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)),
|
||||
@ -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);
|
||||
|
@ -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;
|
||||
@ -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)),
|
||||
@ -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);
|
||||
|
@ -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',
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user