Skip to content

Commit 19d4cc5

Browse files
committed
rdflib-utils: allow to mock additional headers for ldp containers
1 parent 099ba8c commit 19d4cc5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,28 @@ describe("mockResponses", () => {
9999
.
100100
<http://container.test/one> a ldp:Container .`);
101101
});
102+
103+
it("mocks standard ldp container headers", async () => {
104+
const fetch = jest.fn();
105+
mockLdpContainer(fetch, "http://container.test/");
106+
const result: Response = await fetch("http://container.test/", {});
107+
expect(result.headers.get("Content-Type")).toEqual("text/turtle");
108+
expect(result.headers.get("Link")).toEqual(
109+
'<http://www.w3.org/ns/ldp#Container>; rel="type"',
110+
);
111+
expect(result.headers.get("Wac-Allow")).toEqual(
112+
'user="read write append control",public="read"',
113+
);
114+
expect(result.headers.get("Accept-Patch")).toEqual("text/n3");
115+
});
116+
117+
it("mocks additional headers as provided", async () => {
118+
const fetch = jest.fn();
119+
mockLdpContainer(fetch, "http://document.test/", undefined, undefined, {
120+
"X-My-Header": "MyValue",
121+
});
122+
const result: Response = await fetch("http://document.test/", {});
123+
expect(result.headers.get("X-My-Header")).toEqual("MyValue");
124+
});
102125
});
103126
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export function mockTurtleDocument(
3636
* @param url - The URL to mock
3737
* @param contains - List of URLs of documents contained in this container
3838
* @param moreTurtle - Additional turtle to include into the response
39+
* @param additionalHeaders - Additional headers to include in the response
3940
*/
4041
export function mockLdpContainer(
4142
fetch: jest.Mock,
4243
url: string,
4344
contains: string[] = [],
4445
moreTurtle: string = "",
46+
additionalHeaders: Record<string, string> = {},
4547
) {
4648
when(fetch)
4749
.calledWith(url, expect.anything())
@@ -54,6 +56,7 @@ export function mockLdpContainer(
5456
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
5557
"wac-allow": 'user="read write append control",public="read"',
5658
"accept-patch": "text/n3",
59+
...additionalHeaders,
5760
}),
5861
text: () =>
5962
Promise.resolve(`

0 commit comments

Comments
 (0)