Repository: solid/community-server (ts)

Issue:
Hide where the resource mapper gets the content type from
Currently the ResourceMapper has a function `getContentTypeFromExtension`. We want this to be more generic so it isn't exposed the content-type comes from the file extension (and so we can have other versions as well). One solution is to have `mapFilePathToUrl` return a tuple with the url and content-type. Would also help for #149 .

Pull request description:
feat: Have ExtensionBasedMapper handle extensions correctly
This resolves #150 and part of #151. 

It does not handle #146 yet. That would require more changes in the file store which is not the focus of this PR (and I don't want to do many changes there until #167 is handled).

Several changes were necessary in the file store and the file store tests (some of the mocks broke since the `fs` calls aren't in the same order anymore), but the main thing to look at is what happens in the `ExtensionBasedMapper`.

It is not fully yet as I would want it. It should only have the 2 public functions from the interface, but some of the other public functions would require more drastic changes to the file store to remove, for which I refer to my comment above.

Patch:
diff --git a/config/presets/ldp/operation-handler.json b/config/presets/ldp/operation-handler.json
index 6ca76e7a1..c81dc03f1 100644
--- a/config/presets/ldp/operation-handler.json
+++ b/config/presets/ldp/operation-handler.json
@@ -17,6 +17,12 @@
             "@id": "urn:solid-server:default:ResourceStore_Patching"
           }
         },
+        {
+          "@type": "HeadOperationHandler",
+          "HeadOperationHandler:_store": {
+            "@id": "urn:solid-server:default:ResourceStore_Patching"
+          }
+        },
         {
           "@type": "PatchOperationHandler",
           "PatchOperationHandler:_store": {
diff --git a/index.ts b/index.ts
index f1be352e0..9413f7026 100644
--- a/index.ts
+++ b/index.ts
@@ -42,6 +42,7 @@ export * from './src/logging/WinstonLoggerFactory';
 // LDP/Operations
 export * from './src/ldp/operations/DeleteOperationHandler';
 export * from './src/ldp/operations/GetOperationHandler';
+export * from './src/ldp/operations/HeadOperationHandler';
 export * from './src/ldp/operations/Operation';
 export * from './src/ldp/operations/OperationHandler';
 export * from './src/ldp/operations/PatchOperationHandler';
diff --git a/src/ldp/http/BasicTargetExtractor.ts b/src/ldp/http/BasicTargetExtractor.ts
index 7138167ba..738db89d0 100644
--- a/src/ldp/http/BasicTargetExtractor.ts
+++ b/src/ldp/http/BasicTargetExtractor.ts
@@ -1,6 +1,7 @@
 import type { TLSSocket } from 'tls';
 import { format } from 'url';
 import type { HttpRequest } from '../../server/HttpRequest';
+import { toCanonicalUrl } from '../../util/Util';
 import type { ResourceIdentifier } from '../representation/ResourceIdentifier';
 import { TargetExtractor } from './TargetExtractor';
 
@@ -28,6 +29,6 @@ export class BasicTargetExtractor extends TargetExtractor {
       pathname: input.url,
     });
 
-    return { path: url };
+    return { path: toCanonicalUrl(url) };
   }
 }
diff --git a/src/ldp/operations/HeadOperationHandler.ts b/src/ldp/operations/HeadOperationHandler.ts
new file mode 100644
index 000000000..b17e497ff
--- /dev/null
+++ b/src/ldp/operations/HeadOperationHandler.ts
@@ -0,0 +1,37 @@
+import { Readable } from 'stream';
+import type { ResourceStore } from '../../storage/ResourceStore';
+import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
+import type { Operation } from './Operation';
+import { OperationHandler } from './OperationHandler';
+import type { ResponseDescription } from './ResponseDescription';
+
+/**
+ * Handles HEAD {@link Operation}s.
+ * Calls the getRepresentation function from a {@link ResourceStore}.
+ */
+export class HeadOperationHandler extends OperationHandler {
+  private readonly store: ResourceStore;
+
+  public constructor(store: ResourceStore) {
+    super();
+    this.store = store;
+  }
+
+  public async canHandle(input: Operation): Promise<void> {
+    if (input.method !== 'HEAD') {
+      throw new UnsupportedHttpError('This handler only supports HEAD operations.');
+    }
+  }
+
+  public async handle(input: Operation): Promise<ResponseDescription> {
+    const body = await this.store.getRepresentation(input.target, input.preferences);
+
+    // Close the Readable as we will not return it.
+    body.data.destroy();
+    body.data = new Readable();
+    body.data._read = function(): void {
+      body.data.push(null);
+    };
+    return { identifier: input.target, body };
+  }
+}
diff --git a/src/storage/ExtensionBasedMapper.ts b/src/storage/ExtensionBasedMapper.ts
index ba79533e7..668032b46 100644
--- a/src/storage/ExtensionBasedMapper.ts
+++ b/src/storage/ExtensionBasedMapper.ts
@@ -1,11 +1,13 @@
+import { promises as fsPromises } from 'fs';
 import { posix } from 'path';
-import { types } from 'mime-types';
+import * as mime from 'mime-types';
 import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
 import { APPLICATION_OCTET_STREAM, TEXT_TURTLE } from '../util/ContentTypes';
 import { ConflictHttpError } from '../util/errors/ConflictHttpError';
 import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
+import { UnsupportedHttpError } from '../util/errors/UnsupportedHttpError';
 import { trimTrailingSlashes } from '../util/Util';
-import type { FileIdentifierMapper } from './FileIdentifierMapper';
+import type { FileIdentifierMapper, ResourceLink } from './FileIdentifierMapper';
 
 const { join: joinPath, normalize: normalizePath } = posix;
 
@@ -22,52 +24,136 @@ export interface ResourcePath {
   documentName?: string;
 }
 
+/**
+ * A mapper that stores the content-type of resources in the file path extension.
+ * In case the extension of the identifier does not correspond to the correct content-type,
+ * a new extension will be appended (with a `$` in front of it).
+ * E.g. if the path is `input.ttl` with content-type `text/plain`, the path would actually be `input.ttl$.txt`.
+ * This new extension is stripped again when generating an identifier.
+ */
 export class ExtensionBasedMapper implements FileIdentifierMapper {
-  private readonly base: string;
-  private readonly prootFilepath: string;
+  private readonly baseRequestURI: string;
+  private readonly rootFilepath: string;
   private readonly types: Record<string, any>;
 
   public constructor(base: string, rootFilepath: string, overrideTypes = { acl: TEXT_TURTLE, metadata: TEXT_TURTLE }) {
-    this.base = base;
-    this.prootFilepath = rootFilepath;
-    this.types = { ...types, ...overrideTypes };
-  }
-
-  public get baseRequestURI(): string {
-    return trimTrailingSlashes(this.base);
-  }
-
-  public get rootFilepath(): string {
-    return trimTrailingSlashes(this.prootFilepath);
+    this.baseRequestURI = trimTrailingSlashes(base);
+    this.rootFilepath = trimTrailingSlashes(normalizePath(rootFilepath));
+    this.types = { ...mime.types, ...overrideTypes };
   }
 
   /**
-   * Strips the baseRequestURI from the identifier and checks if the stripped base URI matches the store's one.
-   * @param identifier - Incoming identifier.
+   * Maps the given resource identifier / URL to a file path.
+   * Determines the content-type if no content-type was provided.
+   * For containers the content-type input gets ignored.
+   * @param identifier - The input identifier.
+   * @param contentType - The (optional) content-type of the resource.
    *
-   * @throws {@link NotFoundHttpError}
-   * If the identifier does not match the baseRequestURI path of the store.
-   *
-   * @returns Absolute path of the file.
+   * @returns A ResourceLink with all the necessary metadata.
    */
-  public mapUrlToFilePath(identifier: ResourceIdentifier, id = ''): string {
-    return this.getAbsolutePath(this.getRelativePath(identifier), id);
+  public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> {
+    let path = this.getRelativePath(identifier);
+
+    if (!path.startsWith('/')) {
+      throw new UnsupportedHttpError('URL needs a / after the base.');
+    }
+
+    if (path.includes('/..')) {
+      throw new UnsupportedHttpError('Disallowed /.. segment in URL.');
+    }
+
+    path = this.getAbsolutePath(path);
+
+    // Container
+    if (identifier.path.endsWith('/')) {
+      return {
+        identifier,
+        filePath: path,
+      };
+    }
+
+    // Would conflict with how new extensions get stored
+    if (/\$\.\w+$/u.test(path)) {
+      throw new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension.');
+    }
+
+    // Existing file
+    if (!contentType) {
+      const [ , folder, documentName ] = /^(.*\/)(.*)$/u.exec(path)!;
+
+      let fileName: string | undefined;
+      try {
+        const files = await fsPromises.readdir(folder);
+        fileName = files.find(
+          (file): boolean =>
+            file.startsWith(documentName) && /^(?:\$\..+)?$/u.test(file.slice(documentName.length)),
+        );
+      } catch {
+        // Parent folder does not exist (or is not a folder)
+        throw new NotFoundHttpError();
+      }
+
+      // File doesn't exist
+      if (!fileName) {
+        throw new NotFoundHttpError();
+      }
+
+      return {
+        identifier,
+        filePath: joinPath(folder, fileName),
+        contentType: this.getContentTypeFromExtension(fileName),
+      };
+    }
+
+    // If the extension of the identifier matches a different content-type than the one that is given,
+    // we need to add a new extension to match the correct type.
+    if (contentType !== this.getContentTypeFromExtension(path)) {
+      const extension = mime.extension(contentType);
+      if (!extension) {
+        throw new UnsupportedHttpError(`Unsupported content-type ${contentType}.`);
+      }
+      path = `${path}$.${extension}`;
+    }
+
+    return {
+      identifier,
+      filePath: path,
+      contentType,
+    };
   }
 
   /**
-   * Strips the rootFilepath path from the filepath and adds the baseRequestURI in front of it.
-   * @param path - The file path.
+   * Maps the given file path to an URL and determines the content-type
+   * @param filePath - The input file path.
+   * @param isContainer - If the path corresponds to a file.
    *
-   * @throws {@Link Error}
-   * If the file path does not match the rootFilepath path of the store.
-   *
-   * @returns Url of the file.
+   * @returns A ResourceLink with all the necessary metadata.
    */
-  public mapFilePathToUrl(path: string): string {
-    if (!path.startsWith(this.rootFilepath)) {
-      throw new Error(`File ${path} is not part of the file storage at ${this.rootFilepath}.`);
+  public async mapFilePathToUrl(filePath: string, isContainer: boolean): Promise<ResourceLink> {
+    if (!filePath.startsWith(this.rootFilepath)) {
+      throw new Error(`File ${filePath} is not part of the file storage at ${this.rootFilepath}.`);
     }
-    return this.baseRequestURI + path.slice(this.rootFilepath.length);
+
+    let relative = filePath.slice(this.rootFilepath.length);
+    if (isContainer) {
+      return {
+        identifier: { path: encodeURI(this.baseRequestURI + relative) },
+        filePath,
+      };
+    }
+
+    // Files
+    const extension = this.getExtension(relative);
+    const contentType = this.getContentTypeFromExtension(relative);
+    if (extension && relative.endsWith(`$.${extension}`)) {
+      relative = relative.slice(0, -(extension.length + 2));
+    }
+
+    return {
+      identifier: { path: encodeURI(this.baseRequestURI + relative) },
+      filePath,
+      contentType,
+    };
   }
 
   /**
@@ -76,9 +162,19 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
    *
    * @returns Content type of the file.
    */
-  public getContentTypeFromExtension(path: string): string {
+  private getContentTypeFromExtension(path: string): string {
+    const extension = this.getExtension(path);
+    return (extension && this.types[extension.toLowerCase()]) || APPLICATION_OCTET_STREAM;
+  }
+
+  /**
+   * Extracts the extension (without dot) from a path.
+   * Custom functin since `path.extname` does not work on all cases (e.g. ".acl")
+   * @param path - Input path to parse.
+   */
+  private getExtension(path: string): string | null {
     const extension = /\.([^./]+)$/u.exec(path);
-    return (extension && this.types[extension[1].toLowerCase()]) || APPLICATION_OCTET_STREAM;
+    return extension && extension[1];
   }
 
   /**
@@ -88,7 +184,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
    *
    * @returns Absolute path of the file.
    */
-  public getAbsolutePath(path: string, identifier = ''): string {
+  private getAbsolutePath(path: string, identifier = ''): string {
     return joinPath(this.rootFilepath, path, identifier);
   }
 
@@ -105,7 +201,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
     if (!identifier.path.startsWith(this.baseRequestURI)) {
       throw new NotFoundHttpError();
     }
-    return identifier.path.slice(this.baseRequestURI.length);
+    return decodeURI(identifier.path).slice(this.baseRequestURI.length);
   }
 
   /**
@@ -116,7 +212,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
    * @throws {@link ConflictHttpError}
    * If the root identifier is passed.
    *
-   * @returns A ResourcePath object containing path and (optional) slug fields.
+   * @returns A ResourcePath object containing (absolute) path and (optional) slug fields.
    */
   public extractDocumentName(identifier: ResourceIdentifier): ResourcePath {
     const [ , containerPath, documentName ] = /^(.*\/)([^/]+\/?)?$/u.exec(this.getRelativePath(identifier)) ?? [];
@@ -125,9 +221,9 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
       throw new ConflictHttpError('Container with that identifier already exists (root).');
     }
     return {
-      containerPath: normalizePath(containerPath),
+      containerPath: this.getAbsolutePath(normalizePath(containerPath)),
 
-      // If documentName is not undefined, return normalized documentName
+      // If documentName is defined, return normalized documentName
       documentName: typeof documentName === 'string' ? normalizePath(documentName) : undefined,
     };
   }
diff --git a/src/storage/FileIdentifierMapper.ts b/src/storage/FileIdentifierMapper.ts
index 17484932f..d957df722 100644
--- a/src/storage/FileIdentifierMapper.ts
+++ b/src/storage/FileIdentifierMapper.ts
@@ -1,21 +1,40 @@
 import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
 
+export interface ResourceLink {
+  /**
+   * Identifier of a resource.
+   */
+  identifier: ResourceIdentifier;
+  /**
+   * File path of a resource.
+   */
+  filePath: string;
+  /**
+   * Content-type for a data resource (not defined for containers).
+   */
+  contentType?: string;
+}
+
 /**
  * Supports mapping a file to an URL and back.
  */
 export interface FileIdentifierMapper {
   /**
-   * Maps the given file path to an URL.
-   * @param file - The input file path.
+   * Maps the given file path to an URL and determines the content-type
+   * @param filePath - The input file path.
+   * @param isContainer - If the path corresponds to a file.
    *
-   * @returns The URL as a string.
+   * @returns A ResourceLink with all the necessary metadata.
    */
-  mapFilePathToUrl: (filePath: string) => string;
+  mapFilePathToUrl: (filePath: string, isContainer: boolean) => Promise<ResourceLink>;
   /**
    * Maps the given resource identifier / URL to a file path.
-   * @param url - The input URL.
+   * Determines the content-type if no content-type was provided.
+   * For containers the content-type input gets ignored.
+   * @param identifier - The input identifier.
+   * @param contentType - The (optional) content-type of the resource.
    *
-   * @returns The file path as a string.
+   * @returns A ResourceLink with all the necessary metadata.
    */
-  mapUrlToFilePath: (identifier: ResourceIdentifier) => string;
+  mapUrlToFilePath: (identifier: ResourceIdentifier, contentType?: string) => Promise<ResourceLink>;
 }
diff --git a/src/storage/FileResourceStore.ts b/src/storage/FileResourceStore.ts
index 97fcec018..ec6fb6ba1 100644
--- a/src/storage/FileResourceStore.ts
+++ b/src/storage/FileResourceStore.ts
@@ -19,6 +19,7 @@ import { CONTENT_TYPE, DCTERMS, HTTP, POSIX, RDF, XSD } from '../util/UriConstan
 import { toTypedLiteral } from '../util/UriUtil';
 import { ensureTrailingSlash } from '../util/Util';
 import type { ExtensionBasedMapper } from './ExtensionBasedMapper';
+import type { ResourceLink } from './FileIdentifierMapper';
 import type { ResourceStore } from './ResourceStore';
 
 const { join: joinPath } = posix;
@@ -58,7 +59,7 @@ export class FileResourceStore implements ResourceStore {
     }
 
     // Get the path from the request URI, all metadata triples if any, and the Slug and Link header values.
-    const path = this.resourceMapper.getRelativePath(container);
+    const path = (await this.resourceMapper.mapUrlToFilePath(container)).filePath;
     const slug = representation.metadata.get(HTTP.slug)?.value;
     const types = representation.metadata.getAll(RDF.type);
 
@@ -72,6 +73,7 @@ export class FileResourceStore implements ResourceStore {
     if (raw.length > 0) {
       metadata = this.metadataController.serializeQuads(raw);
     }
+
     return isContainer ?
       this.createContainer(path, newIdentifier, path.endsWith('/'), metadata) :
       this.createFile(path, newIdentifier, representation.data, path.endsWith('/'), metadata);
@@ -88,7 +90,7 @@ export class FileResourceStore implements ResourceStore {
     }
 
     // Get the file status of the path defined by the request URI mapped to the corresponding filepath.
-    path = this.resourceMapper.getAbsolutePath(path);
+    path = (await this.resourceMapper.mapUrlToFilePath(identifier)).filePath;
     let stats;
     try {
       stats = await fsPromises.lstat(path);
@@ -115,20 +117,20 @@ export class FileResourceStore implements ResourceStore {
    */
   public async getRepresentation(identifier: ResourceIdentifier): Promise<Representation> {
     // Get the file status of the path defined by the request URI mapped to the corresponding filepath.
-    const path = this.resourceMapper.mapUrlToFilePath(identifier);
+    const resourceLink = await this.resourceMapper.mapUrlToFilePath(identifier);
     let stats;
     try {
-      stats = await fsPromises.lstat(path);
+      stats = await fsPromises.lstat(resourceLink.filePath);
     } catch {
       throw new NotFoundHttpError();
     }
 
     // Get the file or directory representation of the path according to its status.
     if (stats.isFile()) {
-      return await this.getFileRepresentation(path, stats);
+      return await this.getFileRepresentation(resourceLink, stats);
     }
     if (stats.isDirectory()) {
-      return await this.getDirectoryRepresentation(ensureTrailingSlash(path), stats);
+      return await this.getDirectoryRepresentation(resourceLink, stats);
     }
     throw new NotFoundHttpError();
   }
@@ -209,25 +211,24 @@ export class FileResourceStore implements ResourceStore {
   /**
    * Helper function to get the representation of a file in the file system.
    * It loads the quads from the corresponding metadata file if it exists.
-   * @param path - The path to the file.
+   * @param resourceLink - The path information of the resource.
    * @param stats - The Stats of the file.
    *
    * @returns The corresponding Representation.
    */
-  private async getFileRepresentation(path: string, stats: Stats): Promise<Representation> {
-    const readStream = createReadStream(path);
-    const contentType = this.resourceMapper.getContentTypeFromExtension(path);
+  private async getFileRepresentation(resourceLink: ResourceLink, stats: Stats): Promise<Representation> {
+    const readStream = createReadStream(resourceLink.filePath);
     let rawMetadata: Quad[] = [];
     try {
-      const readMetadataStream = createReadStream(`${path}.metadata`);
+      const readMetadataStream = createReadStream(`${resourceLink.filePath}.metadata`);
       rawMetadata = await this.metadataController.parseQuads(readMetadataStream);
     } catch {
       // Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
     }
-    const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path)).addQuads(rawMetadata)
+    const metadata = new RepresentationMetadata(resourceLink.identifier.path).addQuads(rawMetadata)
       .set(DCTERMS.modified, toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
       .set(POSIX.size, toTypedLiteral(stats.size, XSD.integer));
-    metadata.contentType = contentType;
+    metadata.contentType = resourceLink.contentType;
     return { metadata, data: readStream, binary: true };
   }
 
@@ -236,23 +237,23 @@ export class FileResourceStore implements ResourceStore {
    * It loads the quads from the corresponding metadata file if it exists
    * and generates quad representations for all its children.
    *
-   * @param path - The path to the directory.
+   * @param resourceLink - The path information of the resource.
    * @param stats - The Stats of the directory.
    *
    * @returns The corresponding Representation.
    */
-  private async getDirectoryRepresentation(path: string, stats: Stats): Promise<Representation> {
-    const files = await fsPromises.readdir(path);
+  private async getDirectoryRepresentation(resourceLink: ResourceLink, stats: Stats): Promise<Representation> {
+    const files = await fsPromises.readdir(resourceLink.filePath);
     const quads: Quad[] = [];
 
-    const containerURI = this.resourceMapper.mapFilePathToUrl(path);
+    const containerURI = resourceLink.identifier.path;
 
     quads.push(...this.metadataController.generateResourceQuads(containerURI, stats));
-    quads.push(...await this.getDirChildrenQuadRepresentation(files, path, containerURI));
+    quads.push(...await this.getDirChildrenQuadRepresentation(files, resourceLink.filePath, containerURI));
 
     let rawMetadata: Quad[] = [];
     try {
-      const readMetadataStream = createReadStream(joinPath(path, '.metadata'));
+      const readMetadataStream = createReadStream(joinPath(resourceLink.filePath, '.metadata'));
       rawMetadata = await this.metadataController.parseQuads(readMetadataStream);
     } catch {
       // Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
@@ -282,14 +283,15 @@ export class FileResourceStore implements ResourceStore {
     const childURIs: string[] = [];
     for (const childName of files) {
       try {
-        const childURI = this.resourceMapper.mapFilePathToUrl(joinPath(path, childName));
         const childStats = await fsPromises.lstat(joinPath(path, childName));
         if (!childStats.isFile() && !childStats.isDirectory()) {
           continue;
         }
+        const childLink = await this.resourceMapper
+          .mapFilePathToUrl(joinPath(path, childName), childStats.isDirectory());
 
-        quads.push(...this.metadataController.generateResourceQuads(childURI, childStats));
-        childURIs.push(childURI);
+        quads.push(...this.metadataController.generateResourceQuads(childLink.identifier.path, childStats));
+        childURIs.push(childLink.identifier.path);
       } catch {
         // Skip the child if there is an error.
       }
@@ -312,9 +314,7 @@ export class FileResourceStore implements ResourceStore {
     // (Re)write file for the resource if no container with that identifier exists.
     let stats;
     try {
-      stats = await fsPromises.lstat(
-        this.resourceMapper.getAbsolutePath(path, newIdentifier),
-      );
+      stats = await fsPromises.lstat(joinPath(path, newIdentifier));
     } catch {
       await this.createFile(path, newIdentifier, data, true, metadata);
       return;
@@ -335,9 +335,7 @@ export class FileResourceStore implements ResourceStore {
   private async setDirectoryRepresentation(path: string, newIdentifier: string, metadata?: Readable): Promise<void> {
     // Create a container if the identifier doesn't exist yet.
     try {
-      await fsPromises.access(
-        this.resourceMapper.getAbsolutePath(path, newIdentifier),
-      );
+      await fsPromises.access(joinPath(path, newIdentifier));
       throw new ConflictHttpError('Resource with that identifier already exists.');
     } catch (error: unknown) {
       if (error instanceof ConflictHttpError) {
@@ -367,12 +365,7 @@ export class FileResourceStore implements ResourceStore {
     }
 
     // Get the file status of the filepath of the directory where the file is to be created.
-    let stats;
-    try {
-      stats = await fsPromises.lstat(this.resourceMapper.getAbsolutePath(path));
-    } catch {
-      throw new MethodNotAllowedHttpError();
-    }
+    const stats = await fsPromises.lstat(path);
 
     // Only create the file if the provided filepath is a valid directory.
     if (!stats.isDirectory()) {
@@ -380,17 +373,17 @@ export class FileResourceStore implements ResourceStore {
     } else {
       // If metadata is specified, save it in a corresponding metadata file.
       if (metadata) {
-        await this.createDataFile(this.resourceMapper.getAbsolutePath(path, `${resourceName}.metadata`), metadata);
+        await this.createDataFile(joinPath(path, `${resourceName}.metadata`), metadata);
       }
 
       // If no error thrown from above, indicating failed metadata file creation, create the actual resource file.
       try {
-        const fullPath = this.resourceMapper.getAbsolutePath(path, resourceName);
+        const fullPath = joinPath(path, resourceName);
         await this.createDataFile(fullPath, data);
-        return { path: this.resourceMapper.mapFilePathToUrl(fullPath) };
+        return (await this.resourceMapper.mapFilePathToUrl(fullPath, false)).identifier;
       } catch (error: unknown) {
         // Normal file has not been created so we don't want the metadata file to remain.
-        await fsPromises.unlink(this.resourceMapper.getAbsolutePath(path, `${resourceName}.metadata`));
+        await fsPromises.unlink(joinPath(path, `${resourceName}.metadata`));
         throw error;
       }
     }
@@ -407,12 +400,12 @@ export class FileResourceStore implements ResourceStore {
    */
   private async createContainer(path: string, containerName: string,
     allowRecursiveCreation: boolean, metadata?: Readable): Promise<ResourceIdentifier> {
-    const fullPath = ensureTrailingSlash(this.resourceMapper.getAbsolutePath(path, containerName));
+    const fullPath = joinPath(path, containerName);
 
     // If recursive creation is not allowed, check if the parent container exists and then create the child directory.
     try {
       if (!allowRecursiveCreation) {
-        const stats = await fsPromises.lstat(this.resourceMapper.getAbsolutePath(path));
+        const stats = await fsPromises.lstat(path);
         if (!stats.isDirectory()) {
           throw new MethodNotAllowedHttpError('The given path is not a valid container.');
         }
@@ -436,7 +429,7 @@ export class FileResourceStore implements ResourceStore {
         throw error;
       }
     }
-    return { path: this.resourceMapper.mapFilePathToUrl(fullPath) };
+    return (await this.resourceMapper.mapFilePathToUrl(fullPath, true)).identifier;
   }
 
   /**
diff --git a/src/util/Util.ts b/src/util/Util.ts
index 080f08178..5a75aeaea 100644
--- a/src/util/Util.ts
+++ b/src/util/Util.ts
@@ -66,3 +66,16 @@ export const pipeStreamsAndErrors = <T extends Writable>(readable: Readable, des
   readable.on('error', (error): boolean => destination.emit('error', new UnsupportedHttpError(error.message)));
   return destination;
 };
+
+/**
+ * Converts a URL string to the "canonical" version that should be used internally for consistency.
+ * Decodes all percent encodings and then makes sure only the necessary characters are encoded again.
+ */
+export const toCanonicalUrl = (url: string): string => {
+  const match = /(\w+:\/\/[^/]+\/)(.*)/u.exec(url);
+  if (!match) {
+    throw new UnsupportedHttpError(`Invalid URL ${url}`);
+  }
+  const [ , domain, path ] = match;
+  return encodeURI(domain + path.split('/').map(decodeURIComponent).join('/'));
+};


Test patch:
diff --git a/test/configs/Util.ts b/test/configs/Util.ts
index e98e7178a..d27f52e67 100644
--- a/test/configs/Util.ts
+++ b/test/configs/Util.ts
@@ -14,6 +14,7 @@ import {
   DeleteOperationHandler,
   FileResourceStore,
   GetOperationHandler,
+  HeadOperationHandler,
   InMemoryResourceStore,
   InteractionController,
   MetadataController,
@@ -94,6 +95,7 @@ export const getPatchingStore = (store: ResourceStore): PatchingStore => {
 export const getOperationHandler = (store: ResourceStore): CompositeAsyncHandler<Operation, ResponseDescription> => {
   const handlers = [
     new GetOperationHandler(store),
+    new HeadOperationHandler(store),
     new PostOperationHandler(store),
     new PutOperationHandler(store),
     new PatchOperationHandler(store),
diff --git a/test/unit/ldp/http/BasicTargetExtractor.test.ts b/test/unit/ldp/http/BasicTargetExtractor.test.ts
index 8d2f3d5fc..db79a4bfc 100644
--- a/test/unit/ldp/http/BasicTargetExtractor.test.ts
+++ b/test/unit/ldp/http/BasicTargetExtractor.test.ts
@@ -16,7 +16,8 @@ describe('A BasicTargetExtractor', (): void => {
   });
 
   it('returns the input URL.', async(): Promise<void> => {
-    await expect(extractor.handle({ url: 'url', headers: { host: 'test.com' }} as any)).resolves.toEqual({ path: 'http://test.com/url' });
+    await expect(extractor.handle({ url: 'url', headers: { host: 'test.com' }} as any))
+      .resolves.toEqual({ path: 'http://test.com/url' });
   });
 
   it('uses https protocol if the connection is secure.', async(): Promise<void> => {
@@ -24,4 +25,9 @@ describe('A BasicTargetExtractor', (): void => {
       { url: 'url', headers: { host: 'test.com' }, connection: { encrypted: true } as any } as any,
     )).resolves.toEqual({ path: 'https://test.com/url' });
   });
+
+  it('decodes relevant percent encodings.', async(): Promise<void> => {
+    await expect(extractor.handle({ url: '/a%20path%26/name', headers: { host: 'test.com' }} as any))
+      .resolves.toEqual({ path: 'http://test.com/a%20path&/name' });
+  });
 });
diff --git a/test/unit/ldp/operations/HeadOperationHandler.test.ts b/test/unit/ldp/operations/HeadOperationHandler.test.ts
new file mode 100644
index 000000000..d0914ed25
--- /dev/null
+++ b/test/unit/ldp/operations/HeadOperationHandler.test.ts
@@ -0,0 +1,28 @@
+import arrayifyStream from 'arrayify-stream';
+import streamifyArray from 'streamify-array';
+import { HeadOperationHandler } from '../../../../src/ldp/operations/HeadOperationHandler';
+import type { Operation } from '../../../../src/ldp/operations/Operation';
+import type { Representation } from '../../../../src/ldp/representation/Representation';
+import type { ResourceStore } from '../../../../src/storage/ResourceStore';
+import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
+
+describe('A HeadOperationHandler', (): void => {
+  const store = {
+    getRepresentation: async(): Promise<Representation> => ({ binary: false, data: streamifyArray([ 1, 2, 3 ]) } as
+      Representation),
+  } as unknown as ResourceStore;
+  const handler = new HeadOperationHandler(store);
+
+  it('only supports HEAD operations.', async(): Promise<void> => {
+    await expect(handler.canHandle({ method: 'HEAD' } as Operation)).resolves.toBeUndefined();
+    await expect(handler.canHandle({ method: 'GET' } as Operation)).rejects.toThrow(UnsupportedHttpError);
+    await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
+  });
+
+  it('returns the representation from the store with the input identifier and empty data.', async(): Promise<void> => {
+    const result = await handler.handle({ target: { path: 'url' }} as Operation);
+    expect(result.identifier.path).toBe('url');
+    expect(result.body?.binary).toBe(false);
+    await expect(arrayifyStream(result.body!.data)).resolves.toEqual([]);
+  });
+});
diff --git a/test/unit/storage/ExtensionBasedMapper.test.ts b/test/unit/storage/ExtensionBasedMapper.test.ts
index a86e33596..0932f89da 100644
--- a/test/unit/storage/ExtensionBasedMapper.test.ts
+++ b/test/unit/storage/ExtensionBasedMapper.test.ts
@@ -1,20 +1,157 @@
+import fs from 'fs';
 import { ExtensionBasedMapper } from '../../../src/storage/ExtensionBasedMapper';
+import { ConflictHttpError } from '../../../src/util/errors/ConflictHttpError';
+import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
+import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
+import { trimTrailingSlashes } from '../../../src/util/Util';
+
+jest.mock('fs');
 
 describe('An ExtensionBasedMapper', (): void => {
   const base = 'http://test.com/';
   const rootFilepath = 'uploads/';
-  const resourceMapper = new ExtensionBasedMapper(base, rootFilepath);
+  const mapper = new ExtensionBasedMapper(base, rootFilepath);
+  let fsPromises: { [ id: string ]: jest.Mock };
+
+  beforeEach(async(): Promise<void> => {
+    jest.clearAllMocks();
+    fs.promises = {
+      readdir: jest.fn(),
+    } as any;
+    fsPromises = fs.promises as any;
+  });
+
+  describe('mapUrlToFilePath', (): void => {
+    it('throws 404 if the input path does not contain the base.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: 'invalid' })).rejects.toThrow(NotFoundHttpError);
+    });
+
+    it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
+        .rejects.toThrow(new UnsupportedHttpError('URL needs a / after the base.'));
+    });
+
+    it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
+        .rejects.toThrow(new UnsupportedHttpError('Disallowed /.. segment in URL.'));
+    });
+
+    it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}container/` })).resolves.toEqual({
+        identifier: { path: `${base}container/` },
+        filePath: `${rootFilepath}container/`,
+      });
+    });
+
+    it('rejects URLs that end with "$.{extension}".', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test$.txt` }))
+        .rejects.toThrow(new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension.'));
+    });
+
+    it('throws 404 when looking in a folder that does not exist.', async(): Promise<void> => {
+      fsPromises.readdir.mockImplementation((): void => {
+        throw new Error('does not exist');
+      });
+      await expect(mapper.mapUrlToFilePath({ path: `${base}no/test.txt` })).rejects.toThrow(NotFoundHttpError);
+    });
+
+    it('throws 404 when looking for a file that does not exist.', async(): Promise<void> => {
+      fsPromises.readdir.mockReturnValue([ 'test.ttl' ]);
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).rejects.toThrow(NotFoundHttpError);
+    });
 
-  it('returns the correct url of a file.', async(): Promise<void> => {
-    let result = resourceMapper.mapFilePathToUrl(`${rootFilepath}test.txt`);
-    expect(result).toEqual(`${base}test.txt`);
+    it('determines the content-type based on the extension.', async(): Promise<void> => {
+      fsPromises.readdir.mockReturnValue([ 'test.txt' ]);
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt`,
+        contentType: 'text/plain',
+      });
+    });
 
-    result = resourceMapper.mapFilePathToUrl(`${rootFilepath}image.jpg`);
-    expect(result).toEqual(`${base}image.jpg`);
+    it('matches even if the content-type does not match the extension.', async(): Promise<void> => {
+      fsPromises.readdir.mockReturnValue([ 'test.txt$.ttl' ]);
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt$.ttl`,
+        contentType: 'text/turtle',
+      });
+    });
+
+    it('generates a file path if the content-type was provided.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/plain')).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt`,
+        contentType: 'text/plain',
+      });
+    });
+
+    it('adds an extension if the given extension does not match the given content-type.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/turtle')).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt$.ttl`,
+        contentType: 'text/turtle',
+      });
+    });
+
+    it('throws 400 if the given content-type is not recognized.', async(): Promise<void> => {
+      await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'fake/data'))
+        .rejects.toThrow(new UnsupportedHttpError(`Unsupported content-type fake/data.`));
+    });
+  });
+
+  describe('mapFilePathToUrl', (): void => {
+    it('throws an error if the input path does not contain the root file path.', async(): Promise<void> => {
+      await expect(mapper.mapFilePathToUrl('invalid', true)).rejects.toThrow(Error);
+    });
+
+    it('returns a generated identifier for directories.', async(): Promise<void> => {
+      await expect(mapper.mapFilePathToUrl(`${rootFilepath}container/`, true)).resolves.toEqual({
+        identifier: { path: `${base}container/` },
+        filePath: `${rootFilepath}container/`,
+      });
+    });
+
+    it('returns a generated identifier for files with corresponding content-type.', async(): Promise<void> => {
+      await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt`,
+        contentType: 'text/plain',
+      });
+    });
+
+    it('removes appended extensions.', async(): Promise<void> => {
+      await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt$.ttl`, false)).resolves.toEqual({
+        identifier: { path: `${base}test.txt` },
+        filePath: `${rootFilepath}test.txt$.ttl`,
+        contentType: 'text/turtle',
+      });
+    });
+
+    it('sets the content-type to application/octet-stream if there is no extension.', async(): Promise<void> => {
+      await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).resolves.toEqual({
+        identifier: { path: `${base}test` },
+        filePath: `${rootFilepath}test`,
+        contentType: 'application/octet-stream',
+      });
+    });
   });
 
-  it('errors when filepath does not contain rootFilepath.', async(): Promise<void> => {
-    expect((): string => resourceMapper.mapFilePathToUrl('random/test.txt')).toThrow(Error);
-    expect((): string => resourceMapper.mapFilePathToUrl('test.txt')).toThrow(Error);
+  describe('extractDocumentName', (): void => {
+    it('throws an error if the input corresponds to root.', async(): Promise<void> => {
+      expect((): any => mapper.extractDocumentName({ path: base })).toThrow(ConflictHttpError);
+      expect((): any => mapper.extractDocumentName({ path: trimTrailingSlashes(base) }))
+        .toThrow(ConflictHttpError);
+    });
+
+    it('parses the identifier into container file path and document name.', async(): Promise<void> => {
+      expect(mapper.extractDocumentName({ path: `${base}test` })).toEqual({
+        containerPath: rootFilepath,
+        documentName: 'test',
+      });
+      expect(mapper.extractDocumentName({ path: `${base}test/` })).toEqual({
+        containerPath: `${rootFilepath}test/`,
+      });
+    });
   });
 });
diff --git a/test/unit/storage/FileResourceStore.test.ts b/test/unit/storage/FileResourceStore.test.ts
index 4181fe23f..8628c3157 100644
--- a/test/unit/storage/FileResourceStore.test.ts
+++ b/test/unit/storage/FileResourceStore.test.ts
@@ -47,6 +47,14 @@ describe('A FileResourceStore', (): void => {
 
   beforeEach(async(): Promise<void> => {
     jest.clearAllMocks();
+    fs.promises = {
+      rmdir: jest.fn(),
+      lstat: jest.fn(),
+      readdir: jest.fn(),
+      mkdir: jest.fn(),
+      unlink: jest.fn(),
+      access: jest.fn(),
+    } as any;
 
     store = new FileResourceStore(
       new ExtensionBasedMapper(base, rootFilepath),
@@ -93,18 +101,18 @@ describe('A FileResourceStore', (): void => {
   });
 
   it('errors if a resource was not found.', async(): Promise<void> => {
-    (fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
-      throw new Error('Path does not exist.');
-    });
-    (fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
+    (fsPromises.lstat as jest.Mock).mockImplementation((): any => {
       throw new Error('Path does not exist.');
     });
+    (fsPromises.readdir as jest.Mock).mockImplementation((): any => []);
     await expect(store.getRepresentation({ path: 'http://wrong.com/wrong' })).rejects.toThrow(NotFoundHttpError);
     await expect(store.getRepresentation({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
+    await expect(store.getRepresentation({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
     await expect(store.addResource({ path: 'http://wrong.com/wrong' }, representation))
       .rejects.toThrow(NotFoundHttpError);
     await expect(store.deleteResource({ path: 'wrong' })).rejects.toThrow(NotFoundHttpError);
     await expect(store.deleteResource({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
+    await expect(store.deleteResource({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
     await expect(store.setRepresentation({ path: 'http://wrong.com/' }, representation))
       .rejects.toThrow(NotFoundHttpError);
   });
@@ -153,6 +161,7 @@ describe('A FileResourceStore', (): void => {
   it('errors for container creation with path to non container.', async(): Promise<void> => {
     // Mock the fs functions.
     (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
+    (fsPromises.readdir as jest.Mock).mockReturnValue([ 'foo' ]);
 
     // Tests
     representation.metadata.add(RDF.type, LDP.BasicContainer);
@@ -161,33 +170,39 @@ describe('A FileResourceStore', (): void => {
     expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
   });
 
-  it('errors 405 for POST invalid path ending without slash.', async(): Promise<void> => {
+  it('errors 404 for POST invalid path ending without slash and 405 for valid.', async(): Promise<void> => {
     // Mock the fs functions.
-    (fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
-      throw new Error('Path does not exist.');
-    });
-    (fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
-      throw new Error('Path does not exist.');
-    });
-    (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
+    (fsPromises.readdir as jest.Mock).mockReturnValue([]);
 
     // Tests
     representation.metadata.add(RDF.type, LDP.BasicContainer);
     representation.metadata.add(HTTP.slug, 'myContainer/');
     await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
-      .rejects.toThrow(MethodNotAllowedHttpError);
-    expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
+      .rejects.toThrow(NotFoundHttpError);
+    expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
 
     representation.metadata.set(RDF.type, LDP.Resource);
     representation.metadata.set(HTTP.slug, 'file.txt');
     await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
-      .rejects.toThrow(MethodNotAllowedHttpError);
-    expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
+      .rejects.toThrow(NotFoundHttpError);
+    expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
 
+    (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
+    (fsPromises.readdir as jest.Mock).mockReturnValue([ 'existingresource' ]);
     representation.metadata.removeAll(RDF.type);
     await expect(store.addResource({ path: `${base}existingresource` }, representation))
       .rejects.toThrow(MethodNotAllowedHttpError);
-    expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
+    expect(fsPromises.lstat as jest.Mock).toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
+
+    (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
+    (fsPromises.mkdir as jest.Mock).mockImplementation((): void => {
+      throw new Error('not a directory');
+    });
+    representation.metadata.removeAll(RDF.type);
+    await expect(store.addResource({ path: `${base}existingresource/container/` }, representation))
+      .rejects.toThrow(MethodNotAllowedHttpError);
+    expect(fsPromises.lstat as jest.Mock)
+      .toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
   });
 
   it('can set data.', async(): Promise<void> => {
@@ -203,6 +218,7 @@ describe('A FileResourceStore', (): void => {
     // Mock: Get
     stats = { ...stats };
     stats.isFile = jest.fn((): any => true);
+    (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
     (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
     (fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
     (fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([]));
@@ -228,6 +244,7 @@ describe('A FileResourceStore', (): void => {
   it('can delete data.', async(): Promise<void> => {
     // Mock the fs functions.
     // Delete
+    (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
     stats.isFile = jest.fn((): any => true);
     (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
     (fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
@@ -236,6 +253,7 @@ describe('A FileResourceStore', (): void => {
     });
 
     // Mock: Get
+    (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ ]);
     (fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
       throw new Error('Path does not exist.');
     });
@@ -325,6 +343,7 @@ describe('A FileResourceStore', (): void => {
   it('errors 404 when accessing non resource (file/directory), e.g. special files.', async(): Promise<void> => {
     // Mock the fs functions.
     (fsPromises.lstat as jest.Mock).mockReturnValue(stats);
+    (fsPromises.readdir as jest.Mock).mockReturnValue([ '14' ]);
 
     // Tests
     await expect(store.deleteResource({ path: `${base}dev/pts/14` })).rejects.toThrow(NotFoundHttpError);
@@ -476,6 +495,7 @@ describe('A FileResourceStore', (): void => {
     (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
     (fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
     (fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
+    (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ '.htaccess' ]);
 
     const result = await store.getRepresentation({ path: `${base}.htaccess` });
     expect(result).toEqual({
@@ -516,6 +536,7 @@ describe('A FileResourceStore', (): void => {
       stats.isDirectory = jest.fn((): any => true);
       (fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
       (fsPromises.mkdir as jest.Mock).mockReturnValue(true);
+      (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'foo' ]);
 
       // Tests
       representation.metadata.add(RDF.type, LDP.BasicContainer);
@@ -545,6 +566,8 @@ describe('A FileResourceStore', (): void => {
     const filePath: string = (fs.createWriteStream as jest.Mock).mock.calls[0][0];
     expect(filePath.startsWith(rootFilepath)).toBeTruthy();
     const name = filePath.slice(rootFilepath.length);
+
+    (fsPromises.readdir as jest.Mock).mockReturnValueOnce([ name ]);
     const result = await store.getRepresentation({ path: `${base}${name}` });
     expect(result).toEqual({
       binary: true,
diff --git a/test/unit/util/Util.test.ts b/test/unit/util/Util.test.ts
index 284a53e31..c196b589f 100644
--- a/test/unit/util/Util.test.ts
+++ b/test/unit/util/Util.test.ts
@@ -1,5 +1,6 @@
 import streamifyArray from 'streamify-array';
-import { ensureTrailingSlash, matchingMediaType, readableToString } from '../../../src/util/Util';
+import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
+import { ensureTrailingSlash, matchingMediaType, readableToString, toCanonicalUrl } from '../../../src/util/Util';
 
 describe('Util function', (): void => {
   describe('ensureTrailingSlash', (): void => {
@@ -31,4 +32,15 @@ describe('Util function', (): void => {
       expect(matchingMediaType('text/plain', 'text/turtle')).toBeFalsy();
     });
   });
+
+  describe('toCanonicalUrl', (): void => {
+    it('makes sure only the necessary parts are encoded.', async(): Promise<void> => {
+      expect(toCanonicalUrl('http://test.com/a%20path%26/name'))
+        .toEqual('http://test.com/a%20path&/name');
+    });
+
+    it('errors on invalid URLs.', async(): Promise<void> => {
+      expect((): any => toCanonicalUrl('notAnUrl')).toThrow(new UnsupportedHttpError('Invalid URL notAnUrl'));
+    });
+  });
 });