Skip to content

Commit 099ba8c

Browse files
committed
rdflib-utils: allow to mock additional headers for turtle documents
1 parent 17aadad commit 099ba8c

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

utils/rdflib/src/test-support/mockResponses.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
import { mockLdpContainer } from "./mockResponses";
1+
import { mockLdpContainer, mockTurtleDocument } from "./mockResponses";
22

33
describe("mockResponses", () => {
4+
describe("mockTurtleResponse", () => {
5+
it("mocks a turtle document body", async () => {
6+
const fetch = jest.fn();
7+
mockTurtleDocument(
8+
fetch,
9+
"http://document.test/",
10+
`<> <> "Some content" .`,
11+
);
12+
const result = await fetch("http://document.test/", {});
13+
expect(await result.text()).toEqual('<> <> "Some content" .');
14+
});
15+
16+
it("mocks standard turtle document headers", async () => {
17+
const fetch = jest.fn();
18+
mockTurtleDocument(
19+
fetch,
20+
"http://document.test/",
21+
`<> <> "Some content" .`,
22+
);
23+
const result: Response = await fetch("http://document.test/", {});
24+
expect(result.headers.get("Content-Type")).toEqual("text/turtle");
25+
expect(result.headers.get("Link")).toEqual(
26+
'<http://www.w3.org/ns/ldp#Resource>; rel="type"',
27+
);
28+
expect(result.headers.get("Wac-Allow")).toEqual(
29+
'user="read write append control",public="read"',
30+
);
31+
expect(result.headers.get("Accept-Patch")).toEqual("text/n3");
32+
});
33+
34+
it("mocks additional headers as provided", async () => {
35+
const fetch = jest.fn();
36+
mockTurtleDocument(
37+
fetch,
38+
"http://document.test/",
39+
`<> <> "Some content" .`,
40+
{
41+
"X-My-Header": "MyValue",
42+
},
43+
);
44+
const result: Response = await fetch("http://document.test/", {});
45+
expect(result.headers.get("X-My-Header")).toEqual("MyValue");
46+
});
47+
});
48+
449
describe("mockLdpContainer", () => {
550
it("mocks a container without contents", async () => {
651
const fetch = jest.fn();

utils/rdflib/src/test-support/mockResponses.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import { when } from "jest-when";
55
* @param fetch - A mocked fetch function
66
* @param url - The URL to mock
77
* @param ttl - The mocked turtle file content
8+
* @param additionalHeaders - Additional headers to include in the response
89
*/
9-
export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
10+
export function mockTurtleDocument(
11+
fetch: jest.Mock,
12+
url: string,
13+
ttl: string,
14+
additionalHeaders: Record<string, string> = {},
15+
) {
1016
when(fetch)
1117
.calledWith(url, expect.anything())
1218
.mockResolvedValue({
@@ -18,6 +24,7 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) {
1824
link: '<http://www.w3.org/ns/ldp#Resource>; rel="type"',
1925
"wac-allow": 'user="read write append control",public="read"',
2026
"accept-patch": "text/n3",
27+
...additionalHeaders,
2128
}),
2229
text: () => Promise.resolve(ttl),
2330
} as Response);

0 commit comments

Comments
 (0)