mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Update LdpHandlerWithoutAuth to use fetch for tests
This commit is contained in:
parent
fa8d406f34
commit
dd3fb63d18
@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
module.exports = {
|
||||
root: true,
|
||||
parser: '@typescript-eslint/parser',
|
||||
@ -6,10 +7,9 @@ module.exports = {
|
||||
project: [ './tsconfig.json', './test/tsconfig.json' ],
|
||||
},
|
||||
globals: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
AsyncIterable: 'readonly',
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
NodeJS: 'readonly',
|
||||
RequestInit: 'readonly',
|
||||
},
|
||||
plugins: [
|
||||
'tsdoc',
|
||||
|
@ -1,12 +1,19 @@
|
||||
import { createReadStream } from 'fs';
|
||||
import type { Server } from 'http';
|
||||
import fetch from 'cross-fetch';
|
||||
import { DataFactory, Parser } from 'n3';
|
||||
import type { MockResponse } from 'node-mocks-http';
|
||||
import { ensureTrailingSlash, PIM, RDF } from '../../src/';
|
||||
import type { HttpHandler, Initializer, ResourceStore } from '../../src/';
|
||||
import { joinFilePath, PIM, RDF } from '../../src/';
|
||||
import type { Initializer, ResourceStore } from '../../src/';
|
||||
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||
import { LDP } from '../../src/util/Vocabularies';
|
||||
import { ResourceHelper } from '../util/TestHelpers';
|
||||
import { BASE, getTestFolder, removeFolder, instantiateFromConfig } from './Config';
|
||||
import { deleteResource, expectQuads, getResource, postResource, putResource } from '../util/FetchUtil';
|
||||
import { getPort } from '../util/Util';
|
||||
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
|
||||
const { literal, namedNode, quad } = DataFactory;
|
||||
|
||||
const port = getPort('LpdHandlerWithoutAuth');
|
||||
const baseUrl = `http://localhost:${port}/`;
|
||||
|
||||
const rootFilePath = getTestFolder('full-config-no-auth');
|
||||
const stores: [string, any][] = [
|
||||
[ 'in-memory storage', {
|
||||
@ -19,13 +26,15 @@ const stores: [string, any][] = [
|
||||
}],
|
||||
];
|
||||
|
||||
describe.each(stores)('An LDP handler without auth using %s', (name, { storeUrn, teardown }): void => {
|
||||
let handler: HttpHandler;
|
||||
let resourceHelper: ResourceHelper;
|
||||
describe.each(stores)('An LDP handler allowing all request %s', (name, { storeUrn, teardown }): void => {
|
||||
let server: Server;
|
||||
let initializer: Initializer;
|
||||
let factory: HttpServerFactory;
|
||||
|
||||
beforeAll(async(): Promise<void> => {
|
||||
const variables: Record<string, any> = {
|
||||
'urn:solid-server:default:variable:baseUrl': BASE,
|
||||
'urn:solid-server:default:variable:port': port,
|
||||
'urn:solid-server:default:variable:baseUrl': baseUrl,
|
||||
'urn:solid-server:default:variable:rootFilePath': rootFilePath,
|
||||
};
|
||||
const internalStore = await instantiateFromConfig(
|
||||
@ -35,287 +44,213 @@ describe.each(stores)('An LDP handler without auth using %s', (name, { storeUrn,
|
||||
) as ResourceStore;
|
||||
variables['urn:solid-server:default:variable:store'] = internalStore;
|
||||
|
||||
// Create and initialize the HTTP handler and related components
|
||||
let initializer: Initializer;
|
||||
// Create and initialize the server
|
||||
const instances = await instantiateFromConfig(
|
||||
'urn:solid-server:test:Instances',
|
||||
'ldp-with-auth.json',
|
||||
variables,
|
||||
) as Record<string, any>;
|
||||
({ handler, initializer } = instances);
|
||||
({ factory, initializer } = instances);
|
||||
|
||||
// Set up the internal store
|
||||
await initializer.handleSafe();
|
||||
|
||||
// Create test helpers for manipulating the components
|
||||
resourceHelper = new ResourceHelper(handler, BASE);
|
||||
server = factory.startServer(port);
|
||||
});
|
||||
|
||||
afterAll(async(): Promise<void> => {
|
||||
await teardown();
|
||||
await new Promise((resolve, reject): void => {
|
||||
server.close((error): void => error ? reject(error) : resolve());
|
||||
});
|
||||
});
|
||||
|
||||
it('can read a folder listing.', async():
|
||||
Promise<void> => {
|
||||
const response = await resourceHelper.getResource(`${BASE}/`);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.getHeaders()).toHaveProperty('content-type', 'text/turtle');
|
||||
it('can read a container listing.', async(): Promise<void> => {
|
||||
const response = await getResource(baseUrl);
|
||||
|
||||
await expectQuads(response, [
|
||||
quad(namedNode(baseUrl), RDF.terms.type, LDP.terms.Container),
|
||||
]);
|
||||
|
||||
const parser = new Parser({ baseIRI: `${BASE}/` });
|
||||
const quads = parser.parse(response._getData().toString());
|
||||
expect(quads.some((entry): boolean => entry.equals(
|
||||
quad(namedNode(`${BASE}/`), RDF.terms.type, LDP.terms.Container),
|
||||
))).toBeTruthy();
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${BASE}/.acl>; rel="acl"`);
|
||||
// This is only here because we're accessing the root container
|
||||
expect(response.getHeaders().link).toContain(`<${PIM.Storage}>; rel="type"`);
|
||||
expect(response.headers.get('link')).toContain(`<${PIM.Storage}>; rel="type"`);
|
||||
});
|
||||
|
||||
it('can read a folder listing with a query string.', async():
|
||||
Promise<void> => {
|
||||
const response = await resourceHelper.getResource(`${BASE}/?abc=def&xyz`);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.getHeaders()).toHaveProperty('content-type', 'text/turtle');
|
||||
it('can read a container listing with a query string.', async(): Promise<void> => {
|
||||
// Helper functions would fail due to query params
|
||||
const response = await fetch(`${baseUrl}?abc=def&xyz`);
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get('content-type')).toBe('text/turtle');
|
||||
expect(response.headers.get('link')).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.headers.get('link')).toContain(`<${baseUrl}.acl>; rel="acl"`);
|
||||
expect(response.headers.get('link')).toContain(`<${PIM.Storage}>; rel="type"`);
|
||||
|
||||
const parser = new Parser({ baseIRI: `${BASE}/` });
|
||||
const quads = parser.parse(response._getData().toString());
|
||||
const parser = new Parser({ baseIRI: baseUrl });
|
||||
const quads = parser.parse(await response.text());
|
||||
expect(quads.some((entry): boolean => entry.equals(
|
||||
quad(namedNode(`${BASE}/`), RDF.terms.type, LDP.terms.Container),
|
||||
))).toBeTruthy();
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${BASE}/.acl>; rel="acl"`);
|
||||
expect(response.getHeaders().link).toContain(`<${PIM.Storage}>; rel="type"`);
|
||||
quad(namedNode(baseUrl), RDF.terms.type, LDP.terms.Container),
|
||||
))).toBe(true);
|
||||
});
|
||||
|
||||
it('can add a file to the store, read it and delete it.', async():
|
||||
Promise<void> => {
|
||||
const filePath = 'testfile0.txt';
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
it('can add a document to the store, read it and delete it.', async(): Promise<void> => {
|
||||
const documentUrl = `${baseUrl}document.txt`;
|
||||
// PUT
|
||||
let response = await resourceHelper.createResource(
|
||||
'../assets/testfile0.txt', filePath, 'text/plain',
|
||||
);
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE0' });
|
||||
|
||||
// GET
|
||||
response = await resourceHelper.getResource(fileUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response._getBuffer().toString()).toContain('TESTFILE0');
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${fileUrl}.acl>; rel="acl"`);
|
||||
expect(response.getHeaders()['accept-patch']).toBe('application/sparql-update');
|
||||
expect(response.getHeaders()['ms-author-via']).toBe('SPARQL');
|
||||
const response = await getResource(documentUrl, { contentType: 'text/plain' });
|
||||
await expect(response.text()).resolves.toBe('TESTFILE0');
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can add and overwrite a file.', async(): Promise<void> => {
|
||||
const filePath = 'file.txt';
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
it('can add and overwrite a document.', async(): Promise<void> => {
|
||||
const documentUrl = `${baseUrl}document.txt`;
|
||||
// PUT
|
||||
let response = await resourceHelper.createResource(
|
||||
'../assets/testfile0.txt', filePath, 'text/plain',
|
||||
);
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE0' });
|
||||
|
||||
// GET
|
||||
response = await resourceHelper.getResource(fileUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response._getBuffer().toString()).toContain('TESTFILE0');
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${fileUrl}.acl>; rel="acl"`);
|
||||
let response = await getResource(documentUrl, { contentType: 'text/plain' });
|
||||
await expect(response.text()).resolves.toBe('TESTFILE0');
|
||||
|
||||
// PUT
|
||||
response = await resourceHelper.replaceResource(
|
||||
'../assets/testfile1.txt', fileUrl, 'text/plain',
|
||||
);
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE1' });
|
||||
|
||||
// GET
|
||||
response = await resourceHelper.getResource(fileUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response._getBuffer().toString()).toContain('TESTFILE1');
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${fileUrl}.acl>; rel="acl"`);
|
||||
response = await getResource(documentUrl, { contentType: 'text/plain' });
|
||||
await expect(response.text()).resolves.toBe('TESTFILE1');
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can create a folder and delete it.', async(): Promise<void> => {
|
||||
const containerPath = 'secondfolder/';
|
||||
const containerUrl = `${BASE}/${containerPath}`;
|
||||
it('can create a container and delete it.', async(): Promise<void> => {
|
||||
const containerUrl = `${baseUrl}secondContainer/`;
|
||||
// PUT
|
||||
let response = await resourceHelper.createContainer(containerPath);
|
||||
await putResource(containerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// GET
|
||||
response = await resourceHelper.getContainer(containerUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.BasicContainer}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${containerUrl}.acl>; rel="acl"`);
|
||||
await getResource(containerUrl);
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(containerUrl);
|
||||
await resourceHelper.shouldNotExist(containerUrl);
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can make a folder and put a file in it.', async(): Promise<void> => {
|
||||
// Create folder
|
||||
const containerPath = 'testfolder0/';
|
||||
const containerUrl = `${BASE}/${containerPath}`;
|
||||
await resourceHelper.createContainer(containerPath);
|
||||
it('can create a container and put a document in it.', async(): Promise<void> => {
|
||||
// Create container
|
||||
const containerUrl = `${baseUrl}testcontainer0/`;
|
||||
await putResource(containerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Create file
|
||||
const filePath = 'testfolder0/testfile0.txt';
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
let response = await resourceHelper.createResource(
|
||||
'../assets/testfile0.txt', filePath, 'text/plain',
|
||||
);
|
||||
// Create document
|
||||
const documentUrl = `${containerUrl}testdocument0.txt`;
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE0' });
|
||||
|
||||
// GET File
|
||||
response = await resourceHelper.getResource(fileUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${fileUrl}.acl>; rel="acl"`);
|
||||
// GET document
|
||||
const response = await getResource(documentUrl, { contentType: 'text/plain' });
|
||||
await expect(response.text()).resolves.toBe('TESTFILE0');
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
await resourceHelper.deleteResource(containerUrl);
|
||||
await resourceHelper.shouldNotExist(containerUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('cannot remove a folder when the folder contains a file.', async(): Promise<void> => {
|
||||
// Create folder
|
||||
const containerPath = 'testfolder1/';
|
||||
const containerUrl = `${BASE}/${containerPath}`;
|
||||
let response = await resourceHelper.createContainer(containerPath);
|
||||
it('cannot remove a container when the container contains a document.', async(): Promise<void> => {
|
||||
// Create container
|
||||
const containerUrl = `${baseUrl}testfolder1/`;
|
||||
await putResource(containerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Create file
|
||||
const filePath = 'testfolder1/testfile0.txt';
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
await resourceHelper.createResource(
|
||||
'../assets/testfile0.txt', filePath, 'text/plain',
|
||||
);
|
||||
// Create document
|
||||
const documentUrl = `${containerUrl}testdocument0.txt`;
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE0' });
|
||||
|
||||
// Try DELETE folder
|
||||
response = await resourceHelper.performRequest(new URL(containerUrl), 'DELETE', {});
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response._getData()).toContain('ConflictHttpError: Can only delete empty containers.');
|
||||
// Try to DELETE container
|
||||
const response = await fetch(containerUrl, { method: 'DELETE' });
|
||||
expect(response.status).toBe(409);
|
||||
await expect(response.text()).resolves.toContain('ConflictHttpError: Can only delete empty containers.');
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
await resourceHelper.deleteResource(containerUrl);
|
||||
await resourceHelper.shouldNotExist(containerUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('cannot remove a folder when the folder contains a subfolder.', async(): Promise<void> => {
|
||||
// Create folder
|
||||
const containerPath = 'testfolder2/';
|
||||
const containerUrl = `${BASE}/${containerPath}`;
|
||||
let response = await resourceHelper.createContainer(containerPath);
|
||||
it('cannot remove a container when the container contains a subfolder.', async(): Promise<void> => {
|
||||
// Create container
|
||||
const containerUrl = `${baseUrl}testcontainer2/`;
|
||||
await putResource(containerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Create subcontainer
|
||||
const subContainerUrl = `${containerUrl}subcontainer0/`;
|
||||
await putResource(subContainerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Try to DELETE container
|
||||
const response = await fetch(containerUrl, { method: 'DELETE' });
|
||||
expect(response.status).toBe(409);
|
||||
await expect(response.text()).resolves.toContain('ConflictHttpError: Can only delete empty containers.');
|
||||
|
||||
// DELETE
|
||||
expect(await deleteResource(subContainerUrl)).toBeUndefined();
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can read the contents of a container.', async(): Promise<void> => {
|
||||
// Create container
|
||||
const containerUrl = `${baseUrl}testcontainer3/`;
|
||||
await putResource(containerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Create subfolder
|
||||
const subContainerPath = `${containerPath}subfolder0/`;
|
||||
const subContainerUrl = `${BASE}/${subContainerPath}`;
|
||||
response = await resourceHelper.createContainer(subContainerPath);
|
||||
const subContainerUrl = `${containerUrl}subcontainer0`;
|
||||
await putResource(subContainerUrl, { contentType: 'text/turtle' });
|
||||
|
||||
// Try DELETE folder
|
||||
response = await resourceHelper.performRequest(new URL(containerUrl), 'DELETE', {});
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response._getData()).toContain('ConflictHttpError: Can only delete empty containers.');
|
||||
// Create document
|
||||
const documentUrl = `${containerUrl}testfile0.txt`;
|
||||
await putResource(documentUrl, { contentType: 'text/plain', body: 'TESTFILE0' });
|
||||
|
||||
const response = await getResource(containerUrl);
|
||||
await expectQuads(response, [
|
||||
quad(namedNode(containerUrl), LDP.terms.contains, namedNode(subContainerUrl)),
|
||||
quad(namedNode(containerUrl), LDP.terms.contains, namedNode(documentUrl)),
|
||||
]);
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(subContainerUrl);
|
||||
await resourceHelper.shouldNotExist(subContainerUrl);
|
||||
await resourceHelper.deleteResource(containerUrl);
|
||||
await resourceHelper.shouldNotExist(containerUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
expect(await deleteResource(subContainerUrl)).toBeUndefined();
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can read the contents of a folder.', async(): Promise<void> => {
|
||||
// Create folder
|
||||
const containerPath = 'testfolder3/';
|
||||
const containerUrl = `${BASE}/${containerPath}`;
|
||||
let response = await resourceHelper.createContainer(containerPath);
|
||||
|
||||
// Create subfolder
|
||||
const subContainerPath = `${containerPath}subfolder0/`;
|
||||
const subContainerUrl = `${BASE}/${subContainerPath}`;
|
||||
response = await resourceHelper.createContainer('testfolder3/subfolder0/');
|
||||
|
||||
// Create file
|
||||
const filePath = `${containerPath}testfile0.txt`;
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
response = await resourceHelper.createResource(
|
||||
'../assets/testfile0.txt', filePath, 'text/plain',
|
||||
);
|
||||
|
||||
response = await resourceHelper.getContainer(containerUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response._getData()).toContain(`<http://www.w3.org/ns/ldp#contains> <${subContainerUrl}> .`);
|
||||
expect(response._getData()).toContain(`<http://www.w3.org/ns/ldp#contains> <${fileUrl}> .`);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.BasicContainer}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.getHeaders().link).toContain(`<${containerUrl}.acl>; rel="acl"`);
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
await resourceHelper.deleteResource(subContainerUrl);
|
||||
await resourceHelper.shouldNotExist(subContainerUrl);
|
||||
await resourceHelper.deleteResource(containerUrl);
|
||||
await resourceHelper.shouldNotExist(containerUrl);
|
||||
it('can upload and delete an image.', async(): Promise<void> => {
|
||||
const documentUrl = `${baseUrl}image.png`;
|
||||
const response = await fetch(documentUrl, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'image/png',
|
||||
},
|
||||
body: createReadStream(joinFilePath(__dirname, '../assets/testimage.png')) as any,
|
||||
});
|
||||
|
||||
it('can upload and delete a image.', async(): Promise<void> => {
|
||||
const filePath = 'image.png';
|
||||
const fileUrl = `${BASE}/${filePath}`;
|
||||
let response = await resourceHelper.createResource(
|
||||
'../assets/testimage.png', filePath, 'image/png',
|
||||
);
|
||||
expect(response.status).toBe(205);
|
||||
await expect(response.text()).resolves.toHaveLength(0);
|
||||
|
||||
// GET
|
||||
response = await resourceHelper.getResource(fileUrl);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response._getHeaders()['content-type']).toBe('image/png');
|
||||
await getResource(documentUrl, { contentType: 'image/png' });
|
||||
|
||||
// DELETE
|
||||
await resourceHelper.deleteResource(fileUrl);
|
||||
await resourceHelper.shouldNotExist(fileUrl);
|
||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('can create a container with a diamond identifier in the data.', async(): Promise<void> => {
|
||||
const slug = 'my-container';
|
||||
|
||||
let response: MockResponse<any> = await resourceHelper.performRequestWithBody(
|
||||
new URL(ensureTrailingSlash(BASE)),
|
||||
'POST',
|
||||
{
|
||||
'content-type': 'text/turtle',
|
||||
'transfer-encoding': 'chunked',
|
||||
link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"',
|
||||
slug,
|
||||
},
|
||||
Buffer.from('<> <http://www.w3.org/2000/01/rdf-schema#label> "My Container" .', 'utf-8'),
|
||||
);
|
||||
const body = '<> <http://www.w3.org/2000/01/rdf-schema#label> "My Container" .';
|
||||
let response = await postResource(baseUrl, { isContainer: true, contentType: 'text/turtle', slug, body });
|
||||
expect(response.headers.get('location')).toBe(`${baseUrl}${slug}/`);
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
expect(response._getHeaders().location).toBe(`${BASE}/${slug}/`);
|
||||
// GET
|
||||
const containerUrl = `${baseUrl}${slug}/`;
|
||||
response = await getResource(containerUrl);
|
||||
|
||||
response = await resourceHelper.performRequest(new URL(`${BASE}/${slug}/`), 'GET', { accept: 'text/turtle' });
|
||||
expect(response.statusCode).toBe(200);
|
||||
await expectQuads(response, [
|
||||
quad(namedNode(containerUrl), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Container')),
|
||||
]);
|
||||
|
||||
const parser = new Parser({ baseIRI: `${BASE}/${slug}/` });
|
||||
const quads = parser.parse(response._getData());
|
||||
expect(quads.some((entry): boolean => entry.equals(quad(
|
||||
namedNode(`${BASE}/${slug}/`),
|
||||
namedNode('http://www.w3.org/2000/01/rdf-schema#label'),
|
||||
literal('My Container'),
|
||||
)))).toBeTruthy();
|
||||
// DELETE
|
||||
expect(await deleteResource(containerUrl)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
@ -21,6 +21,7 @@
|
||||
],
|
||||
"@graph": [
|
||||
{
|
||||
"comment": "Sets up an HTTP server with only the LDP handler as HttpHandler.",
|
||||
"@id": "urn:solid-server:test:Instances",
|
||||
"@type": "RecordObject",
|
||||
"RecordObject:_record": [
|
||||
@ -41,9 +42,21 @@
|
||||
{
|
||||
"RecordObject:_record_key": "store",
|
||||
"RecordObject:_record_value": { "@id": "urn:solid-server:default:ResourceStore" }
|
||||
},
|
||||
{
|
||||
"RecordObject:_record_key": "factory",
|
||||
"RecordObject:_record_value": { "@id": "urn:solid-server:default:ServerFactory" }
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
{
|
||||
"@id": "urn:solid-server:default:ServerFactory",
|
||||
"@type": "BaseHttpServerFactory",
|
||||
"handler": {
|
||||
"@id": "urn:solid-server:default:LdpHandler"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@id": "urn:solid-server:default:RoutingResourceStore",
|
||||
"@type": "PassthroughStore",
|
||||
|
120
test/util/FetchUtil.ts
Normal file
120
test/util/FetchUtil.ts
Normal file
@ -0,0 +1,120 @@
|
||||
import 'jest-rdf';
|
||||
import type { Response } from 'cross-fetch';
|
||||
import fetch from 'cross-fetch';
|
||||
import type { Quad } from 'n3';
|
||||
import { Parser } from 'n3';
|
||||
import { isContainerPath } from '../../src/util/PathUtil';
|
||||
import { LDP } from '../../src/util/Vocabularies';
|
||||
|
||||
/**
|
||||
* This is specifically for GET requests which are expected to succeed.
|
||||
*/
|
||||
export async function getResource(url: string, expected?: { contentType?: string }): Promise<Response> {
|
||||
const isContainer = isContainerPath(url);
|
||||
const response = await fetch(url);
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get('link')).toContain(`<${LDP.Resource}>; rel="type"`);
|
||||
expect(response.headers.get('link')).toContain(`<${url}.acl>; rel="acl"`);
|
||||
expect(response.headers.get('accept-patch')).toBe('application/sparql-update');
|
||||
expect(response.headers.get('ms-author-via')).toBe('SPARQL');
|
||||
|
||||
if (isContainer) {
|
||||
expect(response.headers.get('link')).toContain(`<${LDP.Container}>; rel="type"`);
|
||||
expect(response.headers.get('link')).toContain(`<${LDP.BasicContainer}>; rel="type"`);
|
||||
}
|
||||
if (expected?.contentType) {
|
||||
expect(response.headers.get('content-type')).toBe(expected.contentType);
|
||||
} else if (isContainer) {
|
||||
expect(response.headers.get('content-type')).toBe('text/turtle');
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is specifically for PUT requests which are expected to succeed.
|
||||
*/
|
||||
export async function putResource(url: string, options: { contentType: string; body?: string }):
|
||||
Promise<Response> {
|
||||
const init: RequestInit = {
|
||||
method: 'PUT',
|
||||
headers: { 'content-type': options.contentType },
|
||||
body: options.body,
|
||||
};
|
||||
if (isContainerPath(url)) {
|
||||
(init.headers as Record<string, string>).link = '<http://www.w3.org/ns/ldp#Container>; rel="type"';
|
||||
}
|
||||
const response = await fetch(url, init);
|
||||
expect(response.status).toBe(205);
|
||||
await expect(response.text()).resolves.toHaveLength(0);
|
||||
return response;
|
||||
}
|
||||
|
||||
export type CreateOptions = {
|
||||
contentType: string;
|
||||
isContainer?: boolean;
|
||||
slug?: string;
|
||||
body?: string;
|
||||
};
|
||||
/**
|
||||
* This is specifically for POST requests which are expected to succeed.
|
||||
*/
|
||||
export async function postResource(container: string, options: CreateOptions): Promise<Response> {
|
||||
const init: RequestInit = {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': options.contentType },
|
||||
body: options.body,
|
||||
};
|
||||
if (options.isContainer) {
|
||||
(init.headers as Record<string, string>).link = '<http://www.w3.org/ns/ldp#Container>; rel="type"';
|
||||
}
|
||||
if (options.slug) {
|
||||
(init.headers as Record<string, string>).slug = options.slug;
|
||||
}
|
||||
const response = await fetch(container, init);
|
||||
await expect(response.text()).resolves.toHaveLength(0);
|
||||
expect(response.status).toBe(201);
|
||||
const regex = new RegExp(`^${container}[^/]+${options.isContainer ? '/' : ''}`, 'u');
|
||||
expect(response.headers.get('location')).toMatch(regex);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is specifically for PATCH requests which are expected to succeed.
|
||||
*/
|
||||
export async function patchResource(url: string, query: string): Promise<Response> {
|
||||
const response = await fetch(url, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'content-type': 'application/sparql-update',
|
||||
},
|
||||
body: query,
|
||||
});
|
||||
await expect(response.text()).resolves.toHaveLength(0);
|
||||
expect(response.status).toBe(205);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function deleteResource(url: string): Promise<void> {
|
||||
let response = await fetch(url, { method: 'DELETE' });
|
||||
expect(response.status).toBe(205);
|
||||
response = await fetch(url);
|
||||
expect(response.status).toBe(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the body of the given Response contains the expected Quads.
|
||||
* If `exact` is true, a 1-to-1 match is expected, if not, the expected quads should be a subset of the body.
|
||||
*/
|
||||
export async function expectQuads(response: Response, expected: Quad[], exact?: boolean): Promise<void> {
|
||||
const parser = new Parser({ baseIRI: response.url });
|
||||
const quads = parser.parse(await response.text());
|
||||
if (exact) {
|
||||
expect(quads).toBeRdfIsomorphic(expected);
|
||||
} else {
|
||||
for (const expectedQuad of expected) {
|
||||
expect(quads.some((entry): boolean => entry.equals(expectedQuad))).toBe(true);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import type { SystemError } from '../../src/util/errors/SystemError';
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
const portNames = [
|
||||
'DynamicPods',
|
||||
'LpdHandlerWithoutAuth',
|
||||
'Middleware',
|
||||
'PodCreation',
|
||||
'RedisResourceLocker',
|
||||
|
Loading…
x
Reference in New Issue
Block a user