Skip to content
Merged
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
64 changes: 62 additions & 2 deletions src/hydra/fetchJsonLd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(`<body>Not Found</body>`, {
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", () =>
Expand Down
17 changes: 11 additions & 6 deletions src/hydra/fetchJsonLd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading