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