fix: Ensure the ETag is representation specific

This commit is contained in:
Joachim Van Herwegen
2023-03-30 13:23:04 +02:00
parent 46ceb6d7eb
commit c3f48ddb97
14 changed files with 276 additions and 102 deletions

View File

@@ -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);

View File

@@ -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);
});
});
});

View File

@@ -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 => {