Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion utils/rdflib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- [mockTurtleDocument](https://solid-contrib.github.io/data-modules/rdflib-utils/functions/test_support.mockTurtleDocument.html)
now allows to pass additional headers to include into the mock response
- [mockLdpContainer](https://solid-contrib.github.io/data-modules/rdflib-utils/functions/test_support.mockLdpContainer.html)
now allows to pass additional headers to include into the mock response

## 0.5.0

### Changes
### Changed

- [mockLdpContainer](https://solid-contrib.github.io/data-modules/rdflib-utils/functions/test_support.mockLdpContainer.html)
now allows to add additional turtle to include into the mock response
Expand Down
72 changes: 70 additions & 2 deletions utils/rdflib/src/test-support/mockResponses.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
import { mockLdpContainer } from "./mockResponses";
import { mockLdpContainer, mockTurtleDocument } from "./mockResponses";

describe("mockResponses", () => {
describe("mockLdpContainer", () => {
describe(mockTurtleDocument.name, () => {
it("mocks a turtle document body", async () => {
const fetch = jest.fn();
mockTurtleDocument(
fetch,
"http://document.test/",
`<> <> "Some content" .`,
);
const result = await fetch("http://document.test/", {});
expect(await result.text()).toEqual('<> <> "Some content" .');
});

it("mocks standard turtle document headers", async () => {
const fetch = jest.fn();
mockTurtleDocument(
fetch,
"http://document.test/",
`<> <> "Some content" .`,
);
const result: Response = await fetch("http://document.test/", {});
expect(result.headers.get("Content-Type")).toEqual("text/turtle");
expect(result.headers.get("Link")).toEqual(
'<http://www.w3.org/ns/ldp#Resource>; rel="type"',
);
expect(result.headers.get("Wac-Allow")).toEqual(
'user="read write append control",public="read"',
);
expect(result.headers.get("Accept-Patch")).toEqual("text/n3");
});

it("mocks additional headers as provided", async () => {
const fetch = jest.fn();
mockTurtleDocument(
fetch,
"http://document.test/",
`<> <> "Some content" .`,
{
"X-My-Header": "MyValue",
},
);
const result: Response = await fetch("http://document.test/", {});
expect(result.headers.get("X-My-Header")).toEqual("MyValue");
});
});

describe(mockLdpContainer.name, () => {
it("mocks a container without contents", async () => {
const fetch = jest.fn();
mockLdpContainer(fetch, "http://container.test/");
Expand Down Expand Up @@ -54,5 +99,28 @@ describe("mockResponses", () => {
.
<http://container.test/one> a ldp:Container .`);
});

it("mocks standard ldp container headers", async () => {
const fetch = jest.fn();
mockLdpContainer(fetch, "http://container.test/");
const result: Response = await fetch("http://container.test/", {});
expect(result.headers.get("Content-Type")).toEqual("text/turtle");
expect(result.headers.get("Link")).toEqual(
'<http://www.w3.org/ns/ldp#Container>; rel="type"',
);
expect(result.headers.get("Wac-Allow")).toEqual(
'user="read write append control",public="read"',
);
expect(result.headers.get("Accept-Patch")).toEqual("text/n3");
});

it("mocks additional headers as provided", async () => {
const fetch = jest.fn();
mockLdpContainer(fetch, "http://document.test/", undefined, undefined, {
"X-My-Header": "MyValue",
});
const result: Response = await fetch("http://document.test/", {});
expect(result.headers.get("X-My-Header")).toEqual("MyValue");
});
});
});
12 changes: 11 additions & 1 deletion utils/rdflib/src/test-support/mockResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { when } from "jest-when";
* @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: jest.Mock, url: string, ttl: string) {
export function mockTurtleDocument(
fetch: jest.Mock,
url: string,
ttl: string,
additionalHeaders: Record<string, string> = {},
) {
when(fetch)
.calledWith(url, expect.anything())
.mockResolvedValue({
Expand All @@ -18,6 +24,7 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
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),
} as Response);
Expand All @@ -29,12 +36,14 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
* @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: jest.Mock,
url: string,
contains: string[] = [],
moreTurtle: string = "",
additionalHeaders: Record<string, string> = {},
) {
when(fetch)
.calledWith(url, expect.anything())
Expand All @@ -47,6 +56,7 @@ export function mockLdpContainer(
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(`
Expand Down