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
28 changes: 27 additions & 1 deletion src/hydra/fetchJsonLd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test("fetch a non JSON-LD document", () => {
);
});

test("fetch an error", () => {
test("fetch an error with Content-Type application/ld+json", () => {
fetchMock.mockResponseOnce(
`{
"@context": "http://json-ld.org/contexts/person.jsonld",
Expand All @@ -69,6 +69,32 @@ test("fetch an error", () => {
);
});

test("fetch an error with Content-Type application/error+json", () => {
fetchMock.mockResponseOnce(
`{
"@context": "http://json-ld.org/contexts/person.jsonld",
"@id": "http://dbpedia.org/resource/John_Lennon",
"name": "John Lennon",
"born": "1940-10-09",
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
}`,
{
status: 400,
statusText: "Bad Request",
headers: { "Content-Type": "application/error+json" },
}
);

return fetchJsonLd("/foo.jsonld").catch(
({ response }: { response: Response }) => {
void response.json().then((body: { born: string }) => {
expect(response.ok).toBe(false);
expect(body.born).toBe("1940-10-09");
});
}
);
});

test("fetch an empty document", () => {
fetchMock.mockResponseOnce("", {
status: 204,
Expand Down
8 changes: 7 additions & 1 deletion src/hydra/fetchJsonLd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Document, JsonLd, RemoteDocument } from "jsonld/jsonld-spec";
import type { RequestInitExtended } from "./types.js";

const jsonLdMimeType = "application/ld+json";
const jsonProblemMimeType = "application/problem+json";

export type RejectedResponseDocument = {
response: Response;
Expand Down Expand Up @@ -31,7 +32,12 @@ export default async function fetchJsonLd(
return Promise.resolve({ response });
}

if (500 <= status || !contentType || !contentType.includes(jsonLdMimeType)) {
if (
500 <= status ||
!contentType ||
(!contentType.includes(jsonLdMimeType) &&
!contentType.includes(jsonProblemMimeType))
) {
const reason: RejectedResponseDocument = { response };
return Promise.reject(reason);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hydra/parseHydraDocumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ const init: MockParams = {
status: 200,
statusText: "OK",
headers: {
Link: '<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"',
Link: '<http://example.com/docs>; rel="http://example.com",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"',
"Content-Type": "application/ld+json",
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/hydra/parseHydraDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function getDocumentationUrlFromHeaders(headers: Headers): string {
}

const matches =
/<(.+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec(
/<([^<]+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec(
linkHeader
);
if (matches === null) {
Expand Down