From 5613ff9e71a31c1d75589e7bcabfb6209e397902 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Mon, 11 Oct 2021 15:21:40 +0200 Subject: [PATCH] fix: Let Representations always have a body This is relevant when the request has a content-type but no data. --- .../permissions/SparqlPatchModesExtractor.ts | 3 --- src/http/Operation.ts | 4 ++-- src/http/input/body/BodyParser.ts | 2 +- src/http/input/body/RawBodyParser.ts | 4 ++-- src/http/input/body/SparqlUpdateBodyParser.ts | 1 + src/http/ldp/PatchOperationHandler.ts | 6 +++--- src/http/ldp/PostOperationHandler.ts | 6 +++--- src/http/ldp/PutOperationHandler.ts | 6 +++--- src/http/representation/BasicRepresentation.ts | 18 ++++++++++++++++-- src/http/representation/Representation.ts | 6 ++++++ src/identity/IdentityProviderHttpHandler.ts | 7 +++---- src/identity/interaction/SessionHttpHandler.ts | 2 +- .../handler/ForgotPasswordHandler.ts | 2 +- .../email-password/handler/LoginHandler.ts | 2 +- .../handler/RegistrationHandler.ts | 2 +- .../handler/ResetPasswordHandler.ts | 2 +- src/init/setup/SetupHttpHandler.ts | 2 +- test/integration/GuardedStream.test.ts | 2 +- test/integration/RequestParser.test.ts | 2 +- .../SparqlPatchModesExtractor.test.ts | 4 ---- .../unit/http/input/body/RawBodyParser.test.ts | 14 ++++++++------ .../http/ldp/DeleteOperationHandler.test.ts | 4 +++- test/unit/http/ldp/GetOperationHandler.test.ts | 4 +++- .../unit/http/ldp/HeadOperationHandler.test.ts | 4 +++- .../http/ldp/PatchOperationHandler.test.ts | 6 ++---- .../unit/http/ldp/PostOperationHandler.test.ts | 6 ++---- test/unit/http/ldp/PutOperationHandler.test.ts | 6 ++---- .../metadata/WebAclMetadataCollector.test.ts | 2 ++ .../IdentityProviderHttpHandler.test.ts | 7 ++++++- test/unit/init/setup/SetupHttpHandler.test.ts | 1 + .../unit/server/AuthorizingHttpHandler.test.ts | 2 ++ test/unit/server/ParsingHttpHandler.test.ts | 4 +++- .../storage/DataAccessorBasedStore.test.ts | 1 + .../accessors/SparqlDataAccessor.test.ts | 11 +++++++---- .../storage/patch/SparqlUpdatePatcher.test.ts | 1 + .../routing/ConvertingRouterRule.test.ts | 2 +- 36 files changed, 95 insertions(+), 63 deletions(-) diff --git a/src/authorization/permissions/SparqlPatchModesExtractor.ts b/src/authorization/permissions/SparqlPatchModesExtractor.ts index 1d14b46b4..cad78bc52 100644 --- a/src/authorization/permissions/SparqlPatchModesExtractor.ts +++ b/src/authorization/permissions/SparqlPatchModesExtractor.ts @@ -11,9 +11,6 @@ export class SparqlPatchModesExtractor extends ModesExtractor { if (method !== 'PATCH') { throw new NotImplementedHttpError(`Cannot determine permissions of ${method}, only PATCH.`); } - if (!body) { - throw new NotImplementedHttpError('Cannot determine permissions of PATCH operations without a body.'); - } if (!this.isSparql(body)) { throw new NotImplementedHttpError('Cannot determine permissions of non-SPARQL patches.'); } diff --git a/src/http/Operation.ts b/src/http/Operation.ts index 4fe768afe..cde1388e8 100644 --- a/src/http/Operation.ts +++ b/src/http/Operation.ts @@ -29,7 +29,7 @@ export interface Operation { */ permissionSet?: PermissionSet; /** - * Optional representation of the body. + * Representation of the body and metadata headers. */ - body?: Representation; + body: Representation; } diff --git a/src/http/input/body/BodyParser.ts b/src/http/input/body/BodyParser.ts index 1f20f2dcf..3ff3a5626 100644 --- a/src/http/input/body/BodyParser.ts +++ b/src/http/input/body/BodyParser.ts @@ -19,4 +19,4 @@ export interface BodyParserArgs { * Parses the body of an incoming {@link HttpRequest} and converts it to a {@link Representation}. */ export abstract class BodyParser extends - AsyncHandler {} + AsyncHandler {} diff --git a/src/http/input/body/RawBodyParser.ts b/src/http/input/body/RawBodyParser.ts index 08f09bded..8d81d5403 100644 --- a/src/http/input/body/RawBodyParser.ts +++ b/src/http/input/body/RawBodyParser.ts @@ -13,7 +13,7 @@ export class RawBodyParser extends BodyParser { // Note that the only reason this is a union is in case the body is empty. // If this check gets moved away from the BodyParsers this union could be removed - public async handle({ request, metadata }: BodyParserArgs): Promise { + public async handle({ request, metadata }: BodyParserArgs): Promise { const { 'content-type': contentType, 'content-length': contentLength, @@ -26,7 +26,7 @@ export class RawBodyParser extends BodyParser { // some still provide a Content-Length of 0 (but without Content-Type). if ((!contentLength || (/^0+$/u.test(contentLength) && !contentType)) && !transferEncoding) { this.logger.debug('HTTP request does not have a body, or its empty body is missing a Content-Type header'); - return; + return new BasicRepresentation([], metadata); } // While RFC7231 allows treating a body without content type as an octet stream, diff --git a/src/http/input/body/SparqlUpdateBodyParser.ts b/src/http/input/body/SparqlUpdateBodyParser.ts index 2e5333f01..b9600769a 100644 --- a/src/http/input/body/SparqlUpdateBodyParser.ts +++ b/src/http/input/body/SparqlUpdateBodyParser.ts @@ -38,6 +38,7 @@ export class SparqlUpdateBodyParser extends BodyParser { binary: true, data: guardedStreamFrom(sparql), metadata, + isEmpty: false, }; } } diff --git a/src/http/ldp/PatchOperationHandler.ts b/src/http/ldp/PatchOperationHandler.ts index 428824474..f5393c60d 100644 --- a/src/http/ldp/PatchOperationHandler.ts +++ b/src/http/ldp/PatchOperationHandler.ts @@ -32,9 +32,9 @@ export class PatchOperationHandler extends OperationHandler { // Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests // without the Content-Type header with a status code of 400." // https://solid.github.io/specification/protocol#http-server - if (!operation.body?.metadata.contentType) { - this.logger.warn('No Content-Type header specified on PATCH request'); - throw new BadRequestHttpError('No Content-Type header specified on PATCH request'); + if (!operation.body.metadata.contentType) { + this.logger.warn('PATCH requests require the Content-Type header to be set'); + throw new BadRequestHttpError('PATCH requests require the Content-Type header to be set'); } await this.store.modifyResource(operation.target, operation.body as Patch, operation.conditions); return new ResetResponseDescription(); diff --git a/src/http/ldp/PostOperationHandler.ts b/src/http/ldp/PostOperationHandler.ts index b9e9e2425..12bf648dc 100644 --- a/src/http/ldp/PostOperationHandler.ts +++ b/src/http/ldp/PostOperationHandler.ts @@ -31,9 +31,9 @@ export class PostOperationHandler extends OperationHandler { // Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests // without the Content-Type header with a status code of 400." // https://solid.github.io/specification/protocol#http-server - if (!operation.body?.metadata.contentType) { - this.logger.warn('No Content-Type header specified on POST request'); - throw new BadRequestHttpError('No Content-Type header specified on POST request'); + if (!operation.body.metadata.contentType) { + this.logger.warn('POST requests require the Content-Type header to be set'); + throw new BadRequestHttpError('POST requests require the Content-Type header to be set'); } const identifier = await this.store.addResource(operation.target, operation.body, operation.conditions); return new CreatedResponseDescription(identifier); diff --git a/src/http/ldp/PutOperationHandler.ts b/src/http/ldp/PutOperationHandler.ts index b256021c4..086da2985 100644 --- a/src/http/ldp/PutOperationHandler.ts +++ b/src/http/ldp/PutOperationHandler.ts @@ -31,9 +31,9 @@ export class PutOperationHandler extends OperationHandler { // Solid, §2.1: "A Solid server MUST reject PUT, POST and PATCH requests // without the Content-Type header with a status code of 400." // https://solid.github.io/specification/protocol#http-server - if (!operation.body?.metadata.contentType) { - this.logger.warn('No Content-Type header specified on PUT request'); - throw new BadRequestHttpError('No Content-Type header specified on PUT request'); + if (!operation.body.metadata.contentType) { + this.logger.warn('PUT requests require the Content-Type header to be set'); + throw new BadRequestHttpError('PUT requests require the Content-Type header to be set'); } await this.store.setRepresentation(operation.target, operation.body, operation.conditions); return new ResetResponseDescription(); diff --git a/src/http/representation/BasicRepresentation.ts b/src/http/representation/BasicRepresentation.ts index 7c9a93f62..f83f13108 100644 --- a/src/http/representation/BasicRepresentation.ts +++ b/src/http/representation/BasicRepresentation.ts @@ -24,6 +24,11 @@ export class BasicRepresentation implements Representation { public readonly metadata: RepresentationMetadata; public readonly binary: boolean; + /** + * An empty Representation + */ + public constructor(); + /** * @param data - The representation data * @param metadata - The representation metadata @@ -86,13 +91,15 @@ export class BasicRepresentation implements Representation { ); public constructor( - data: Readable | any[] | string, - metadata: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string, + data?: Readable | any[] | string, + metadata?: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string, metadataRest?: MetadataRecord | string | boolean, binary?: boolean, ) { if (typeof data === 'string' || Array.isArray(data)) { data = guardedStreamFrom(data); + } else if (!data) { + data = guardedStreamFrom([]); } this.data = guardStream(data); @@ -110,4 +117,11 @@ export class BasicRepresentation implements Representation { } this.binary = binary; } + + /** + * Data should only be interpreted if there is a content type. + */ + public get isEmpty(): boolean { + return !this.metadata.contentType; + } } diff --git a/src/http/representation/Representation.ts b/src/http/representation/Representation.ts index 1020ca566..b2d9ba429 100644 --- a/src/http/representation/Representation.ts +++ b/src/http/representation/Representation.ts @@ -19,4 +19,10 @@ export interface Representation { * (as opposed to complex objects). */ binary: boolean; + /** + * Whether the data stream is empty. + * This being true does not imply that the data stream has a length of more than 0, + * only that it is a possibility and should be read to be sure. + */ + isEmpty: boolean; } diff --git a/src/identity/IdentityProviderHttpHandler.ts b/src/identity/IdentityProviderHttpHandler.ts index b07b6a981..5d9f416b2 100644 --- a/src/identity/IdentityProviderHttpHandler.ts +++ b/src/identity/IdentityProviderHttpHandler.ts @@ -3,7 +3,6 @@ import type { ErrorHandler } from '../http/output/error/ErrorHandler'; import { RedirectResponseDescription } from '../http/output/response/RedirectResponseDescription'; import { ResponseDescription } from '../http/output/response/ResponseDescription'; import { BasicRepresentation } from '../http/representation/BasicRepresentation'; -import type { Representation } from '../http/representation/Representation'; import { getLoggerFor } from '../logging/LogUtil'; import type { HttpRequest } from '../server/HttpRequest'; import type { OperationHttpHandlerInput } from '../server/OperationHttpHandler'; @@ -124,10 +123,10 @@ export class IdentityProviderHttpHandler extends OperationHttpHandler { } // Cloning input data so it can be sent back in case of errors - let clone: Representation | undefined; + let clone = operation.body; // IDP handlers expect JSON data - if (operation.body) { + if (!operation.body.isEmpty) { const args = { representation: operation.body, preferences: { type: { [APPLICATION_JSON]: 1 }}, @@ -187,7 +186,7 @@ export class IdentityProviderHttpHandler extends OperationHttpHandler { const details = await readJsonStream(response.data!); // Add the input data to the JSON response; - if (operation.body) { + if (!operation.body.isEmpty) { details.prefilled = await readJsonStream(operation.body.data); // Don't send passwords back diff --git a/src/identity/interaction/SessionHttpHandler.ts b/src/identity/interaction/SessionHttpHandler.ts index 4727476c6..3633ba6c9 100644 --- a/src/identity/interaction/SessionHttpHandler.ts +++ b/src/identity/interaction/SessionHttpHandler.ts @@ -12,7 +12,7 @@ export class SessionHttpHandler extends InteractionHandler { throw new NotImplementedHttpError('Only interactions with a valid session are supported.'); } - const { remember } = await readJsonStream(operation.body!.data); + const { remember } = await readJsonStream(operation.body.data); return { type: 'complete', details: { webId: oidcInteraction.session.accountId, shouldRemember: Boolean(remember) }, diff --git a/src/identity/interaction/email-password/handler/ForgotPasswordHandler.ts b/src/identity/interaction/email-password/handler/ForgotPasswordHandler.ts index 835562c3e..17e68c99a 100644 --- a/src/identity/interaction/email-password/handler/ForgotPasswordHandler.ts +++ b/src/identity/interaction/email-password/handler/ForgotPasswordHandler.ts @@ -39,7 +39,7 @@ export class ForgotPasswordHandler extends InteractionHandler { public async handle({ operation }: InteractionHandlerInput): Promise> { // Validate incoming data - const { email } = await readJsonStream(operation.body!.data); + const { email } = await readJsonStream(operation.body.data); assert(typeof email === 'string' && email.length > 0, 'Email required'); await this.resetPassword(email); diff --git a/src/identity/interaction/email-password/handler/LoginHandler.ts b/src/identity/interaction/email-password/handler/LoginHandler.ts index f85faab66..3be6c0a59 100644 --- a/src/identity/interaction/email-password/handler/LoginHandler.ts +++ b/src/identity/interaction/email-password/handler/LoginHandler.ts @@ -43,7 +43,7 @@ export class LoginHandler extends InteractionHandler { */ private async parseInput(operation: Operation): Promise<{ email: string; password: string; remember: boolean }> { const prefilled: Record = {}; - const { email, password, remember } = await readJsonStream(operation.body!.data); + const { email, password, remember } = await readJsonStream(operation.body.data); assert(typeof email === 'string' && email.length > 0, 'Email required'); prefilled.email = email; assert(typeof password === 'string' && password.length > 0, 'Password required'); diff --git a/src/identity/interaction/email-password/handler/RegistrationHandler.ts b/src/identity/interaction/email-password/handler/RegistrationHandler.ts index 22985fc67..e91251606 100644 --- a/src/identity/interaction/email-password/handler/RegistrationHandler.ts +++ b/src/identity/interaction/email-password/handler/RegistrationHandler.ts @@ -19,7 +19,7 @@ export class RegistrationHandler extends InteractionHandler { public async handle({ operation }: InteractionHandlerInput): Promise> { - const data = await readJsonStream(operation.body!.data); + const data = await readJsonStream(operation.body.data); const validated = this.registrationManager.validateInput(data, false); const details = await this.registrationManager.register(validated, false); return { type: 'response', details }; diff --git a/src/identity/interaction/email-password/handler/ResetPasswordHandler.ts b/src/identity/interaction/email-password/handler/ResetPasswordHandler.ts index 27a0917d5..07f721a5d 100644 --- a/src/identity/interaction/email-password/handler/ResetPasswordHandler.ts +++ b/src/identity/interaction/email-password/handler/ResetPasswordHandler.ts @@ -24,7 +24,7 @@ export class ResetPasswordHandler extends InteractionHandler { // Extract record ID from request URL const recordId = /\/([^/]+)$/u.exec(operation.target.path)?.[1]; // Validate input data - const { password, confirmPassword } = await readJsonStream(operation.body!.data); + const { password, confirmPassword } = await readJsonStream(operation.body.data); assert( typeof recordId === 'string' && recordId.length > 0, 'Invalid request. Open the link from your email again', diff --git a/src/init/setup/SetupHttpHandler.ts b/src/init/setup/SetupHttpHandler.ts index b8ace31e3..ffb3a1c79 100644 --- a/src/init/setup/SetupHttpHandler.ts +++ b/src/init/setup/SetupHttpHandler.ts @@ -159,7 +159,7 @@ export class SetupHttpHandler extends OperationHttpHandler { // Registration manager expects JSON data let json: SetupInput = {}; - if (operation.body) { + if (!operation.body.isEmpty) { const args = { representation: operation.body, preferences: { type: { [APPLICATION_JSON]: 1 }}, diff --git a/test/integration/GuardedStream.test.ts b/test/integration/GuardedStream.test.ts index 6e84edb42..d1f478de7 100644 --- a/test/integration/GuardedStream.test.ts +++ b/test/integration/GuardedStream.test.ts @@ -37,7 +37,7 @@ class DummyConverter extends TypedRepresentationConverter { const data = guardedStreamFrom([ 'dummy' ]); const metadata = new RepresentationMetadata(representation.metadata, 'x/x'); - return { binary: true, data, metadata }; + return { binary: true, data, metadata, isEmpty: false }; } } diff --git a/test/integration/RequestParser.test.ts b/test/integration/RequestParser.test.ts index 495231e07..3503fc15b 100644 --- a/test/integration/RequestParser.test.ts +++ b/test/integration/RequestParser.test.ts @@ -55,7 +55,7 @@ describe('A BasicRequestParser with simple input parsers', (): void => { }); expect(result.body?.metadata.contentType).toEqual('text/turtle'); - await expect(arrayifyStream(result.body!.data)).resolves.toEqual( + await expect(arrayifyStream(result.body.data)).resolves.toEqual( [ ' .' ], ); }); diff --git a/test/unit/authorization/permissions/SparqlPatchModesExtractor.test.ts b/test/unit/authorization/permissions/SparqlPatchModesExtractor.test.ts index 64bf1176f..9fdf97a1f 100644 --- a/test/unit/authorization/permissions/SparqlPatchModesExtractor.test.ts +++ b/test/unit/authorization/permissions/SparqlPatchModesExtractor.test.ts @@ -19,10 +19,6 @@ describe('A SparqlPatchModesExtractor', (): void => { await expect(result).rejects.toThrow(NotImplementedHttpError); await expect(result).rejects.toThrow('Cannot determine permissions of GET, only PATCH.'); - result = extractor.canHandle({ ...operation, body: undefined }); - await expect(result).rejects.toThrow(NotImplementedHttpError); - await expect(result).rejects.toThrow('Cannot determine permissions of PATCH operations without a body.'); - result = extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch }); await expect(result).rejects.toThrow(NotImplementedHttpError); await expect(result).rejects.toThrow('Cannot determine permissions of non-SPARQL patches.'); diff --git a/test/unit/http/input/body/RawBodyParser.test.ts b/test/unit/http/input/body/RawBodyParser.test.ts index 1ec76b30c..8b9b29bc1 100644 --- a/test/unit/http/input/body/RawBodyParser.test.ts +++ b/test/unit/http/input/body/RawBodyParser.test.ts @@ -19,16 +19,18 @@ describe('A RawBodyparser', (): void => { }); it('returns empty output if there is no content length or transfer encoding.', async(): Promise => { - input.request = guardedStreamFrom([ '' ]) as HttpRequest; + input.request = guardedStreamFrom([ 'data' ]) as HttpRequest; input.request.headers = {}; - await expect(bodyParser.handle(input)).resolves.toBeUndefined(); + const result = await bodyParser.handle(input); + await expect(arrayifyStream(result.data)).resolves.toEqual([]); }); // https://github.com/solid/community-server/issues/498 it('returns empty output if the content length is 0 and there is no content type.', async(): Promise => { - input.request = guardedStreamFrom([ '' ]) as HttpRequest; + input.request = guardedStreamFrom([ 'data' ]) as HttpRequest; input.request.headers = { 'content-length': '0' }; - await expect(bodyParser.handle(input)).resolves.toBeUndefined(); + const result = await bodyParser.handle(input); + await expect(arrayifyStream(result.data)).resolves.toEqual([]); }); it('errors when a content length is specified without content type.', async(): Promise => { @@ -48,7 +50,7 @@ describe('A RawBodyparser', (): void => { it('returns a Representation if there is empty data.', async(): Promise => { input.request = guardedStreamFrom([]) as HttpRequest; input.request.headers = { 'content-length': '0', 'content-type': 'text/turtle' }; - const result = (await bodyParser.handle(input))!; + const result = await bodyParser.handle(input); expect(result).toEqual({ binary: true, data: input.request, @@ -60,7 +62,7 @@ describe('A RawBodyparser', (): void => { it('returns a Representation if there is non-empty data.', async(): Promise => { input.request = guardedStreamFrom([ ' .' ]) as HttpRequest; input.request.headers = { 'transfer-encoding': 'chunked', 'content-type': 'text/turtle' }; - const result = (await bodyParser.handle(input))!; + const result = await bodyParser.handle(input); expect(result).toEqual({ binary: true, data: input.request, diff --git a/test/unit/http/ldp/DeleteOperationHandler.test.ts b/test/unit/http/ldp/DeleteOperationHandler.test.ts index 27512a9c8..e16d25590 100644 --- a/test/unit/http/ldp/DeleteOperationHandler.test.ts +++ b/test/unit/http/ldp/DeleteOperationHandler.test.ts @@ -1,5 +1,6 @@ import { DeleteOperationHandler } from '../../../../src/http/ldp/DeleteOperationHandler'; import type { Operation } from '../../../../src/http/Operation'; +import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; import { BasicConditions } from '../../../../src/storage/BasicConditions'; import type { ResourceStore } from '../../../../src/storage/ResourceStore'; import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError'; @@ -7,10 +8,11 @@ import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplemen describe('A DeleteOperationHandler', (): void => { let operation: Operation; const conditions = new BasicConditions({}); + const body = new BasicRepresentation(); const store = {} as unknown as ResourceStore; const handler = new DeleteOperationHandler(store); beforeEach(async(): Promise => { - operation = { method: 'DELETE', target: { path: 'http://test.com/foo' }, preferences: {}, conditions }; + operation = { method: 'DELETE', target: { path: 'http://test.com/foo' }, preferences: {}, conditions, body }; store.deleteResource = jest.fn(async(): Promise => undefined); }); diff --git a/test/unit/http/ldp/GetOperationHandler.test.ts b/test/unit/http/ldp/GetOperationHandler.test.ts index b0ac4b5d0..b667c70fc 100644 --- a/test/unit/http/ldp/GetOperationHandler.test.ts +++ b/test/unit/http/ldp/GetOperationHandler.test.ts @@ -1,5 +1,6 @@ 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 { BasicConditions } from '../../../../src/storage/BasicConditions'; import type { ResourceStore } from '../../../../src/storage/ResourceStore'; @@ -9,11 +10,12 @@ describe('A GetOperationHandler', (): void => { let operation: Operation; const conditions = new BasicConditions({}); const preferences = {}; + const body = new BasicRepresentation(); let store: ResourceStore; let handler: GetOperationHandler; beforeEach(async(): Promise => { - operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions }; + operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, conditions, body }; store = { getRepresentation: jest.fn(async(): Promise => ({ binary: false, data: 'data', metadata: 'metadata' } as any)), diff --git a/test/unit/http/ldp/HeadOperationHandler.test.ts b/test/unit/http/ldp/HeadOperationHandler.test.ts index 4a345e93f..141c87715 100644 --- a/test/unit/http/ldp/HeadOperationHandler.test.ts +++ b/test/unit/http/ldp/HeadOperationHandler.test.ts @@ -1,6 +1,7 @@ import type { Readable } from 'stream'; import { HeadOperationHandler } from '../../../../src/http/ldp/HeadOperationHandler'; import type { Operation } from '../../../../src/http/Operation'; +import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; import type { Representation } from '../../../../src/http/representation/Representation'; import { BasicConditions } from '../../../../src/storage/BasicConditions'; import type { ResourceStore } from '../../../../src/storage/ResourceStore'; @@ -10,12 +11,13 @@ describe('A HeadOperationHandler', (): void => { let operation: Operation; const conditions = new BasicConditions({}); const preferences = {}; + const body = new BasicRepresentation(); let store: ResourceStore; let handler: HeadOperationHandler; let data: Readable; beforeEach(async(): Promise => { - operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions }; + operation = { method: 'HEAD', target: { path: 'http://test.com/foo' }, preferences, conditions, body }; data = { destroy: jest.fn() } as any; store = { getRepresentation: jest.fn(async(): Promise => diff --git a/test/unit/http/ldp/PatchOperationHandler.test.ts b/test/unit/http/ldp/PatchOperationHandler.test.ts index 6f029fc3e..d782c5d70 100644 --- a/test/unit/http/ldp/PatchOperationHandler.test.ts +++ b/test/unit/http/ldp/PatchOperationHandler.test.ts @@ -25,10 +25,8 @@ describe('A PatchOperationHandler', (): void => { await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError); }); - it('errors if there is no body or content-type.', async(): Promise => { - operation.body!.metadata.contentType = undefined; - await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); - delete operation.body; + it('errors if there is no content-type.', async(): Promise => { + operation.body.metadata.contentType = undefined; await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); }); diff --git a/test/unit/http/ldp/PostOperationHandler.test.ts b/test/unit/http/ldp/PostOperationHandler.test.ts index 5ecf165d4..d75230950 100644 --- a/test/unit/http/ldp/PostOperationHandler.test.ts +++ b/test/unit/http/ldp/PostOperationHandler.test.ts @@ -32,10 +32,8 @@ describe('A PostOperationHandler', (): void => { await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError); }); - it('errors if there is no body or content-type.', async(): Promise => { - operation.body!.metadata.contentType = undefined; - await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); - delete operation.body; + it('errors if there is no content-type.', async(): Promise => { + operation.body.metadata.contentType = undefined; await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); }); diff --git a/test/unit/http/ldp/PutOperationHandler.test.ts b/test/unit/http/ldp/PutOperationHandler.test.ts index 74d327b5f..6f9c81221 100644 --- a/test/unit/http/ldp/PutOperationHandler.test.ts +++ b/test/unit/http/ldp/PutOperationHandler.test.ts @@ -26,10 +26,8 @@ describe('A PutOperationHandler', (): void => { await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError); }); - it('errors if there is no body or content-type.', async(): Promise => { - operation.body!.metadata.contentType = undefined; - await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); - delete operation.body; + it('errors if there is no content-type.', async(): Promise => { + operation.body.metadata.contentType = undefined; await expect(handler.handle({ operation })).rejects.toThrow(BadRequestHttpError); }); diff --git a/test/unit/http/ldp/metadata/WebAclMetadataCollector.test.ts b/test/unit/http/ldp/metadata/WebAclMetadataCollector.test.ts index 053c6133f..c4c865b73 100644 --- a/test/unit/http/ldp/metadata/WebAclMetadataCollector.test.ts +++ b/test/unit/http/ldp/metadata/WebAclMetadataCollector.test.ts @@ -3,6 +3,7 @@ import { CredentialGroup } from '../../../../../src/authentication/Credentials'; import type { AclPermission } from '../../../../../src/authorization/permissions/AclPermission'; import { WebAclMetadataCollector } from '../../../../../src/http/ldp/metadata/WebAclMetadataCollector'; import type { Operation } from '../../../../../src/http/Operation'; +import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation'; import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata'; import { ACL, AUTH } from '../../../../../src/util/Vocabularies'; @@ -16,6 +17,7 @@ describe('A WebAclMetadataCollector', (): void => { method: 'GET', target: { path: 'http://test.com/foo' }, preferences: {}, + body: new BasicRepresentation(), }; metadata = new RepresentationMetadata(); diff --git a/test/unit/identity/IdentityProviderHttpHandler.test.ts b/test/unit/identity/IdentityProviderHttpHandler.test.ts index 7007a71ec..d11f8f367 100644 --- a/test/unit/identity/IdentityProviderHttpHandler.test.ts +++ b/test/unit/identity/IdentityProviderHttpHandler.test.ts @@ -38,7 +38,12 @@ describe('An IdentityProviderHttpHandler', (): void => { let handler: IdentityProviderHttpHandler; beforeEach(async(): Promise => { - operation = { method: 'GET', target: { path: 'http://test.com/idp' }, preferences: { type: { 'text/html': 1 }}}; + operation = { + method: 'GET', + target: { path: 'http://test.com/idp' }, + preferences: { type: { 'text/html': 1 }}, + body: new BasicRepresentation(), + }; provider = { callback: jest.fn(), diff --git a/test/unit/init/setup/SetupHttpHandler.test.ts b/test/unit/init/setup/SetupHttpHandler.test.ts index ae6ba9fdc..d58bcf510 100644 --- a/test/unit/init/setup/SetupHttpHandler.test.ts +++ b/test/unit/init/setup/SetupHttpHandler.test.ts @@ -43,6 +43,7 @@ describe('A SetupHttpHandler', (): void => { method: 'GET', target: { path: 'http://test.com/setup' }, preferences: { type: { 'text/html': 1 }}, + body: new BasicRepresentation(), }; errorHandler = { handleSafe: jest.fn(({ error }: ErrorHandlerArgs): ResponseDescription => ({ diff --git a/test/unit/server/AuthorizingHttpHandler.test.ts b/test/unit/server/AuthorizingHttpHandler.test.ts index e2c0f5696..7d11ba6b8 100644 --- a/test/unit/server/AuthorizingHttpHandler.test.ts +++ b/test/unit/server/AuthorizingHttpHandler.test.ts @@ -5,6 +5,7 @@ import type { PermissionReader } from '../../../src/authorization/PermissionRead import type { ModesExtractor } from '../../../src/authorization/permissions/ModesExtractor'; import { AccessMode } from '../../../src/authorization/permissions/Permissions'; import type { Operation } from '../../../src/http/Operation'; +import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation'; import { AuthorizingHttpHandler } from '../../../src/server/AuthorizingHttpHandler'; import type { HttpRequest } from '../../../src/server/HttpRequest'; import type { HttpResponse } from '../../../src/server/HttpResponse'; @@ -30,6 +31,7 @@ describe('An AuthorizingHttpHandler', (): void => { target: { path: 'http://test.com/foo' }, method: 'GET', preferences: {}, + body: new BasicRepresentation(), }; credentialsExtractor = { diff --git a/test/unit/server/ParsingHttpHandler.test.ts b/test/unit/server/ParsingHttpHandler.test.ts index cc94be1ba..132949222 100644 --- a/test/unit/server/ParsingHttpHandler.test.ts +++ b/test/unit/server/ParsingHttpHandler.test.ts @@ -5,6 +5,7 @@ import type { ErrorHandler } from '../../../src/http/output/error/ErrorHandler'; import { OkResponseDescription } from '../../../src/http/output/response/OkResponseDescription'; import { ResponseDescription } from '../../../src/http/output/response/ResponseDescription'; import type { ResponseWriter } from '../../../src/http/output/ResponseWriter'; +import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation'; import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata'; import type { HttpRequest } from '../../../src/server/HttpRequest'; import type { HttpResponse } from '../../../src/server/HttpResponse'; @@ -15,7 +16,8 @@ describe('A ParsingHttpHandler', (): void => { const request: HttpRequest = {} as any; const response: HttpResponse = {} as any; const preferences = { type: { 'text/html': 1 }}; - const operation: Operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences }; + const body = new BasicRepresentation(); + const operation: Operation = { method: 'GET', target: { path: 'http://test.com/foo' }, preferences, body }; const errorResponse = new ResponseDescription(400); let requestParser: jest.Mocked; let metadataCollector: jest.Mocked; diff --git a/test/unit/storage/DataAccessorBasedStore.test.ts b/test/unit/storage/DataAccessorBasedStore.test.ts index 355e2fbdd..7fe7c5adc 100644 --- a/test/unit/storage/DataAccessorBasedStore.test.ts +++ b/test/unit/storage/DataAccessorBasedStore.test.ts @@ -164,6 +164,7 @@ describe('A DataAccessorBasedStore', (): void => { metadata: new RepresentationMetadata( { [CONTENT_TYPE]: 'text/plain', [RDF.type]: DataFactory.namedNode(LDP.Resource) }, ), + isEmpty: false, }; }); diff --git a/test/unit/storage/accessors/SparqlDataAccessor.test.ts b/test/unit/storage/accessors/SparqlDataAccessor.test.ts index bed9fc766..cbd7e4f81 100644 --- a/test/unit/storage/accessors/SparqlDataAccessor.test.ts +++ b/test/unit/storage/accessors/SparqlDataAccessor.test.ts @@ -4,6 +4,7 @@ import arrayifyStream from 'arrayify-stream'; import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint'; import { DataFactory } from 'n3'; import type { Quad } from 'rdf-js'; +import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata'; import { SparqlDataAccessor } from '../../../../src/storage/accessors/SparqlDataAccessor'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; @@ -69,11 +70,13 @@ describe('A SparqlDataAccessor', (): void => { }); it('can only handle quad data.', async(): Promise => { - await expect(accessor.canHandle({ binary: true, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError); - metadata.contentType = 'newInternalType'; - await expect(accessor.canHandle({ binary: false, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError); + let representation = new BasicRepresentation(data, metadata, true); + await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError); + representation = new BasicRepresentation(data, 'newInternalType', false); + await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError); + representation = new BasicRepresentation(data, INTERNAL_QUADS, false); metadata.contentType = INTERNAL_QUADS; - await expect(accessor.canHandle({ binary: false, data, metadata })).resolves.toBeUndefined(); + await expect(accessor.canHandle(representation)).resolves.toBeUndefined(); }); it('returns the corresponding quads when data is requested.', async(): Promise => { diff --git a/test/unit/storage/patch/SparqlUpdatePatcher.test.ts b/test/unit/storage/patch/SparqlUpdatePatcher.test.ts index 3b1a18bce..01386217a 100644 --- a/test/unit/storage/patch/SparqlUpdatePatcher.test.ts +++ b/test/unit/storage/patch/SparqlUpdatePatcher.test.ts @@ -19,6 +19,7 @@ function getPatch(query: string): SparqlUpdatePatch { data: guardedStreamFrom(prefixedQuery), metadata: new RepresentationMetadata(), binary: true, + isEmpty: false, }; } diff --git a/test/unit/storage/routing/ConvertingRouterRule.test.ts b/test/unit/storage/routing/ConvertingRouterRule.test.ts index 17b0c2af4..94a49ba79 100644 --- a/test/unit/storage/routing/ConvertingRouterRule.test.ts +++ b/test/unit/storage/routing/ConvertingRouterRule.test.ts @@ -26,7 +26,7 @@ describe('A ConvertingRouterRule', (): void => { rule = new ConvertingRouterRule({ store: store1, supportChecker: checker1 }, defaultStore); metadata = new RepresentationMetadata(); - representation = { binary: true, data: 'data!' as any, metadata }; + representation = { binary: true, data: 'data!' as any, metadata, isEmpty: false }; }); it('returns the corresponding store if it supports the input.', async(): Promise => {