| @@ -0,0 +1,169 @@ |
| | 1 | // --------------------------------------------------------------------------- |
| | 2 | // Regression test for non-JSON request-body serialization. |
| | 3 | // |
| | 4 | // Before the fix, the invoke path only had two branches — JSON, or |
| | 5 | // `String(bodyValue)` with whatever content-type the spec declared. For an |
| | 6 | // object body that meant shipping the literal string `[object Object]` |
| | 7 | // with `Content-Type: application/x-www-form-urlencoded`, which servers |
| | 8 | // reject or hold open waiting for valid framing. |
| | 9 | // |
| | 10 | // Now we dispatch on content-type: form-urlencoded → bodyUrlParams, |
| | 11 | // multipart → bodyFormDataRecord, string passthrough for pre-serialized |
| | 12 | // bodies, JSON.stringify as a last-resort fallback (never `[object Object]`). |
| | 13 | // --------------------------------------------------------------------------- |
| | 14 | |
| | 15 | import { describe, expect, it } from "@effect/vitest"; |
| | 16 | import { Effect } from "effect"; |
| | 17 | import { FetchHttpClient } from "@effect/platform"; |
| | 18 | import { createServer } from "node:http"; |
| | 19 | import type { AddressInfo } from "node:net"; |
| | 20 | |
| | 21 | import { |
| | 22 | createExecutor, |
| | 23 | definePlugin, |
| | 24 | makeTestConfig, |
| | 25 | type InvokeOptions, |
| | 26 | type SecretProvider, |
| | 27 | } from "@executor/sdk"; |
| | 28 | |
| | 29 | import { openApiPlugin } from "./plugin"; |
| | 30 | |
| | 31 | const autoApprove: InvokeOptions = { onElicitation: "accept-all" }; |
| | 32 | const TEST_SCOPE = "test-scope"; |
| | 33 | |
| | 34 | const memoryProvider: SecretProvider = (() => { |
| | 35 | const store = new Map<string, string>(); |
| | 36 | return { |
| | 37 | key: "memory", |
| | 38 | writable: true, |
| | 39 | get: (id, scope) => |
| | 40 | Effect.sync(() => store.get(`${scope}\u0000${id}`) ?? null), |
| | 41 | set: (id, value, scope) => |
| | 42 | Effect.sync(() => { |
| | 43 | store.set(`${scope}\u0000${id}`, value); |
| | 44 | }), |
| | 45 | delete: (id, scope) => |
| | 46 | Effect.sync(() => store.delete(`${scope}\u0000${id}`)), |
| | 47 | list: () => Effect.sync(() => []), |
| | 48 | }; |
| | 49 | })(); |
| | 50 | |
| | 51 | const memorySecretsPlugin = definePlugin(() => ({ |
| | 52 | id: "memory-secrets" as const, |
| | 53 | storage: () => ({}), |
| | 54 | secretProviders: [memoryProvider], |
| | 55 | })); |
| | 56 | |
| | 57 | type Captured = { |
| | 58 | contentType: string; |
| | 59 | body: string; |
| | 60 | }; |
| | 61 | |
| | 62 | const startEchoServer = () => |
| | 63 | Effect.acquireRelease( |
| | 64 | Effect.async<{ baseUrl: string; captured: Captured; close: () => void }>( |
| | 65 | (resume) => { |
| | 66 | const captured: Captured = { contentType: "", body: "" }; |
| | 67 | const server = createServer((req, res) => { |
| | 68 | const chunks: Buffer[] = []; |
| | 69 | req.on("data", (c: Buffer) => chunks.push(c)); |
| | 70 | req.on("end", () => { |
| | 71 | captured.contentType = req.headers["content-type"] ?? ""; |
| | 72 | captured.body = Buffer.concat(chunks).toString("utf8"); |
| | 73 | res.writeHead(200, { "content-type": "application/json" }); |
| | 74 | res.end(JSON.stringify({ ok: true })); |
| | 75 | }); |
| | 76 | }); |
| | 77 | server.listen(0, "127.0.0.1", () => { |
| | 78 | const port = (server.address() as AddressInfo).port; |
| | 79 | resume( |
| | 80 | Effect.succeed({ |
| | 81 | baseUrl: `http://127.0.0.1:${port}`, |
| | 82 | captured, |
| | 83 | close: () => server.close(), |
| | 84 | }), |
| | 85 | ); |
| | 86 | }); |
| | 87 | }, |
| | 88 | ), |
| | 89 | (s) => Effect.sync(() => s.close()), |
| | 90 | ); |
| | 91 | |
| | 92 | const formSpec = JSON.stringify({ |
| | 93 | openapi: "3.0.0", |
| | 94 | info: { title: "FormTest", version: "1.0.0" }, |
| | 95 | paths: { |
| | 96 | "/submit": { |
| | 97 | post: { |
| | 98 | operationId: "submit", |
| | 99 | tags: ["forms"], |
| | 100 | requestBody: { |
| | 101 | required: true, |
| | 102 | content: { |
| | 103 | "application/x-www-form-urlencoded": { |
| | 104 | schema: { |
| | 105 | type: "object", |
| | 106 | properties: { |
| | 107 | name: { type: "string" }, |
| | 108 | email: { type: "string" }, |
| | 109 | }, |
| | 110 | }, |
| | 111 | }, |
| | 112 | }, |
| | 113 | }, |
| | 114 | responses: { |
| | 115 | "200": { |
| | 116 | description: "ok", |
| | 117 | content: { |
| | 118 | "application/json": { |
| | 119 | schema: { |
| | 120 | type: "object", |
| | 121 | properties: { ok: { type: "boolean" } }, |
| | 122 | }, |
| | 123 | }, |
| | 124 | }, |
| | 125 | }, |
| | 126 | }, |
| | 127 | }, |
| | 128 | }, |
| | 129 | }, |
| | 130 | }); |
| | 131 | |
| | 132 | describe("OpenAPI non-JSON request body serialization", () => { |
| | 133 | it.scoped( |
| | 134 | "form-urlencoded object body is properly encoded (no '[object Object]')", |
| | 135 | () => |
| | 136 | Effect.gen(function* () { |
| | 137 | const { baseUrl, captured } = yield* startEchoServer(); |
| | 138 | |
| | 139 | const executor = yield* createExecutor( |
| | 140 | makeTestConfig({ |
| | 141 | plugins: [ |
| | 142 | openApiPlugin({ httpClientLayer: FetchHttpClient.layer }), |
| | 143 | memorySecretsPlugin(), |
| | 144 | ] as const, |
| | 145 | }), |
| | 146 | ); |
| | 147 | |
| | 148 | yield* executor.openapi.addSpec({ |
| | 149 | spec: formSpec, |
| | 150 | scope: TEST_SCOPE, |
| | 151 | namespace: "form", |
| | 152 | baseUrl, |
| | 153 | }); |
| | 154 | |
| | 155 | yield* executor.tools.invoke( |
| | 156 | "form.forms.submit", |
| | 157 | { body: { name: "Acme", email: "a@b.com" } }, |
| | 158 | autoApprove, |
| | 159 | ); |
| | 160 | |
| | 161 | expect(captured.contentType).toBe("application/x-www-form-urlencoded"); |
| | 162 | expect(captured.body).not.toBe("[object Object]"); |
| | 163 | |
| | 164 | const parsed = new URLSearchParams(captured.body); |
| | 165 | expect(parsed.get("name")).toBe("Acme"); |
| | 166 | expect(parsed.get("email")).toBe("a@b.com"); |
| | 167 | }), |
| | 168 | ); |
| | 169 | }); |