From 1a45b65df702815a65cc6fb539a6687eea5d3194 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Fri, 30 Oct 2020 17:56:39 +0100 Subject: [PATCH] refactor: Implement empty canHandle on base class. (#289) --- src/authentication/UnsecureWebIdExtractor.ts | 4 ---- src/authorization/AllowEverythingAuthorizer.ts | 4 ---- src/authorization/WebAclAuthorizer.ts | 4 ---- src/ldp/http/AcceptPreferenceParser.ts | 4 ---- src/ldp/http/BasicRequestParser.ts | 4 ---- src/ldp/http/BasicTargetExtractor.ts | 4 ---- src/ldp/http/RawBodyParser.ts | 4 ---- src/ldp/http/metadata/BasicMetadataExtractor.ts | 4 ---- src/util/AsyncHandler.ts | 5 ++++- test/unit/authentication/UnsecureWebIdExtractor.ts | 2 +- .../authorization/AllowEverythingAuthorizer.test.ts | 2 +- test/unit/authorization/WebAclAuthorizer.test.ts | 2 +- test/unit/ldp/http/AcceptPreferenceParser.test.ts | 2 +- test/unit/ldp/http/BasicRequestParser.test.ts | 2 +- test/unit/ldp/http/BasicTargetExtractor.test.ts | 2 +- test/unit/ldp/http/RawBodyParser.test.ts | 2 +- .../ldp/http/metadata/BasicMetadataExtractor.test.ts | 2 +- test/unit/server/ExpressHttpServer.test.ts | 4 ---- test/unit/util/AsyncHandler.test.ts | 12 +++++++++++- 19 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/authentication/UnsecureWebIdExtractor.ts b/src/authentication/UnsecureWebIdExtractor.ts index 3a596e9b6..f47b98d67 100644 --- a/src/authentication/UnsecureWebIdExtractor.ts +++ b/src/authentication/UnsecureWebIdExtractor.ts @@ -6,10 +6,6 @@ import { CredentialsExtractor } from './CredentialsExtractor'; * Credentials extractor which simply interprets the contents of the Authorization header as a webID. */ export class UnsecureWebIdExtractor extends CredentialsExtractor { - public async canHandle(): Promise { - // Supports all requests - } - public async handle(input: HttpRequest): Promise { return { webID: input.headers.authorization }; } diff --git a/src/authorization/AllowEverythingAuthorizer.ts b/src/authorization/AllowEverythingAuthorizer.ts index 222ac5178..4a1d5d4c7 100644 --- a/src/authorization/AllowEverythingAuthorizer.ts +++ b/src/authorization/AllowEverythingAuthorizer.ts @@ -4,10 +4,6 @@ import { Authorizer } from './Authorizer'; * Authorizer which allows all access independent of the identifier and requested permissions. */ export class AllowEverythingAuthorizer extends Authorizer { - public async canHandle(): Promise { - // Can handle all requests - } - public async handle(): Promise { // Allows all actions } diff --git a/src/authorization/WebAclAuthorizer.ts b/src/authorization/WebAclAuthorizer.ts index 7bb2c15b4..d7eb32f51 100644 --- a/src/authorization/WebAclAuthorizer.ts +++ b/src/authorization/WebAclAuthorizer.ts @@ -35,10 +35,6 @@ export class WebAclAuthorizer extends Authorizer { this.resourceStore = resourceStore; } - public async canHandle(): Promise { - // Can handle everything - } - /** * Checks if an agent is allowed to execute the requested actions. * Will throw an error if this is not the case. diff --git a/src/ldp/http/AcceptPreferenceParser.ts b/src/ldp/http/AcceptPreferenceParser.ts index 958ad4bed..7756200b2 100644 --- a/src/ldp/http/AcceptPreferenceParser.ts +++ b/src/ldp/http/AcceptPreferenceParser.ts @@ -19,10 +19,6 @@ export class AcceptPreferenceParser extends PreferenceParser { super(); } - public async canHandle(): Promise { - // Supports all HttpRequests - } - public async handle(input: HttpRequest): Promise { const result: RepresentationPreferences = {}; const headers: diff --git a/src/ldp/http/BasicRequestParser.ts b/src/ldp/http/BasicRequestParser.ts index b82a287d3..c16aaf474 100644 --- a/src/ldp/http/BasicRequestParser.ts +++ b/src/ldp/http/BasicRequestParser.ts @@ -31,10 +31,6 @@ export class BasicRequestParser extends RequestParser { Object.assign(this, args); } - public async canHandle(): Promise { - // Can handle all requests - } - public async handle(request: HttpRequest): Promise { const { method } = request; if (!method) { diff --git a/src/ldp/http/BasicTargetExtractor.ts b/src/ldp/http/BasicTargetExtractor.ts index a5905fb17..35ddbc4ce 100644 --- a/src/ldp/http/BasicTargetExtractor.ts +++ b/src/ldp/http/BasicTargetExtractor.ts @@ -13,10 +13,6 @@ import { TargetExtractor } from './TargetExtractor'; export class BasicTargetExtractor extends TargetExtractor { protected readonly logger = getLoggerFor(this); - public async canHandle(): Promise { - // Can handle all URLs - } - public async handle(request: HttpRequest): Promise { if (!request.url) { this.logger.error('The request has no URL'); diff --git a/src/ldp/http/RawBodyParser.ts b/src/ldp/http/RawBodyParser.ts index faf71b2d6..4b893260d 100644 --- a/src/ldp/http/RawBodyParser.ts +++ b/src/ldp/http/RawBodyParser.ts @@ -10,10 +10,6 @@ import { BodyParser } from './BodyParser'; export class RawBodyParser extends BodyParser { protected readonly logger = getLoggerFor(this); - public async canHandle(): Promise { - // All content-types are supported - } - // 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 { diff --git a/src/ldp/http/metadata/BasicMetadataExtractor.ts b/src/ldp/http/metadata/BasicMetadataExtractor.ts index be5d0b24b..dbd361731 100644 --- a/src/ldp/http/metadata/BasicMetadataExtractor.ts +++ b/src/ldp/http/metadata/BasicMetadataExtractor.ts @@ -14,10 +14,6 @@ export class BasicMetadataExtractor extends MetadataExtractor { this.parsers = parsers; } - public async canHandle(): Promise { - // Can handle all requests - } - public async handle(request: HttpRequest): Promise { const metadata = new RepresentationMetadata(); diff --git a/src/util/AsyncHandler.ts b/src/util/AsyncHandler.ts index 27ce2a134..78d9dca22 100644 --- a/src/util/AsyncHandler.ts +++ b/src/util/AsyncHandler.ts @@ -9,7 +9,10 @@ export abstract class AsyncHandler { * * @returns A promise resolving if this input can be handled, rejecting with an Error if not. */ - public abstract canHandle(input: TInput): Promise; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public async canHandle(input: TInput): Promise { + // Support any input by default + } /** * Handles the given input. This should only be done if the {@link canHandle} function returned `true`. diff --git a/test/unit/authentication/UnsecureWebIdExtractor.ts b/test/unit/authentication/UnsecureWebIdExtractor.ts index 2b6416b2c..15f648b97 100644 --- a/test/unit/authentication/UnsecureWebIdExtractor.ts +++ b/test/unit/authentication/UnsecureWebIdExtractor.ts @@ -5,7 +5,7 @@ describe('An UnsecureWebIdExtractor', (): void => { const extractor = new UnsecureWebIdExtractor(); it('can handle all input.', async(): Promise => { - await expect(extractor.canHandle()).resolves.toBeUndefined(); + await expect(extractor.canHandle({} as HttpRequest)).resolves.toBeUndefined(); }); it('returns undefined if there is no input.', async(): Promise => { diff --git a/test/unit/authorization/AllowEverythingAuthorizer.test.ts b/test/unit/authorization/AllowEverythingAuthorizer.test.ts index 6f3de0dc8..c87b0e1f2 100644 --- a/test/unit/authorization/AllowEverythingAuthorizer.test.ts +++ b/test/unit/authorization/AllowEverythingAuthorizer.test.ts @@ -4,7 +4,7 @@ describe('An AllowEverythingAuthorizer', (): void => { const authorizer = new AllowEverythingAuthorizer(); it('can handle everything.', async(): Promise => { - await expect(authorizer.canHandle()).resolves.toBeUndefined(); + await expect(authorizer.canHandle({} as any)).resolves.toBeUndefined(); }); it('always returns undefined.', async(): Promise => { diff --git a/test/unit/authorization/WebAclAuthorizer.test.ts b/test/unit/authorization/WebAclAuthorizer.test.ts index 0790768b7..6561b433f 100644 --- a/test/unit/authorization/WebAclAuthorizer.test.ts +++ b/test/unit/authorization/WebAclAuthorizer.test.ts @@ -43,7 +43,7 @@ describe('A WebAclAuthorizer', (): void => { it('handles all inputs.', async(): Promise => { authorizer = new WebAclAuthorizer(aclManager, containerManager, null as any); - await expect(authorizer.canHandle()).resolves.toBeUndefined(); + await expect(authorizer.canHandle({} as any)).resolves.toBeUndefined(); }); it('allows access if the acl file allows all agents.', async(): Promise => { diff --git a/test/unit/ldp/http/AcceptPreferenceParser.test.ts b/test/unit/ldp/http/AcceptPreferenceParser.test.ts index 62118fb20..0a013e262 100644 --- a/test/unit/ldp/http/AcceptPreferenceParser.test.ts +++ b/test/unit/ldp/http/AcceptPreferenceParser.test.ts @@ -5,7 +5,7 @@ describe('An AcceptPreferenceParser', (): void => { const preferenceParser = new AcceptPreferenceParser(); it('can handle all input.', async(): Promise => { - await expect(preferenceParser.canHandle()).resolves.toBeUndefined(); + await expect(preferenceParser.canHandle({} as HttpRequest)).resolves.toBeUndefined(); }); it('returns an empty result if there is no relevant input.', async(): Promise => { diff --git a/test/unit/ldp/http/BasicRequestParser.test.ts b/test/unit/ldp/http/BasicRequestParser.test.ts index 42bde116b..f6648d0d0 100644 --- a/test/unit/ldp/http/BasicRequestParser.test.ts +++ b/test/unit/ldp/http/BasicRequestParser.test.ts @@ -21,7 +21,7 @@ describe('A BasicRequestParser', (): void => { }); it('can handle any input.', async(): Promise => { - await expect(requestParser.canHandle()).resolves.toBeUndefined(); + await expect(requestParser.canHandle({} as any)).resolves.toBeUndefined(); }); it('errors if there is no input.', async(): Promise => { diff --git a/test/unit/ldp/http/BasicTargetExtractor.test.ts b/test/unit/ldp/http/BasicTargetExtractor.test.ts index cb6969584..baea42e20 100644 --- a/test/unit/ldp/http/BasicTargetExtractor.test.ts +++ b/test/unit/ldp/http/BasicTargetExtractor.test.ts @@ -4,7 +4,7 @@ describe('A BasicTargetExtractor', (): void => { const extractor = new BasicTargetExtractor(); it('can handle any input.', async(): Promise => { - await expect(extractor.canHandle()).resolves.toBeUndefined(); + await expect(extractor.canHandle({} as any)).resolves.toBeUndefined(); }); it('errors if there is no URL.', async(): Promise => { diff --git a/test/unit/ldp/http/RawBodyParser.test.ts b/test/unit/ldp/http/RawBodyParser.test.ts index b87eca65f..15a236b4a 100644 --- a/test/unit/ldp/http/RawBodyParser.test.ts +++ b/test/unit/ldp/http/RawBodyParser.test.ts @@ -15,7 +15,7 @@ describe('A RawBodyparser', (): void => { }); it('accepts all input.', async(): Promise => { - await expect(bodyParser.canHandle()).resolves.toBeUndefined(); + await expect(bodyParser.canHandle({} as any)).resolves.toBeUndefined(); }); it('returns empty output if there was no content length or transfer encoding.', async(): Promise => { diff --git a/test/unit/ldp/http/metadata/BasicMetadataExtractor.test.ts b/test/unit/ldp/http/metadata/BasicMetadataExtractor.test.ts index 51c303e9f..4c970acf3 100644 --- a/test/unit/ldp/http/metadata/BasicMetadataExtractor.test.ts +++ b/test/unit/ldp/http/metadata/BasicMetadataExtractor.test.ts @@ -28,7 +28,7 @@ describe(' A BasicMetadataExtractor', (): void => { ]); it('can handle all requests.', async(): Promise => { - await expect(handler.canHandle()).resolves.toBeUndefined(); + await expect(handler.canHandle({} as any)).resolves.toBeUndefined(); }); it('will add metadata from the parsers.', async(): Promise => { diff --git a/test/unit/server/ExpressHttpServer.test.ts b/test/unit/server/ExpressHttpServer.test.ts index a301d06ee..3c73cf739 100644 --- a/test/unit/server/ExpressHttpServer.test.ts +++ b/test/unit/server/ExpressHttpServer.test.ts @@ -12,10 +12,6 @@ const handle = async(input: { request: HttpRequest; response: HttpResponse }): P }; class SimpleHttpHandler extends HttpHandler { - public async canHandle(): Promise { - // Supports all HttpRequests - } - public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise { return handle(input); } diff --git a/test/unit/util/AsyncHandler.test.ts b/test/unit/util/AsyncHandler.test.ts index 75de47759..e22770d3b 100644 --- a/test/unit/util/AsyncHandler.test.ts +++ b/test/unit/util/AsyncHandler.test.ts @@ -1,7 +1,17 @@ -import type { AsyncHandler } from '../../../src/util/AsyncHandler'; +import { AsyncHandler } from '../../../src/util/AsyncHandler'; import { StaticAsyncHandler } from '../../util/StaticAsyncHandler'; describe('An AsyncHandler', (): void => { + it('supports any input by default.', async(): Promise => { + class EmptyHandler extends AsyncHandler { + public async handle(): Promise { + return null; + } + } + const handler = new EmptyHandler(); + await expect(handler.canHandle('test')).resolves.toBeUndefined(); + }); + it('calls canHandle and handle when handleSafe is called.', async(): Promise => { const handlerTrue: AsyncHandler = new StaticAsyncHandler(true, null); const canHandleFn = jest.fn(async(input: any): Promise => input);