diff --git a/src/hydra/fetchJsonLd.test.ts b/src/hydra/fetchJsonLd.test.ts index 962aed4..68c168e 100644 --- a/src/hydra/fetchJsonLd.test.ts +++ b/src/hydra/fetchJsonLd.test.ts @@ -31,7 +31,7 @@ test("fetch a JSON-LD document", async () => { expect(data.body["name"]).toBe("John Lennon"); }); -test("fetch a non JSON-LD document", async () => { +test("fetch a non JSON-LD document with 2xx returns empty response", async () => { server.use( http.get( "http://localhost/foo.jsonld", @@ -44,12 +44,72 @@ test("fetch a non JSON-LD document", async () => { ), ); + const data = await fetchJsonLd("http://localhost/foo.jsonld"); + expect(data.response.ok).toBe(true); + expect(data).not.toHaveProperty("body"); +}); + +test("fetch a non JSON-LD document with non-2xx rejects", async () => { + server.use( + http.get( + "http://localhost/foo.jsonld", + () => + new Response(`Not Found`, { + headers: { "Content-Type": "text/html" }, + status: 404, + statusText: "Not Found", + }), + ), + ); + const promise = fetchJsonLd("http://localhost/foo.jsonld"); - await expect(promise).rejects.toHaveProperty("response.ok", true); + await expect(promise).rejects.toHaveProperty("response.ok", false); await expect(promise).rejects.not.toHaveProperty("body"); }); +test("fetch a 202 Accepted with non JSON-LD content type returns empty response", async () => { + server.use( + http.post( + "http://localhost/foo.jsonld", + () => + new Response(null, { + status: 202, + statusText: "Accepted", + }), + ), + ); + + const data = await fetchJsonLd("http://localhost/foo.jsonld", { + method: "POST", + }); + expect(data.response.ok).toBe(true); + expect(data.response.status).toBe(202); + expect(data).not.toHaveProperty("body"); +}); + +test("fetch a 202 Accepted with JSON-LD content type parses body", async () => { + server.use( + http.post("http://localhost/foo.jsonld", () => + Response.json(httpResponse, { + headers: { "Content-Type": "application/ld+json" }, + status: 202, + statusText: "Accepted", + }), + ), + ); + + const data = await fetchJsonLd("http://localhost/foo.jsonld", { + method: "POST", + }); + expect(data.response.ok).toBe(true); + expect(data.response.status).toBe(202); + assert("body" in data, "Response should have a body property"); + assert(data.body !== null, "Body should not be null"); + assert("name" in data.body, "Body should have a name property"); + expect(data.body["name"]).toBe("John Lennon"); +}); + test("fetch an error with Content-Type application/ld+json", async () => { server.use( http.get("http://localhost/foo.jsonld", () => diff --git a/src/hydra/fetchJsonLd.ts b/src/hydra/fetchJsonLd.ts index bd6131f..99be05c 100644 --- a/src/hydra/fetchJsonLd.ts +++ b/src/hydra/fetchJsonLd.ts @@ -35,17 +35,22 @@ export default async function fetchJsonLd( return { response }; } - if ( - status >= 500 || - !contentType || - (!contentType.includes(jsonLdMimeType) && - !contentType.includes(jsonProblemMimeType)) - ) { + const isJsonContent = + contentType !== null && + (contentType.includes(jsonLdMimeType) || + contentType.includes(jsonProblemMimeType)); + + if (status >= 500 || (!isJsonContent && !response.ok)) { const reason: RejectedResponseDocument = { response }; // oxlint-disable-next-line no-throw-literal throw reason; } + // 2xx response with a content type different from JSON-LD: return empty response + if (!isJsonContent) { + return { response }; + } + const body = (await response.json()) as JsonLd; return { response,