diff --git a/src/hydra/fetchJsonLd.test.ts b/src/hydra/fetchJsonLd.test.ts index 68c168e..ce9f4f7 100644 --- a/src/hydra/fetchJsonLd.test.ts +++ b/src/hydra/fetchJsonLd.test.ts @@ -154,6 +154,40 @@ test("fetch an error with Content-Type application/error+json", async () => { ); }); +test("fetch with an explicit empty options object still sends the default Accept header", async () => { + let receivedAccept: string | null = null; + + server.use( + http.get("http://localhost/foo.jsonld", ({ request }) => { + receivedAccept = request.headers.get("Accept"); + return Response.json(httpResponse, { + headers: { "Content-Type": "application/ld+json" }, + status: 200, + statusText: "OK", + }); + }), + ); + + await fetchJsonLd("http://localhost/foo.jsonld", {}); + expect(receivedAccept).toBe("application/ld+json"); +}); + +test("fetch does not mutate the passed options object", async () => { + server.use( + http.get("http://localhost/foo.jsonld", () => + Response.json(httpResponse, { + headers: { "Content-Type": "application/ld+json" }, + status: 200, + statusText: "OK", + }), + ), + ); + + const options = {}; + await fetchJsonLd("http://localhost/foo.jsonld", options); + expect(options).toEqual({}); +}); + test("fetch an empty document", async () => { server.use( http.get( diff --git a/src/hydra/fetchJsonLd.ts b/src/hydra/fetchJsonLd.ts index 99be05c..349e707 100644 --- a/src/hydra/fetchJsonLd.ts +++ b/src/hydra/fetchJsonLd.ts @@ -61,12 +61,10 @@ export default async function fetchJsonLd( } function setHeaders(options: RequestInitExtended): RequestInit { - if (!options.headers) { - return { ...options, headers: {} }; - } - let headers = - typeof options.headers === "function" ? options.headers() : options.headers; + typeof options.headers === "function" + ? options.headers() + : (options.headers ?? {}); headers = new Headers(headers);