From 4cc1b7f3fa46c5e8e23cc37bf004ea1768edb284 Mon Sep 17 00:00:00 2001 From: Angelo Veltens Date: Sat, 10 May 2025 22:05:00 +0200 Subject: [PATCH 1/4] rdflib-utils: allow to mock additional headers for turtle documents --- .../src/test-support/mockResponses.spec.ts | 47 ++++++++++++++++++- .../rdflib/src/test-support/mockResponses.ts | 9 +++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/utils/rdflib/src/test-support/mockResponses.spec.ts b/utils/rdflib/src/test-support/mockResponses.spec.ts index 7d28ad8..e8dc04d 100644 --- a/utils/rdflib/src/test-support/mockResponses.spec.ts +++ b/utils/rdflib/src/test-support/mockResponses.spec.ts @@ -1,6 +1,51 @@ -import { mockLdpContainer } from "./mockResponses"; +import { mockLdpContainer, mockTurtleDocument } from "./mockResponses"; describe("mockResponses", () => { + describe("mockTurtleResponse", () => { + 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( + '; 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", () => { it("mocks a container without contents", async () => { const fetch = jest.fn(); diff --git a/utils/rdflib/src/test-support/mockResponses.ts b/utils/rdflib/src/test-support/mockResponses.ts index fe6ac4e..027b2b3 100644 --- a/utils/rdflib/src/test-support/mockResponses.ts +++ b/utils/rdflib/src/test-support/mockResponses.ts @@ -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 = {}, +) { when(fetch) .calledWith(url, expect.anything()) .mockResolvedValue({ @@ -18,6 +24,7 @@ export function mockTurtleDocument(fetch: jest.Mock, url: string, ttl: string) { link: '; rel="type"', "wac-allow": 'user="read write append control",public="read"', "accept-patch": "text/n3", + ...additionalHeaders, }), text: () => Promise.resolve(ttl), } as Response); From a73eaf7a93b1286c2564ecc3bd6bc479dc26be45 Mon Sep 17 00:00:00 2001 From: Angelo Veltens Date: Sat, 10 May 2025 22:12:33 +0200 Subject: [PATCH 2/4] rdflib-utils: allow to mock additional headers for ldp containers --- .../src/test-support/mockResponses.spec.ts | 23 +++++++++++++++++++ .../rdflib/src/test-support/mockResponses.ts | 3 +++ 2 files changed, 26 insertions(+) diff --git a/utils/rdflib/src/test-support/mockResponses.spec.ts b/utils/rdflib/src/test-support/mockResponses.spec.ts index e8dc04d..eac8f89 100644 --- a/utils/rdflib/src/test-support/mockResponses.spec.ts +++ b/utils/rdflib/src/test-support/mockResponses.spec.ts @@ -99,5 +99,28 @@ describe("mockResponses", () => { . 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( + '; 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"); + }); }); }); diff --git a/utils/rdflib/src/test-support/mockResponses.ts b/utils/rdflib/src/test-support/mockResponses.ts index 027b2b3..96012d9 100644 --- a/utils/rdflib/src/test-support/mockResponses.ts +++ b/utils/rdflib/src/test-support/mockResponses.ts @@ -36,12 +36,14 @@ export function mockTurtleDocument( * @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 = {}, ) { when(fetch) .calledWith(url, expect.anything()) @@ -54,6 +56,7 @@ export function mockLdpContainer( link: '; rel="type"', "wac-allow": 'user="read write append control",public="read"', "accept-patch": "text/n3", + ...additionalHeaders, }), text: () => Promise.resolve(` From 802d049cf3f511cfce5dab96de5d9a513e32eece Mon Sep 17 00:00:00 2001 From: Angelo Veltens Date: Sat, 10 May 2025 22:14:25 +0200 Subject: [PATCH 3/4] rdflib-utils: fix name of describe block and make sure they stay up-to-day with method under test --- utils/rdflib/src/test-support/mockResponses.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/rdflib/src/test-support/mockResponses.spec.ts b/utils/rdflib/src/test-support/mockResponses.spec.ts index eac8f89..71dd742 100644 --- a/utils/rdflib/src/test-support/mockResponses.spec.ts +++ b/utils/rdflib/src/test-support/mockResponses.spec.ts @@ -1,7 +1,7 @@ import { mockLdpContainer, mockTurtleDocument } from "./mockResponses"; describe("mockResponses", () => { - describe("mockTurtleResponse", () => { + describe(mockTurtleDocument.name, () => { it("mocks a turtle document body", async () => { const fetch = jest.fn(); mockTurtleDocument( @@ -46,7 +46,7 @@ describe("mockResponses", () => { }); }); - describe("mockLdpContainer", () => { + describe(mockLdpContainer.name, () => { it("mocks a container without contents", async () => { const fetch = jest.fn(); mockLdpContainer(fetch, "http://container.test/"); From 899aee333c7129777702d88bdefd8cea8d6793ec Mon Sep 17 00:00:00 2001 From: Angelo Veltens Date: Sat, 10 May 2025 22:16:55 +0200 Subject: [PATCH 4/4] rdflib-utils: update changelog --- utils/rdflib/CHANGELOG.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/utils/rdflib/CHANGELOG.md b/utils/rdflib/CHANGELOG.md index b6e349e..c6a7a56 100644 --- a/utils/rdflib/CHANGELOG.md +++ b/utils/rdflib/CHANGELOG.md @@ -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