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
5 changes: 5 additions & 0 deletions packages/quicktype-core/src/input/io/NodeIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export async function readableFromFileOrURL(
const response = await globalThis.fetch(fileOrURL, {
headers: parseHeaders(httpHeaders),
});
if (!response.ok) {
throw new Error(
`HTTP ${response.status} ${response.statusText}`,
);
}

return readableFromResponseBody(defined(response.body));
}
Expand Down
13 changes: 13 additions & 0 deletions test/unit/url-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as path from "node:path";

import { afterAll, beforeAll, describe, expect, test } from "vitest";

import { readFromFileOrURL } from "../../packages/quicktype-core/src/input/io/NodeIO.js";
import { main as quicktype } from "../../src";

const files: Record<string, string> = {
Expand Down Expand Up @@ -74,6 +75,12 @@ async function generateTypeScript(

beforeAll(async () => {
server = http.createServer((request, response) => {
if (path.basename(request.url ?? "") === "unauthorized.json") {
response.writeHead(401, { "Content-Type": "application/json" });
response.end('{"message":"Bad credentials"}');
return;
}

const content = files[path.basename(request.url ?? "")];
if (content === undefined) {
response.writeHead(404);
Expand Down Expand Up @@ -105,6 +112,12 @@ describe("native fetch URL inputs", () => {
expect(output).toContain("veryUniquePropertyName");
});

test("rejects an HTTP error response", async () => {
await expect(
readFromFileOrURL(`${baseURL}/unauthorized.json`),
).rejects.toThrow("HTTP 401 Unauthorized");
});

test("resolves a relative remote JSON Schema reference", async () => {
const output = await generateTypeScript("schema", "main.schema");
expect(output).toContain("veryUniqueReferencedProperty");
Expand Down
Loading