|
| 1 | +import internalLoad, { type LoadOptions } from "../src/load.js"; |
| 2 | +import type { Subschema } from "../src/types"; |
| 3 | +import { Response, fetch as unidiciFetch } from "undici"; |
| 4 | +import { Readable } from "node:stream"; |
| 5 | +import { dump as stringifyYaml } from "js-yaml"; |
| 6 | + |
| 7 | +describe("Load", () => { |
| 8 | + describe("remote schema", () => { |
| 9 | + describe("in json", () => { |
| 10 | + test("type deduced from extension .json", async () => { |
| 11 | + const output = await load(new URL("https://example.com/openapi.json"), { |
| 12 | + async fetch() { |
| 13 | + return new Response(JSON.stringify(exampleSchema), { |
| 14 | + headers: { "Content-Type": "text/plain" }, |
| 15 | + }); |
| 16 | + }, |
| 17 | + }); |
| 18 | + expect(output["."].schema).toEqual(exampleSchema); |
| 19 | + }); |
| 20 | + test("type deduced from Content-Type header", async () => { |
| 21 | + const output = await load(new URL("https://example.com/openapi"), { |
| 22 | + async fetch() { |
| 23 | + return new Response(JSON.stringify(exampleSchema), { |
| 24 | + headers: { "Content-Type": "application/json" }, |
| 25 | + }); |
| 26 | + }, |
| 27 | + }); |
| 28 | + expect(output["."].schema).toEqual(exampleSchema); |
| 29 | + }); |
| 30 | + // Regression test for https://github.com/drwpow/openapi-typescript/issues/988 |
| 31 | + test("type deduced from Content-Type header, in lowercase", async () => { |
| 32 | + const output = await load(new URL("https://example.com/openapi"), { |
| 33 | + async fetch() { |
| 34 | + return new Response(JSON.stringify(exampleSchema), { |
| 35 | + headers: { "content-type": "application/json" }, |
| 36 | + }); |
| 37 | + }, |
| 38 | + }); |
| 39 | + expect(output["."].schema).toEqual(exampleSchema); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("in yaml", () => { |
| 44 | + test("type deduced from extension .yaml", async () => { |
| 45 | + const output = await load(new URL("https://example.com/openapi.yaml"), { |
| 46 | + async fetch() { |
| 47 | + return new Response(stringifyYaml(exampleSchema), { |
| 48 | + headers: { "Content-Type": "text/plain" }, |
| 49 | + }); |
| 50 | + }, |
| 51 | + }); |
| 52 | + expect(output["."].schema).toEqual(exampleSchema); |
| 53 | + }); |
| 54 | + test("type deduced from extension .yml", async () => { |
| 55 | + const output = await load(new URL("https://example.com/openapi.yml"), { |
| 56 | + async fetch() { |
| 57 | + return new Response(stringifyYaml(exampleSchema), { |
| 58 | + headers: { "Content-Type": "text/plain" }, |
| 59 | + }); |
| 60 | + }, |
| 61 | + }); |
| 62 | + expect(output["."].schema).toEqual(exampleSchema); |
| 63 | + }); |
| 64 | + test("type deduced from Content-Type header", async () => { |
| 65 | + const output = await load(new URL("https://example.com/openapi"), { |
| 66 | + async fetch() { |
| 67 | + return new Response(stringifyYaml(exampleSchema), { |
| 68 | + headers: { "Content-Type": "application/yaml" }, |
| 69 | + }); |
| 70 | + }, |
| 71 | + }); |
| 72 | + expect(output["."].schema).toEqual(exampleSchema); |
| 73 | + }); |
| 74 | + // Regression test for https://github.com/drwpow/openapi-typescript/issues/988 |
| 75 | + test("type deduced from Content-Type header, in lowercase", async () => { |
| 76 | + const output = await load(new URL("https://example.com/openapi"), { |
| 77 | + async fetch() { |
| 78 | + return new Response(stringifyYaml(exampleSchema), { |
| 79 | + headers: { "content-type": "application/yaml" }, |
| 80 | + }); |
| 81 | + }, |
| 82 | + }); |
| 83 | + expect(output["."].schema).toEqual(exampleSchema); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +const exampleSchema = { |
| 90 | + openapi: "3.1.0", |
| 91 | + paths: { |
| 92 | + "/foo": { |
| 93 | + get: {}, |
| 94 | + }, |
| 95 | + }, |
| 96 | +}; |
| 97 | + |
| 98 | +async function load( |
| 99 | + schema: URL | Subschema | Readable, |
| 100 | + options?: Partial<LoadOptions> |
| 101 | +): Promise<{ [url: string]: Subschema }> { |
| 102 | + return internalLoad(schema, { |
| 103 | + rootURL: schema as URL, |
| 104 | + schemas: {}, |
| 105 | + urlCache: new Set(), |
| 106 | + fetch: vi.fn(unidiciFetch), |
| 107 | + additionalProperties: false, |
| 108 | + alphabetize: false, |
| 109 | + defaultNonNullable: false, |
| 110 | + discriminators: {}, |
| 111 | + immutableTypes: false, |
| 112 | + indentLv: 0, |
| 113 | + operations: {}, |
| 114 | + pathParamsAsTypes: false, |
| 115 | + postTransform: undefined, |
| 116 | + silent: true, |
| 117 | + supportArrayLength: false, |
| 118 | + transform: undefined, |
| 119 | + ...options, |
| 120 | + }); |
| 121 | +} |
0 commit comments