From d1d29a3f52d621cc308817489f70eba5ebe4ff67 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Sun, 20 Dec 2020 15:41:45 +0100 Subject: [PATCH] test: Use Components.js in AuthenticatedLdpHandler. --- config/presets/ldp/response-writer.json | 6 +- .../http/metadata/LinkRelMetadataWriter.ts | 7 +- test/configs/BasicConfig.ts | 50 ------------- test/configs/BasicHandlersConfig.ts | 74 ------------------- .../AuthenticatedLdpHandler.test.ts | 43 +++-------- 5 files changed, 21 insertions(+), 159 deletions(-) delete mode 100644 test/configs/BasicConfig.ts delete mode 100644 test/configs/BasicHandlersConfig.ts diff --git a/config/presets/ldp/response-writer.json b/config/presets/ldp/response-writer.json index fd0f7a3d3..2e26b1b12 100644 --- a/config/presets/ldp/response-writer.json +++ b/config/presets/ldp/response-writer.json @@ -20,10 +20,10 @@ }, { "@type": "LinkRelMetadataWriter", - "LinkRelMetadataWriter:_headerMap": [ + "LinkRelMetadataWriter:_linkRelMap": [ { - "LinkRelMetadataWriter:_headerMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", - "LinkRelMetadataWriter:_headerMap_value": "type" + "LinkRelMetadataWriter:_linkRelMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", + "LinkRelMetadataWriter:_linkRelMap_value": "type" } ] } diff --git a/src/ldp/http/metadata/LinkRelMetadataWriter.ts b/src/ldp/http/metadata/LinkRelMetadataWriter.ts index a87d86e74..e0d590616 100644 --- a/src/ldp/http/metadata/LinkRelMetadataWriter.ts +++ b/src/ldp/http/metadata/LinkRelMetadataWriter.ts @@ -1,3 +1,4 @@ +import { getLoggerFor } from '../../../logging/LogUtil'; import type { HttpResponse } from '../../../server/HttpResponse'; import { addHeader } from '../../../util/HeaderUtil'; import type { RepresentationMetadata } from '../../representation/RepresentationMetadata'; @@ -9,6 +10,7 @@ import { MetadataWriter } from './MetadataWriter'; */ export class LinkRelMetadataWriter extends MetadataWriter { private readonly linkRelMap: Record; + protected readonly logger = getLoggerFor(this); public constructor(linkRelMap: Record) { super(); @@ -16,9 +18,12 @@ export class LinkRelMetadataWriter extends MetadataWriter { } public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise { - for (const key of Object.keys(this.linkRelMap)) { + const keys = Object.keys(this.linkRelMap); + this.logger.debug(`Available link relations: ${keys.length}`); + for (const key of keys) { const values = input.metadata.getAll(key).map((term): string => `<${term.value}>; rel="${this.linkRelMap[key]}"`); if (values.length > 0) { + this.logger.debug(`Adding Link header ${values}`); addHeader(input.response, 'link', values); } } diff --git a/test/configs/BasicConfig.ts b/test/configs/BasicConfig.ts deleted file mode 100644 index 0cebb8084..000000000 --- a/test/configs/BasicConfig.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { - HttpHandler, - ResourceStore, -} from '../../src/index'; -import { - AllowEverythingAuthorizer, - AuthenticatedLdpHandler, - EmptyCredentialsExtractor, - MethodPermissionsExtractor, -} from '../../src/index'; -import type { ServerConfig } from './ServerConfig'; -import { getOperationHandler, getInMemoryResourceStore, getBasicRequestParser, getResponseWriter } from './Util'; - -/** - * BasicConfig works with - * - an AllowEverythingAuthorizer (no acl) - * - an InMemoryResourceStore - * - GET, POST & DELETE operation handlers - */ - -export class BasicConfig implements ServerConfig { - public store: ResourceStore; - - public constructor() { - this.store = getInMemoryResourceStore(); - } - - public getHttpHandler(): HttpHandler { - const requestParser = getBasicRequestParser(); - - const credentialsExtractor = new EmptyCredentialsExtractor(); - const permissionsExtractor = new MethodPermissionsExtractor(); - const authorizer = new AllowEverythingAuthorizer(); - - const operationHandler = getOperationHandler(this.store); - - const responseWriter = getResponseWriter(); - - const handler = new AuthenticatedLdpHandler({ - requestParser, - credentialsExtractor, - permissionsExtractor, - authorizer, - operationHandler, - responseWriter, - }); - - return handler; - } -} diff --git a/test/configs/BasicHandlersConfig.ts b/test/configs/BasicHandlersConfig.ts deleted file mode 100644 index 2412c4943..000000000 --- a/test/configs/BasicHandlersConfig.ts +++ /dev/null @@ -1,74 +0,0 @@ -import type { - HttpHandler, - ResourceStore, -} from '../../src/index'; -import { - AllowEverythingAuthorizer, - AuthenticatedLdpHandler, - EmptyCredentialsExtractor, - MethodPermissionsExtractor, - QuadToRdfConverter, - RawBodyParser, - RdfToQuadConverter, - SparqlUpdateBodyParser, - SparqlPatchPermissionsExtractor, - WaterfallHandler, -} from '../../src/index'; - -import type { ServerConfig } from './ServerConfig'; -import { - getInMemoryResourceStore, - getOperationHandler, - getConvertingStore, - getPatchingStore, - getBasicRequestParser, - getResponseWriter, -} from './Util'; - -/** - * BasicHandlersConfig works with - * - an AllowEverythingAuthorizer (no acl) - * - an InMemoryResourceStore wrapped in a converting store & wrapped in a patching store - * - GET, POST, PUT, PATCH & DELETE operation handlers - */ - -export class BasicHandlersConfig implements ServerConfig { - public store: ResourceStore; - - public constructor() { - const convertingStore = getConvertingStore( - getInMemoryResourceStore(), - [ new QuadToRdfConverter(), new RdfToQuadConverter() ], - ); - this.store = getPatchingStore(convertingStore); - } - - public getHttpHandler(): HttpHandler { - const requestParser = getBasicRequestParser([ - new SparqlUpdateBodyParser(), - new RawBodyParser(), - ]); - - const credentialsExtractor = new EmptyCredentialsExtractor(); - const permissionsExtractor = new WaterfallHandler([ - new MethodPermissionsExtractor(), - new SparqlPatchPermissionsExtractor(), - ]); - const authorizer = new AllowEverythingAuthorizer(); - - const operationHandler = getOperationHandler(this.store); - - const responseWriter = getResponseWriter(); - - const handler = new AuthenticatedLdpHandler({ - requestParser, - credentialsExtractor, - permissionsExtractor, - authorizer, - operationHandler, - responseWriter, - }); - - return handler; - } -} diff --git a/test/integration/AuthenticatedLdpHandler.test.ts b/test/integration/AuthenticatedLdpHandler.test.ts index f80f8c07e..defb26379 100644 --- a/test/integration/AuthenticatedLdpHandler.test.ts +++ b/test/integration/AuthenticatedLdpHandler.test.ts @@ -3,24 +3,23 @@ import * as url from 'url'; import { namedNode, quad } from '@rdfjs/data-model'; import { Parser } from 'n3'; import type { MockResponse } from 'node-mocks-http'; -import { RootContainerInitializer } from '../../src/init/RootContainerInitializer'; +import type { HttpHandler } from '../../src/server/HttpHandler'; import { LDP } from '../../src/util/UriConstants'; -import { BasicConfig } from '../configs/BasicConfig'; -import { BasicHandlersConfig } from '../configs/BasicHandlersConfig'; -import { BASE } from '../configs/Util'; +import { BASE, instantiateFromConfig } from '../configs/Util'; import { call } from '../util/Util'; describe('An integrated AuthenticatedLdpHandler', (): void => { + let handler: HttpHandler; + + beforeAll(async(): Promise => { + handler = await instantiateFromConfig( + 'urn:solid-server:default:LdpHandler', 'auth-allow-all.json', { + 'urn:solid-server:default:variable:baseUrl': BASE, + }, + ) as HttpHandler; + }); + describe('with simple handlers', (): void => { - const config = new BasicConfig(); - const handler = config.getHttpHandler(); - - beforeAll(async(): Promise => { - // Initialize store - const initializer = new RootContainerInitializer(BASE, config.store); - await initializer.handleSafe(); - }); - it('can add, read and delete data based on incoming requests.', async(): Promise => { // POST let requestUrl = new URL('http://test.com/'); @@ -70,15 +69,6 @@ describe('An integrated AuthenticatedLdpHandler', (): void => { }); describe('with simple PATCH handlers', (): void => { - const config = new BasicHandlersConfig(); - const handler = config.getHttpHandler(); - - beforeAll(async(): Promise => { - // Initialize store - const initializer = new RootContainerInitializer(BASE, config.store); - await initializer.handleSafe(); - }); - it('can handle simple SPARQL updates.', async(): Promise => { // POST let requestUrl = new URL('http://test.com/'); @@ -142,15 +132,6 @@ describe('An integrated AuthenticatedLdpHandler', (): void => { }); describe('with simple PUT handlers', (): void => { - const config = new BasicHandlersConfig(); - const handler = config.getHttpHandler(); - - beforeAll(async(): Promise => { - // Initialize store - const initializer = new RootContainerInitializer(BASE, config.store); - await initializer.handleSafe(); - }); - it('should overwrite the content on PUT request.', async(): Promise => { // POST let requestUrl = new URL('http://test.com/');