mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Ensure the ETag is representation specific
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import type { Readable } from 'stream';
|
||||
import { GetOperationHandler } from '../../../../src/http/ldp/GetOperationHandler';
|
||||
import type { Operation } from '../../../../src/http/Operation';
|
||||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||
import type { Representation } from '../../../../src/http/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import { BasicConditions } from '../../../../src/storage/BasicConditions';
|
||||
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
import { NotModifiedHttpError } from '../../../../src/util/errors/NotModifiedHttpError';
|
||||
|
||||
describe('A GetOperationHandler', (): void => {
|
||||
let operation: Operation;
|
||||
@@ -13,12 +16,15 @@ describe('A GetOperationHandler', (): void => {
|
||||
const body = new BasicRepresentation();
|
||||
let store: ResourceStore;
|
||||
let handler: GetOperationHandler;
|
||||
let data: Readable;
|
||||
const metadata = new RepresentationMetadata();
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
|
||||
data = { destroy: jest.fn() } as any;
|
||||
store = {
|
||||
getRepresentation: jest.fn(async(): Promise<Representation> =>
|
||||
({ binary: false, data: 'data', metadata: 'metadata' } as any)),
|
||||
({ binary: false, data, metadata } as any)),
|
||||
} as unknown as ResourceStore;
|
||||
|
||||
handler = new GetOperationHandler(store);
|
||||
@@ -33,9 +39,17 @@ describe('A GetOperationHandler', (): void => {
|
||||
it('returns the representation from the store with the correct response.', async(): Promise<void> => {
|
||||
const result = await handler.handle({ operation });
|
||||
expect(result.statusCode).toBe(200);
|
||||
expect(result.metadata).toBe('metadata');
|
||||
expect(result.data).toBe('data');
|
||||
expect(result.metadata).toBe(metadata);
|
||||
expect(result.data).toBe(data);
|
||||
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(store.getRepresentation).toHaveBeenLastCalledWith(operation.target, preferences, conditions);
|
||||
});
|
||||
|
||||
it('returns a 304 if the conditions do not match.', async(): Promise<void> => {
|
||||
operation.conditions = {
|
||||
matchesMetadata: (): boolean => false,
|
||||
};
|
||||
await expect(handler.handle({ operation })).rejects.toThrow(NotModifiedHttpError);
|
||||
expect(data.destroy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,9 +3,11 @@ import { HeadOperationHandler } from '../../../../src/http/ldp/HeadOperationHand
|
||||
import type { Operation } from '../../../../src/http/Operation';
|
||||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
|
||||
import type { Representation } from '../../../../src/http/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import { BasicConditions } from '../../../../src/storage/BasicConditions';
|
||||
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
import { NotModifiedHttpError } from '../../../../src/util/errors/NotModifiedHttpError';
|
||||
|
||||
describe('A HeadOperationHandler', (): void => {
|
||||
let operation: Operation;
|
||||
@@ -15,13 +17,14 @@ describe('A HeadOperationHandler', (): void => {
|
||||
let store: ResourceStore;
|
||||
let handler: HeadOperationHandler;
|
||||
let data: Readable;
|
||||
const metadata = new RepresentationMetadata();
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
|
||||
data = { destroy: jest.fn() } as any;
|
||||
store = {
|
||||
getRepresentation: jest.fn(async(): Promise<Representation> =>
|
||||
({ binary: false, data, metadata: 'metadata' } as any)),
|
||||
({ binary: false, data, metadata } as any)),
|
||||
} as any;
|
||||
|
||||
handler = new HeadOperationHandler(store);
|
||||
@@ -38,10 +41,18 @@ describe('A HeadOperationHandler', (): void => {
|
||||
it('returns the representation from the store with the correct response.', async(): Promise<void> => {
|
||||
const result = await handler.handle({ operation });
|
||||
expect(result.statusCode).toBe(200);
|
||||
expect(result.metadata).toBe('metadata');
|
||||
expect(result.metadata).toBe(metadata);
|
||||
expect(result.data).toBeUndefined();
|
||||
expect(data.destroy).toHaveBeenCalledTimes(1);
|
||||
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(store.getRepresentation).toHaveBeenLastCalledWith(operation.target, preferences, conditions);
|
||||
});
|
||||
|
||||
it('returns a 304 if the conditions do not match.', async(): Promise<void> => {
|
||||
operation.conditions = {
|
||||
matchesMetadata: (): boolean => false,
|
||||
};
|
||||
await expect(handler.handle({ operation })).rejects.toThrow(NotModifiedHttpError);
|
||||
expect(data.destroy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,21 +2,22 @@ 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 { DC } from '../../../../../src/util/Vocabularies';
|
||||
import { CONTENT_TYPE, DC } 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();
|
||||
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/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: `"${new Date(dateTime).getTime()}"`,
|
||||
etag: getETag(metadata),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,71 +1,82 @@
|
||||
import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata';
|
||||
import { BasicConditions } from '../../../src/storage/BasicConditions';
|
||||
import { getETag } from '../../../src/storage/Conditions';
|
||||
import { DC } from '../../../src/util/Vocabularies';
|
||||
import { CONTENT_TYPE, DC } from '../../../src/util/Vocabularies';
|
||||
|
||||
function getMetadata(modified: Date, type = 'application/ld+json'): RepresentationMetadata {
|
||||
return new RepresentationMetadata({
|
||||
[DC.modified]: `${modified.toISOString()}`,
|
||||
[CONTENT_TYPE]: type,
|
||||
});
|
||||
}
|
||||
|
||||
describe('A BasicConditions', (): void => {
|
||||
const now = new Date(2020, 10, 20);
|
||||
const tomorrow = new Date(2020, 10, 21);
|
||||
const yesterday = new Date(2020, 10, 19);
|
||||
const eTags = [ '123456', 'abcdefg' ];
|
||||
const turtleTag = getETag(getMetadata(now, 'text/turtle'))!;
|
||||
const jsonLdTag = getETag(getMetadata(now))!;
|
||||
|
||||
it('copies the input parameters.', async(): Promise<void> => {
|
||||
const eTags = [ '123456', 'abcdefg' ];
|
||||
const options = { matchesETag: eTags, notMatchesETag: eTags, modifiedSince: now, unmodifiedSince: now };
|
||||
expect(new BasicConditions(options)).toMatchObject(options);
|
||||
});
|
||||
|
||||
it('always returns false if notMatchesETag contains *.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ notMatchesETag: [ '*' ]});
|
||||
expect(conditions.matches()).toBe(false);
|
||||
expect(conditions.matchesMetadata(new RepresentationMetadata())).toBe(false);
|
||||
});
|
||||
|
||||
it('requires matchesETag to contain the provided ETag.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ matchesETag: [ '1234' ]});
|
||||
expect(conditions.matches('abcd')).toBe(false);
|
||||
expect(conditions.matches('1234')).toBe(true);
|
||||
it('requires matchesETag to match the provided ETag timestamp.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ matchesETag: [ turtleTag ]});
|
||||
expect(conditions.matchesMetadata(getMetadata(yesterday))).toBe(false);
|
||||
expect(conditions.matchesMetadata(getMetadata(now))).toBe(true);
|
||||
});
|
||||
|
||||
it('requires matchesETag to match the exact provided ETag in strict mode.', async(): Promise<void> => {
|
||||
const turtleConditions = new BasicConditions({ matchesETag: [ turtleTag ]});
|
||||
const jsonLdConditions = new BasicConditions({ matchesETag: [ jsonLdTag ]});
|
||||
expect(turtleConditions.matchesMetadata(getMetadata(now), true)).toBe(false);
|
||||
expect(jsonLdConditions.matchesMetadata(getMetadata(now), true)).toBe(true);
|
||||
});
|
||||
|
||||
it('supports all ETags if matchesETag contains *.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ matchesETag: [ '*' ]});
|
||||
expect(conditions.matches('abcd')).toBe(true);
|
||||
expect(conditions.matches('1234')).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(yesterday))).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(now))).toBe(true);
|
||||
});
|
||||
|
||||
it('requires notMatchesETag to not contain the provided ETag.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ notMatchesETag: [ '1234' ]});
|
||||
expect(conditions.matches('1234')).toBe(false);
|
||||
expect(conditions.matches('abcd')).toBe(true);
|
||||
it('requires notMatchesETag to not match the provided ETag timestamp.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ notMatchesETag: [ turtleTag ]});
|
||||
expect(conditions.matchesMetadata(getMetadata(yesterday))).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(now))).toBe(false);
|
||||
});
|
||||
|
||||
it('requires notMatchesETag to not match the exact provided ETag in strict mode.', async(): Promise<void> => {
|
||||
const turtleConditions = new BasicConditions({ notMatchesETag: [ turtleTag ]});
|
||||
const jsonLdConditions = new BasicConditions({ notMatchesETag: [ jsonLdTag ]});
|
||||
expect(turtleConditions.matchesMetadata(getMetadata(now), true)).toBe(true);
|
||||
expect(jsonLdConditions.matchesMetadata(getMetadata(now), true)).toBe(false);
|
||||
});
|
||||
|
||||
it('requires lastModified to be after modifiedSince.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ modifiedSince: now });
|
||||
expect(conditions.matches(undefined, yesterday)).toBe(false);
|
||||
expect(conditions.matches(undefined, tomorrow)).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(yesterday))).toBe(false);
|
||||
expect(conditions.matchesMetadata(getMetadata(tomorrow))).toBe(true);
|
||||
});
|
||||
|
||||
it('requires lastModified to be before unmodifiedSince.', async(): Promise<void> => {
|
||||
const conditions = new BasicConditions({ unmodifiedSince: now });
|
||||
expect(conditions.matches(undefined, tomorrow)).toBe(false);
|
||||
expect(conditions.matches(undefined, yesterday)).toBe(true);
|
||||
});
|
||||
|
||||
it('can match based on the last modified date in the metadata.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ [DC.modified]: now.toISOString() });
|
||||
const conditions = new BasicConditions({
|
||||
modifiedSince: yesterday,
|
||||
unmodifiedSince: tomorrow,
|
||||
matchesETag: [ getETag(metadata)! ],
|
||||
notMatchesETag: [ '123456' ],
|
||||
});
|
||||
expect(conditions.matchesMetadata(metadata)).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(yesterday))).toBe(true);
|
||||
expect(conditions.matchesMetadata(getMetadata(tomorrow))).toBe(false);
|
||||
});
|
||||
|
||||
it('matches if no date is found in the metadata.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata();
|
||||
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
|
||||
const conditions = new BasicConditions({
|
||||
modifiedSince: yesterday,
|
||||
unmodifiedSince: tomorrow,
|
||||
matchesETag: [ getETag(metadata)! ],
|
||||
notMatchesETag: [ '123456' ],
|
||||
});
|
||||
expect(conditions.matchesMetadata(metadata)).toBe(true);
|
||||
|
||||
@@ -1,17 +1,53 @@
|
||||
import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata';
|
||||
import { getETag } from '../../../src/storage/Conditions';
|
||||
import { DC } from '../../../src/util/Vocabularies';
|
||||
import { getETag, isCurrentETag } from '../../../src/storage/Conditions';
|
||||
import { CONTENT_TYPE, DC } from '../../../src/util/Vocabularies';
|
||||
|
||||
describe('Conditions', (): void => {
|
||||
describe('#getETag', (): void => {
|
||||
it('creates an ETag based on the date last modified.', async(): Promise<void> => {
|
||||
it('creates an ETag based on the date last modified and content-type.', async(): Promise<void> => {
|
||||
const now = new Date();
|
||||
const metadata = new RepresentationMetadata({ [DC.modified]: now.toISOString() });
|
||||
expect(getETag(metadata)).toBe(`"${now.getTime()}"`);
|
||||
const metadata = new RepresentationMetadata({
|
||||
[DC.modified]: now.toISOString(),
|
||||
[CONTENT_TYPE]: 'text/turtle',
|
||||
});
|
||||
expect(getETag(metadata)).toBe(`"${now.getTime()}-text/turtle"`);
|
||||
});
|
||||
|
||||
it('returns undefined if no date was found.', async(): Promise<void> => {
|
||||
it('returns undefined if no date or content-type was found.', async(): Promise<void> => {
|
||||
const now = new Date();
|
||||
expect(getETag(new RepresentationMetadata())).toBeUndefined();
|
||||
expect(getETag(new RepresentationMetadata({ [DC.modified]: now.toISOString() }))).toBeUndefined();
|
||||
expect(getETag(new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' }))).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#isCurrentETag', (): void => {
|
||||
const now = new Date();
|
||||
|
||||
it('compares an ETag with the current resource state.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({
|
||||
[DC.modified]: now.toISOString(),
|
||||
[CONTENT_TYPE]: 'text/turtle',
|
||||
});
|
||||
const eTag = getETag(metadata)!;
|
||||
expect(isCurrentETag(eTag, metadata)).toBe(true);
|
||||
expect(isCurrentETag('"ETag"', metadata)).toBe(false);
|
||||
});
|
||||
|
||||
it('ignores the content-type.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({
|
||||
[DC.modified]: now.toISOString(),
|
||||
[CONTENT_TYPE]: 'text/turtle',
|
||||
});
|
||||
const eTag = getETag(metadata)!;
|
||||
metadata.contentType = 'application/ld+json';
|
||||
expect(isCurrentETag(eTag, metadata)).toBe(true);
|
||||
expect(isCurrentETag('"ETag"', metadata)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false if the metadata has no last modified date.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata();
|
||||
expect(isCurrentETag('"ETag"', metadata)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,7 +19,6 @@ import { ForbiddenHttpError } from '../../../src/util/errors/ForbiddenHttpError'
|
||||
import { MethodNotAllowedHttpError } from '../../../src/util/errors/MethodNotAllowedHttpError';
|
||||
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
|
||||
import { NotImplementedHttpError } from '../../../src/util/errors/NotImplementedHttpError';
|
||||
import { NotModifiedHttpError } from '../../../src/util/errors/NotModifiedHttpError';
|
||||
import { PreconditionFailedHttpError } from '../../../src/util/errors/PreconditionFailedHttpError';
|
||||
import type { Guarded } from '../../../src/util/GuardedStream';
|
||||
import { SingleRootIdentifierStrategy } from '../../../src/util/identifiers/SingleRootIdentifierStrategy';
|
||||
@@ -218,18 +217,6 @@ describe('A DataAccessorBasedStore', (): void => {
|
||||
);
|
||||
expect(result.metadata.contentType).toBe(INTERNAL_QUADS);
|
||||
});
|
||||
|
||||
it('throws a 304 if the request is a read type error.', async(): Promise<void> => {
|
||||
const resourceID = { path: root };
|
||||
const conditions = new BasicConditions({ notMatchesETag: [ '*' ]});
|
||||
await expect(store.getRepresentation(resourceID, undefined, conditions)).rejects.toThrow(NotModifiedHttpError);
|
||||
});
|
||||
|
||||
it('has conditions but throws no error.', async(): Promise<void> => {
|
||||
const resourceID = { path: root };
|
||||
const conditions = new BasicConditions({ matchesETag: [ '*' ]});
|
||||
await expect(store.getRepresentation(resourceID, undefined, conditions)).resolves.toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('adding a Resource', (): void => {
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import 'jest-rdf';
|
||||
import type { Readable } from 'stream';
|
||||
import type { NamedNode, Literal } from 'n3';
|
||||
import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation';
|
||||
import type { Representation } from '../../../src/http/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata';
|
||||
import { addTemplateMetadata, cloneRepresentation, updateModifiedDate } from '../../../src/util/ResourceUtil';
|
||||
import type { Conditions } from '../../../src/storage/Conditions';
|
||||
import { NotModifiedHttpError } from '../../../src/util/errors/NotModifiedHttpError';
|
||||
import type { Guarded } from '../../../src/util/GuardedStream';
|
||||
import {
|
||||
addTemplateMetadata,
|
||||
assertReadConditions,
|
||||
cloneRepresentation,
|
||||
updateModifiedDate,
|
||||
} from '../../../src/util/ResourceUtil';
|
||||
import { CONTENT_TYPE_TERM, DC, SOLID_META, XSD } from '../../../src/util/Vocabularies';
|
||||
|
||||
describe('ResourceUtil', (): void => {
|
||||
@@ -59,4 +68,36 @@ describe('ResourceUtil', (): void => {
|
||||
expect(representation.metadata.contentType).not.toBe(res.metadata.contentType);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#assertReadConditions', (): void => {
|
||||
let data: jest.Mocked<Guarded<Readable>>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
data = {
|
||||
destroy: jest.fn(),
|
||||
} as any;
|
||||
representation.data = data;
|
||||
});
|
||||
|
||||
it('does nothing if the conditions are undefined.', async(): Promise<void> => {
|
||||
expect((): any => assertReadConditions(representation)).not.toThrow();
|
||||
expect(data.destroy).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('does nothing if the conditions match.', async(): Promise<void> => {
|
||||
const conditions: Conditions = {
|
||||
matchesMetadata: (): boolean => true,
|
||||
};
|
||||
expect((): any => assertReadConditions(representation, conditions)).not.toThrow();
|
||||
expect(data.destroy).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('throws a NotModifiedHttpError if the conditions do not match.', async(): Promise<void> => {
|
||||
const conditions: Conditions = {
|
||||
matchesMetadata: (): boolean => false,
|
||||
};
|
||||
expect((): any => assertReadConditions(representation, conditions)).toThrow(NotModifiedHttpError);
|
||||
expect(data.destroy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user