mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Merge LdpHandlerOperation with LdpHandlerWithoutAuth
This commit is contained in:
parent
4083d24e4a
commit
d04ab2951b
@ -6,7 +6,7 @@ import { joinFilePath, PIM, RDF } from '../../src/';
|
|||||||
import type { Initializer, ResourceStore } from '../../src/';
|
import type { Initializer, ResourceStore } from '../../src/';
|
||||||
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
import type { HttpServerFactory } from '../../src/server/HttpServerFactory';
|
||||||
import { LDP } from '../../src/util/Vocabularies';
|
import { LDP } from '../../src/util/Vocabularies';
|
||||||
import { deleteResource, expectQuads, getResource, postResource, putResource } from '../util/FetchUtil';
|
import { deleteResource, expectQuads, getResource, patchResource, postResource, putResource } from '../util/FetchUtil';
|
||||||
import { getPort } from '../util/Util';
|
import { getPort } from '../util/Util';
|
||||||
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
|
import { getTestFolder, instantiateFromConfig, removeFolder } from './Config';
|
||||||
const { literal, namedNode, quad } = DataFactory;
|
const { literal, namedNode, quad } = DataFactory;
|
||||||
@ -26,7 +26,7 @@ const stores: [string, any][] = [
|
|||||||
}],
|
}],
|
||||||
];
|
];
|
||||||
|
|
||||||
describe.each(stores)('An LDP handler allowing all request %s', (name, { storeUrn, teardown }): void => {
|
describe.each(stores)('An LDP handler allowing all requests %s', (name, { storeUrn, teardown }): void => {
|
||||||
let server: Server;
|
let server: Server;
|
||||||
let initializer: Initializer;
|
let initializer: Initializer;
|
||||||
let factory: HttpServerFactory;
|
let factory: HttpServerFactory;
|
||||||
@ -271,4 +271,53 @@ describe.each(stores)('An LDP handler allowing all request %s', (name, { storeUr
|
|||||||
// DELETE
|
// DELETE
|
||||||
expect(await deleteResource(documentUrl)).toBeUndefined();
|
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle simple SPARQL updates.', async(): Promise<void> => {
|
||||||
|
// POST
|
||||||
|
const body = [ '<http://test.com/s1> <http://test.com/p1> <http://test.com/o1>.',
|
||||||
|
'<http://test.com/s2> <http://test.com/p2> <http://test.com/o2>.' ].join('\n');
|
||||||
|
let response = await postResource(baseUrl, { contentType: 'text/turtle', body });
|
||||||
|
const documentUrl = response.headers.get('location')!;
|
||||||
|
|
||||||
|
// PATCH
|
||||||
|
const query = [ 'DELETE { <http://test.com/s1> <http://test.com/p1> <http://test.com/o1> }',
|
||||||
|
'INSERT {<http://test.com/s3> <http://test.com/p3> <http://test.com/o3>}',
|
||||||
|
'WHERE {}',
|
||||||
|
].join('\n');
|
||||||
|
await patchResource(documentUrl, query);
|
||||||
|
|
||||||
|
// PATCH using a content-type header with charset
|
||||||
|
const query2 = [ 'DELETE { <http://test.com/s2> <http://test.com/p2> <http://test.com/o2> }',
|
||||||
|
'INSERT {<#s4> <#p4> <#o4>}',
|
||||||
|
'WHERE {}',
|
||||||
|
].join('\n');
|
||||||
|
response = await fetch(documentUrl, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/sparql-update ; charset=UTF-8',
|
||||||
|
},
|
||||||
|
body: query2,
|
||||||
|
});
|
||||||
|
await expect(response.text()).resolves.toHaveLength(0);
|
||||||
|
expect(response.status).toBe(205);
|
||||||
|
|
||||||
|
// GET
|
||||||
|
response = await getResource(documentUrl);
|
||||||
|
const expected = [
|
||||||
|
quad(
|
||||||
|
namedNode('http://test.com/s3'),
|
||||||
|
namedNode('http://test.com/p3'),
|
||||||
|
namedNode('http://test.com/o3'),
|
||||||
|
),
|
||||||
|
quad(
|
||||||
|
namedNode(`${documentUrl}#s4`),
|
||||||
|
namedNode(`${documentUrl}#p4`),
|
||||||
|
namedNode(`${documentUrl}#o4`),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
await expectQuads(response, expected, true);
|
||||||
|
|
||||||
|
// DELETE
|
||||||
|
expect(await deleteResource(documentUrl)).toBeUndefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,231 +0,0 @@
|
|||||||
import 'jest-rdf';
|
|
||||||
import * as url from 'url';
|
|
||||||
import { namedNode, quad } from '@rdfjs/data-model';
|
|
||||||
import { Parser } from 'n3';
|
|
||||||
import type { MockResponse } from 'node-mocks-http';
|
|
||||||
import type { HttpHandler } from '../../src/server/HttpHandler';
|
|
||||||
import { LDP } from '../../src/util/Vocabularies';
|
|
||||||
import { performRequest } from '../util/Util';
|
|
||||||
import { BASE, instantiateFromConfig } from './Config';
|
|
||||||
|
|
||||||
describe('An integrated AuthenticatedLdpHandler', (): void => {
|
|
||||||
let handler: HttpHandler;
|
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
|
||||||
handler = await instantiateFromConfig(
|
|
||||||
'urn:solid-server:default:LdpHandler', 'server-without-auth.json', {
|
|
||||||
'urn:solid-server:default:variable:baseUrl': BASE,
|
|
||||||
},
|
|
||||||
) as HttpHandler;
|
|
||||||
|
|
||||||
// The tests depend on there being a root container here
|
|
||||||
await performRequest(
|
|
||||||
handler,
|
|
||||||
new URL('http://test.com/'),
|
|
||||||
'PUT',
|
|
||||||
{ 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' },
|
|
||||||
[ ],
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can add, read and delete data based on incoming requests.', async(): Promise<void> => {
|
|
||||||
// POST
|
|
||||||
let requestUrl = new URL('http://test.com/');
|
|
||||||
let response: MockResponse<any> = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'POST',
|
|
||||||
{ 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' },
|
|
||||||
[ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(201);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
const id = response._getHeaders().location;
|
|
||||||
expect(id).toContain(url.format(requestUrl));
|
|
||||||
|
|
||||||
// GET
|
|
||||||
requestUrl = new URL(id);
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'GET',
|
|
||||||
{ accept: 'text/turtle' },
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
expect(response._getData()).toContain(
|
|
||||||
'<http://test.com/s> <http://test.com/p> <http://test.com/o>.',
|
|
||||||
);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${id}.acl>; rel="acl"`);
|
|
||||||
|
|
||||||
// DELETE
|
|
||||||
response = await performRequest(handler, requestUrl, 'DELETE', {}, []);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
|
|
||||||
// GET
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'GET',
|
|
||||||
{ accept: 'text/turtle' },
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(404);
|
|
||||||
expect(response._getData()).toContain('NotFoundHttpError');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can handle simple SPARQL updates.', async(): Promise<void> => {
|
|
||||||
// POST
|
|
||||||
let requestUrl = new URL('http://test.com/');
|
|
||||||
let response: MockResponse<any> = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'POST',
|
|
||||||
{ 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' },
|
|
||||||
[ '<http://test.com/s1> <http://test.com/p1> <http://test.com/o1>.',
|
|
||||||
'<s2> <http://test.com/p2> <http://test.com/o2>.' ],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(201);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
const id = response._getHeaders().location;
|
|
||||||
expect(id).toContain(url.format(requestUrl));
|
|
||||||
|
|
||||||
// PATCH
|
|
||||||
requestUrl = new URL(id);
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'PATCH',
|
|
||||||
{ 'content-type': 'application/sparql-update', 'transfer-encoding': 'chunked' },
|
|
||||||
[ 'DELETE { <s1> <http://test.com/p1> <http://test.com/o1> }',
|
|
||||||
'INSERT {<s3> <http://test.com/p3> <http://test.com/o3>}',
|
|
||||||
'WHERE {}',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
|
|
||||||
// PATCH using a content-type header with charset
|
|
||||||
requestUrl = new URL(id);
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'PATCH',
|
|
||||||
{ 'content-type': ' application/sparql-update ; charset=UTF-8', 'transfer-encoding': 'chunked' },
|
|
||||||
[ 'DELETE { <s1> <http://test.com/p1> <http://test.com/o1> }',
|
|
||||||
'INSERT {<s3> <http://test.com/p3> <http://test.com/o3>}',
|
|
||||||
'WHERE {}',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
|
|
||||||
// GET
|
|
||||||
response = await performRequest(handler, requestUrl, 'GET', { accept: 'text/turtle' }, []);
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
expect(response._getData()).toContain(
|
|
||||||
'<http://test.com/s2> <http://test.com/p2> <http://test.com/o2>.',
|
|
||||||
);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${id}.acl>; rel="acl"`);
|
|
||||||
const parser = new Parser();
|
|
||||||
let triples = parser.parse(response._getData());
|
|
||||||
expect(triples).toBeRdfIsomorphic([
|
|
||||||
quad(
|
|
||||||
namedNode('http://test.com/s2'),
|
|
||||||
namedNode('http://test.com/p2'),
|
|
||||||
namedNode('http://test.com/o2'),
|
|
||||||
),
|
|
||||||
quad(
|
|
||||||
namedNode('http://test.com/s3'),
|
|
||||||
namedNode('http://test.com/p3'),
|
|
||||||
namedNode('http://test.com/o3'),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// PATCH
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'PATCH',
|
|
||||||
{ 'content-type': 'application/sparql-update', 'transfer-encoding': 'chunked' },
|
|
||||||
[ 'DELETE DATA { <s2> <http://test.com/p2> <http://test.com/o2> }; ',
|
|
||||||
'INSERT DATA {<s4> <http://test.com/p4> <http://test.com/o4>}',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
|
|
||||||
// GET
|
|
||||||
response = await performRequest(handler, requestUrl, 'GET', { accept: 'text/turtle' }, []);
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
triples = parser.parse(response._getData());
|
|
||||||
expect(triples).toBeRdfIsomorphic([
|
|
||||||
quad(
|
|
||||||
namedNode('http://test.com/s3'),
|
|
||||||
namedNode('http://test.com/p3'),
|
|
||||||
namedNode('http://test.com/o3'),
|
|
||||||
),
|
|
||||||
quad(
|
|
||||||
namedNode('http://test.com/s4'),
|
|
||||||
namedNode('http://test.com/p4'),
|
|
||||||
namedNode('http://test.com/o4'),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should overwrite the content on PUT request.', async(): Promise<void> => {
|
|
||||||
// POST
|
|
||||||
let requestUrl = new URL('http://test.com/');
|
|
||||||
let response: MockResponse<any> = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'POST',
|
|
||||||
{ 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' },
|
|
||||||
[
|
|
||||||
'<http://test.com/s1> <http://test.com/p1> <http://test.com/o1>.',
|
|
||||||
'<http://test.com/s2> <http://test.com/p2> <http://test.com/o2>.',
|
|
||||||
],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(201);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
const id = response._getHeaders().location;
|
|
||||||
expect(id).toContain(url.format(requestUrl));
|
|
||||||
|
|
||||||
// PUT
|
|
||||||
requestUrl = new URL(id);
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'PUT',
|
|
||||||
{ 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' },
|
|
||||||
[ '<http://test.com/s3> <http://test.com/p3> <http://test.com/o3>.' ],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(205);
|
|
||||||
expect(response._getData()).toHaveLength(0);
|
|
||||||
|
|
||||||
// GET
|
|
||||||
requestUrl = new URL(id);
|
|
||||||
response = await performRequest(
|
|
||||||
handler,
|
|
||||||
requestUrl,
|
|
||||||
'GET',
|
|
||||||
{ accept: 'text/turtle' },
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
expect(response.statusCode).toBe(200);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${LDP.Resource}>; rel="type"`);
|
|
||||||
expect(response.getHeaders().link).toContain(`<${id}.acl>; rel="acl"`);
|
|
||||||
const parser = new Parser();
|
|
||||||
const triples = parser.parse(response._getData());
|
|
||||||
expect(triples).toBeRdfIsomorphic([
|
|
||||||
quad(
|
|
||||||
namedNode('http://test.com/s3'),
|
|
||||||
namedNode('http://test.com/p3'),
|
|
||||||
namedNode('http://test.com/o3'),
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user