@solid-data-modules/rdflib-utils
Version:
Utility functions for the development of Solid Data Modules for RDFLib.js
98 lines (97 loc) • 3.37 kB
JavaScript
import { when } from "jest-when";
/**
* Mock a turtle document at the given URL
* @param fetch - A mocked fetch function
* @param url - The URL to mock
* @param ttl - The mocked turtle file content
* @param additionalHeaders - Additional headers to include in the response
*/
export function mockTurtleDocument(fetch, url, ttl, additionalHeaders = {}) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
ok: true,
status: 200,
statusText: "OK",
headers: new Headers({
"Content-Type": "text/turtle",
link: '<http://www.w3.org/ns/ldp#Resource>; rel="type"',
"wac-allow": 'user="read write append control",public="read"',
"accept-patch": "text/n3",
...additionalHeaders,
}),
text: () => Promise.resolve(ttl),
});
}
/**
* Mock a LDP container at the given URL
* @param fetch - A mocked fetch function
* @param url - The URL to mock
* @param contains - List of URLs of documents contained in this container
* @param moreTurtle - Additional turtle to include into the response
* @param additionalHeaders - Additional headers to include in the response
*/
export function mockLdpContainer(fetch, url, contains = [], moreTurtle = "", additionalHeaders = {}) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
ok: true,
status: 200,
statusText: "OK",
headers: new Headers({
"Content-Type": "text/turtle",
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
"wac-allow": 'user="read write append control",public="read"',
"accept-patch": "text/n3",
...additionalHeaders,
}),
text: () => Promise.resolve(`
@prefix dc: <http://purl.org/dc/terms/>.
@prefix ldp: <http://www.w3.org/ns/ldp#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
<> a ldp:Container, ldp:BasicContainer, ldp:Resource ;
${contains.map((it) => `ldp:contains <${it}>`).join("; ")}
.
${moreTurtle}`),
});
}
/**
* Mock a 404 - Not Found response for the given URL
* @param fetch - A mocked fetch function
* @param url - The URL to mock
*/
export function mockNotFound(fetch, url) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
ok: true,
status: 404,
statusText: "Not Found",
headers: new Headers({
"Content-Type": "text/plain",
"wac-allow": 'user="read write append control",public="read"',
"accept-patch": "text/n3",
}),
text: () => Promise.resolve("Not Found"),
});
}
/**
* Mock a 403 - Forbidden response for the given URL
* @param fetch - A mocked fetch function
* @param url - The URL to mock
*/
export function mockForbidden(fetch, url) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
ok: true,
status: 403,
statusText: "Forbidden",
headers: new Headers({
"Content-Type": "text/plain",
"wac-allow": 'user="read write append control",public=""',
}),
text: () => Promise.resolve("You do not have access to this resource."),
});
}
//# sourceMappingURL=mockResponses.js.map