Skip to content

Commit

Permalink
Add support for relative URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Jul 2, 2024
1 parent 8210322 commit 103ec21
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
32 changes: 31 additions & 1 deletion src/http/httpError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import { describe, it, expect } from "@jest/globals";
import { describe, it, expect, jest } from "@jest/globals";
import { ClientHttpError } from "./httpError";
import { mockProblemDetails } from "./problemDetails.mock";
import {
Expand Down Expand Up @@ -189,4 +189,34 @@ describe("ClientHttpError", () => {
new ClientHttpError(response, "Not important", "Some error message");
}).toThrow(InruptClientError);
});

it("supports relative URLs for type and instance", () => {
const responseUrl = "https://example.org/resource";
const relativeTypeUrl = "../type";
const relativeInstanceUrl = "../instance";
const response = new Response(undefined, {
status: 400,
statusText: "Bad Request",
headers: new Headers({
"Content-Type": "application/problem+json",
}),
});
jest.spyOn(response, "url", "get").mockReturnValue(responseUrl);
const { problemDetails: mockedProblemDetails } = mockProblemDetails({});
const error = new ClientHttpError(
response,
JSON.stringify({
...mockedProblemDetails,
type: relativeTypeUrl,
instance: relativeInstanceUrl,
}),
"Some error message",
);
expect(error.problemDetails.type.href).toStrictEqual(
new URL(relativeTypeUrl, responseUrl).href,
);
expect(error.problemDetails.instance?.href).toStrictEqual(
new URL(relativeInstanceUrl, responseUrl).href,
);
});
});
8 changes: 4 additions & 4 deletions src/http/problemDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ export function hasProblemDetails(
return isProblemDetails((error as WithProblemDetails).problemDetails);
}

function asUrl(url: string): URL | undefined {
function asUrl(url: string, base: string): URL | undefined {
try {
return new URL(url);
return new URL(url, base);
} catch {
return undefined;
}
Expand All @@ -153,7 +153,7 @@ export function buildProblemDetails(response: ErrorResponse): ProblemDetails {
if (response.headers.get("Content-Type") === PROBLEM_DETAILS_MIME) {
try {
const responseBody = JSON.parse(response.body);
const responseType = asUrl(responseBody.type);
const responseType = asUrl(responseBody.type, response.url);
if (responseType !== undefined) {
type = responseType;
}
Expand All @@ -166,7 +166,7 @@ export function buildProblemDetails(response: ErrorResponse): ProblemDetails {
if (typeof responseBody.detail === "string") {
detail = responseBody.detail;
}
const responseInstance = asUrl(responseBody.instance);
const responseInstance = asUrl(responseBody.instance, response.url);
if (responseInstance !== undefined) {
instance = responseInstance;
}
Expand Down

0 comments on commit 103ec21

Please sign in to comment.