fix: Only check DataAccessor canHandle call for Documents

This commit is contained in:
Joachim Van Herwegen 2021-09-13 15:25:43 +02:00
parent 0271133d33
commit a1c3633a25
2 changed files with 20 additions and 13 deletions

View File

@ -141,9 +141,6 @@ export class DataAccessorBasedStore implements ResourceStore {
Promise<ResourceIdentifier> {
this.validateIdentifier(container);
// Ensure the representation is supported by the accessor
await this.accessor.canHandle(representation);
const parentMetadata = await this.getSafeNormalizedMetadata(container);
// Solid, §5.3: "When a POST method request targets a resource without an existing representation,
@ -168,9 +165,16 @@ export class DataAccessorBasedStore implements ResourceStore {
// Clients who want the server to assign a URI of a resource, MUST use the POST request."
// https://solid.github.io/specification/protocol#resource-type-heuristics
const newID = await this.createSafeUri(container, representation.metadata);
const isContainer = isContainerIdentifier(newID);
// Ensure the representation is supported by the accessor
// Containers are not checked because uploaded representations are treated as metadata
if (!isContainer) {
await this.accessor.canHandle(representation);
}
// Write the data. New containers should never be made for a POST request.
await this.writeData(newID, representation, isContainerIdentifier(newID), false, false);
await this.writeData(newID, representation, isContainer, false, false);
return newID;
}
@ -179,9 +183,6 @@ export class DataAccessorBasedStore implements ResourceStore {
conditions?: Conditions): Promise<ResourceIdentifier[]> {
this.validateIdentifier(identifier);
// Ensure the representation is supported by the accessor
await this.accessor.canHandle(representation);
// Check if the resource already exists
const oldMetadata = await this.getSafeNormalizedMetadata(identifier);
@ -202,6 +203,12 @@ export class DataAccessorBasedStore implements ResourceStore {
throw new BadRequestHttpError('Containers should have a `/` at the end of their path, resources should not.');
}
// Ensure the representation is supported by the accessor
// Containers are not checked because uploaded representations are treated as metadata
if (!isContainer) {
await this.accessor.canHandle(representation);
}
this.validateConditions(conditions, oldMetadata);
// Potentially have to create containers if it didn't exist yet

View File

@ -214,12 +214,6 @@ describe('A DataAccessorBasedStore', (): void => {
.rejects.toThrow(NotFoundHttpError);
});
it('checks if the DataAccessor supports the data.', async(): Promise<void> => {
const resourceID = { path: `${root}container/` };
representation.binary = false;
await expect(store.addResource(resourceID, representation)).rejects.toThrow(BadRequestHttpError);
});
it('will 404 if the target does not exist.', async(): Promise<void> => {
const resourceID = { path: `${root}container/` };
await expect(store.addResource(resourceID, representation)).rejects.toThrow(NotFoundHttpError);
@ -241,6 +235,12 @@ describe('A DataAccessorBasedStore', (): void => {
await expect(result).rejects.toThrow('The given path is not a container.');
});
it('checks if the DataAccessor supports the data.', async(): Promise<void> => {
const resourceID = { path: root };
representation.binary = false;
await expect(store.addResource(resourceID, representation)).rejects.toThrow(BadRequestHttpError);
});
it('throws a 412 if the conditions are not matched.', async(): Promise<void> => {
const resourceID = { path: root };
const conditions = new BasicConditions({ notMatchesETag: [ '*' ]});