|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { createObservedClient } from "../helpers.js"; |
| 3 | +import type { FetchOptions, HeadersOptions } from "../../src/index.js"; |
| 4 | +import type { paths } from "./schemas/common.js"; |
| 5 | + |
| 6 | +describe("createClient options", () => { |
| 7 | + test("baseUrl", async () => { |
| 8 | + let actualURL = new URL("https://fakeurl.example"); |
| 9 | + const client = createObservedClient<paths>({ baseUrl: "https://api.foo.bar/v2" }, async (req) => { |
| 10 | + actualURL = new URL(req.url); |
| 11 | + return Response.json([]); |
| 12 | + }); |
| 13 | + await client.GET("/resources"); |
| 14 | + expect(actualURL.href).toBe("https://api.foo.bar/v2/resources"); |
| 15 | + }); |
| 16 | + |
| 17 | + test("baseUrl removes trailing slash", async () => { |
| 18 | + let actualURL = new URL("https://fakeurl.example"); |
| 19 | + const client = createObservedClient<paths>({ baseUrl: "https://api.foo.bar/v3/" }, async (req) => { |
| 20 | + actualURL = new URL(req.url); |
| 21 | + return Response.json([]); |
| 22 | + }); |
| 23 | + await client.GET("/resources"); |
| 24 | + expect(actualURL.href).toBe("https://api.foo.bar/v3/resources"); |
| 25 | + }); |
| 26 | + |
| 27 | + test("baseUrl per request", async () => { |
| 28 | + let actualURL = new URL("https://fakeurl.example"); |
| 29 | + const client = createObservedClient<paths>({ baseUrl: "https://fakeurl.example" }, async (req) => { |
| 30 | + actualURL = new URL(req.url); |
| 31 | + return Response.json([]); |
| 32 | + }); |
| 33 | + |
| 34 | + const localBaseUrl = "https://api.foo.bar/v3"; |
| 35 | + await client.GET("/resources", { baseUrl: localBaseUrl }); |
| 36 | + |
| 37 | + // assert baseUrl and path mesh as expected |
| 38 | + expect(actualURL.href).toBe("https://api.foo.bar/v3/resources"); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("content-type", () => { |
| 42 | + const BODY_ACCEPTING_METHODS = [["PUT"], ["POST"], ["DELETE"], ["OPTIONS"], ["PATCH"]] as const; |
| 43 | + const ALL_METHODS = [...BODY_ACCEPTING_METHODS, ["GET"], ["HEAD"]] as const; |
| 44 | + |
| 45 | + async function fireRequestAndGetContentType(options: { |
| 46 | + defaultHeaders?: HeadersOptions; |
| 47 | + method: (typeof ALL_METHODS)[number][number]; |
| 48 | + fetchOptions: FetchOptions<any>; |
| 49 | + }) { |
| 50 | + let headers = new Headers(); |
| 51 | + const client = createObservedClient<any>({ headers: options.defaultHeaders }, async (req) => { |
| 52 | + headers = req.headers; |
| 53 | + return Response.json([]); |
| 54 | + }); |
| 55 | + await client[options.method]("/resources", options.fetchOptions as any); |
| 56 | + return headers.get("content-type"); |
| 57 | + } |
| 58 | + |
| 59 | + test.each(ALL_METHODS)("no content-type for body-less requests - %s", async (method) => { |
| 60 | + const contentType = await fireRequestAndGetContentType({ |
| 61 | + method, |
| 62 | + fetchOptions: {}, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(contentType).toBe(null); |
| 66 | + }); |
| 67 | + |
| 68 | + test.each(ALL_METHODS)("no content-type for `undefined` body requests - %s", async (method) => { |
| 69 | + const contentType = await fireRequestAndGetContentType({ |
| 70 | + method, |
| 71 | + fetchOptions: { body: undefined }, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(contentType).toBe(null); |
| 75 | + }); |
| 76 | + |
| 77 | + const BODIES = [{ prop: "a" }, {}, "", "str", null, false, 0, 1, new Date("2024-08-07T09:52:00.836Z")] as const; |
| 78 | + const METHOD_BODY_COMBINATIONS = BODY_ACCEPTING_METHODS.flatMap(([method]) => |
| 79 | + BODIES.map((body) => [method, body] as const), |
| 80 | + ); |
| 81 | + |
| 82 | + test.each(METHOD_BODY_COMBINATIONS)( |
| 83 | + "implicit default content-type for body-full requests - %s, %j", |
| 84 | + async (method, body) => { |
| 85 | + const contentType = await fireRequestAndGetContentType({ |
| 86 | + method, |
| 87 | + fetchOptions: { body }, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(contentType).toBe("application/json"); |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + test.each(METHOD_BODY_COMBINATIONS)( |
| 95 | + "provided default content-type for body-full requests - %s, %j", |
| 96 | + async (method, body) => { |
| 97 | + const contentType = await fireRequestAndGetContentType({ |
| 98 | + defaultHeaders: { "content-type": "application/my-json" }, |
| 99 | + method, |
| 100 | + fetchOptions: { body }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(contentType).toBe("application/my-json"); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + test.each(METHOD_BODY_COMBINATIONS)( |
| 108 | + "native-fetch default content-type for body-full requests, when default is suppressed - %s, %j", |
| 109 | + async (method, body) => { |
| 110 | + const contentType = await fireRequestAndGetContentType({ |
| 111 | + defaultHeaders: { "content-type": null }, |
| 112 | + method, |
| 113 | + fetchOptions: { body }, |
| 114 | + }); |
| 115 | + // the fetch implementation won't allow sending a body without content-type, |
| 116 | + // and it defaults to `text/plain;charset=UTF-8`, however the actual default value |
| 117 | + // is irrelevant and might be flaky across different fetch implementations |
| 118 | + // for us, it's important that it's not `application/json` |
| 119 | + expect(contentType).not.toBe("application/json"); |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + test.each(METHOD_BODY_COMBINATIONS)( |
| 124 | + "specified content-type for body-full requests - %s, %j", |
| 125 | + async (method, body) => { |
| 126 | + const contentType = await fireRequestAndGetContentType({ |
| 127 | + method, |
| 128 | + fetchOptions: { |
| 129 | + body, |
| 130 | + headers: { "content-type": "application/my-json" }, |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(contentType).toBe("application/my-json"); |
| 135 | + }, |
| 136 | + ); |
| 137 | + |
| 138 | + test.each(METHOD_BODY_COMBINATIONS)( |
| 139 | + "specified content-type for body-full requests, even when default is suppressed - %s, %j", |
| 140 | + async (method, body) => { |
| 141 | + const contentType = await fireRequestAndGetContentType({ |
| 142 | + method, |
| 143 | + fetchOptions: { |
| 144 | + body, |
| 145 | + headers: { "content-type": "application/my-json" }, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + expect(contentType).toBe("application/my-json"); |
| 150 | + }, |
| 151 | + ); |
| 152 | + }); |
| 153 | +}); |
0 commit comments