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