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 diff --git a/utils/rdflib/src/test-support/mockResponses.spec.ts b/utils/rdflib/src/test-support/mockResponses.spec.ts index 7d28ad8..71dd742 100644 --- a/utils/rdflib/src/test-support/mockResponses.spec.ts +++ b/utils/rdflib/src/test-support/mockResponses.spec.ts @@ -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( + '; 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/"); @@ -54,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 fe6ac4e..96012d9 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); @@ -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 = {}, ) { when(fetch) .calledWith(url, expect.anything()) @@ -47,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(`