Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/hydra/fetchJsonLd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions src/hydra/fetchJsonLd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading