Skip to content

Commit

Permalink
fix: Fix test issues
Browse files Browse the repository at this point in the history
The root container wasn't being created for file based tests.
Containers should be POSTed as RDF bodies.
Containment triples of child containers had no trailing slash.
  • Loading branch information
joachimvh committed Oct 14, 2020
1 parent e85ca62 commit 2296219
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/storage/ExtensionBasedMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ 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 { decodeUriPathComponents, encodeUriPathComponents, trimTrailingSlashes } from '../util/Util';
import {
decodeUriPathComponents,
encodeUriPathComponents,
ensureTrailingSlash,
trimTrailingSlashes,
} from '../util/Util';
import type { FileIdentifierMapper, ResourceLink } from './FileIdentifierMapper';

const { join: joinPath, normalize: normalizePath } = posix;
Expand Down Expand Up @@ -137,7 +142,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
let relative = filePath.slice(this.rootFilepath.length);
if (isContainer) {
return {
identifier: { path: this.baseRequestURI + encodeUriPathComponents(relative) },
identifier: { path: ensureTrailingSlash(this.baseRequestURI + encodeUriPathComponents(relative)) },
filePath,
};
}
Expand All @@ -150,7 +155,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
}

return {
identifier: { path: this.baseRequestURI + encodeUriPathComponents(relative) },
identifier: { path: trimTrailingSlashes(this.baseRequestURI + encodeUriPathComponents(relative)) },
filePath,
contentType,
};
Expand Down
2 changes: 1 addition & 1 deletion src/storage/FileResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class FileResourceStore implements ResourceStore {
const files = await fsPromises.readdir(path);
const match = files.find((file): any => !file.startsWith('.metadata'));
if (typeof match === 'string') {
throw new ConflictHttpError('Container is not empty.');
throw new ConflictHttpError('Can only delete empty containers.');
}

// Only delete the metadata file as auxiliary resource because this is the only file created by this store.
Expand Down
1 change: 1 addition & 0 deletions test/integration/AuthenticatedFileResourceStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('A server using a AuthenticatedFileResourceStore', (): void => {

beforeAll(async(): Promise<void> => {
rootFilePath = getRootFilePath('AuthenticatedFileResourceStore');
mkdirSync(rootFilePath, { recursive: true });
config = new AuthenticatedFileResourceStoreConfig(BASE, rootFilePath);
handler = config.getHttpHandler();
({ store } = config);
Expand Down
10 changes: 6 additions & 4 deletions test/integration/FileResourceStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mkdirSync } from 'fs';
import * as rimraf from 'rimraf';
import type { HttpHandler } from '../../src/server/HttpHandler';
import { FileResourceStoreConfig } from '../configs/FileResourceStoreConfig';
Expand All @@ -13,6 +14,7 @@ describe('A server using a FileResourceStore', (): void => {

beforeAll(async(): Promise<void> => {
rootFilePath = getRootFilePath('FileResourceStore');
mkdirSync(rootFilePath, { recursive: true });
config = new FileResourceStoreConfig(BASE, rootFilePath);
handler = config.getHttpHandler();
fileHelper = new FileTestHelper(handler, new URL(BASE));
Expand Down Expand Up @@ -109,7 +111,7 @@ describe('A server using a FileResourceStore', (): void => {
// Try DELETE folder
response = await fileHelper.simpleCall(new URL(folderId), 'DELETE', {});
expect(response.statusCode).toBe(409);
expect(response._getData()).toContain('ConflictHttpError: Container is not empty.');
expect(response._getData()).toContain('ConflictHttpError: Can only delete empty containers.');

// DELETE
await fileHelper.deleteFile('http://test.com/testfolder1/testfile0.txt');
Expand All @@ -130,7 +132,7 @@ describe('A server using a FileResourceStore', (): void => {
// Try DELETE folder
response = await fileHelper.simpleCall(new URL(folderId), 'DELETE', {});
expect(response.statusCode).toBe(409);
expect(response._getData()).toContain('ConflictHttpError: Container is not empty.');
expect(response._getData()).toContain('ConflictHttpError: Can only delete empty containers.');

// DELETE
await fileHelper.deleteFolder(subFolderId);
Expand All @@ -145,7 +147,7 @@ describe('A server using a FileResourceStore', (): void => {
const folderId = response._getHeaders().location;

// Create subfolder
response = await fileHelper.createFolder('testfolder3/subfolder0');
response = await fileHelper.createFolder('testfolder3/subfolder0/');
const subFolderId = response._getHeaders().location;

// Create file
Expand All @@ -155,7 +157,7 @@ describe('A server using a FileResourceStore', (): void => {
response = await fileHelper.getFolder(folderId);
expect(response.statusCode).toBe(200);
expect(response._getHeaders().location).toBe(folderId);
expect(response._getBuffer().toString()).toContain('<http://www.w3.org/ns/ldp#contains> <http://test.com/testfolder3/subfolder0> .');
expect(response._getBuffer().toString()).toContain('<http://www.w3.org/ns/ldp#contains> <http://test.com/testfolder3/subfolder0/> .');
expect(response._getBuffer().toString()).toContain('<http://www.w3.org/ns/ldp#contains> <http://test.com/testfolder3/testfile0.txt> .');

// DELETE
Expand Down
2 changes: 1 addition & 1 deletion test/util/TestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class FileTestHelper {
{
slug,
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
'content-type': 'text/plain',
'content-type': 'text/turtle',
'transfer-encoding': 'chunked',
},
);
Expand Down

0 comments on commit 2296219

Please sign in to comment.