Files
clean-architecture-frontend…/tests/unit/request-builder.test.ts
DongHyeonkaandClaude Opus 5.5 ebbd50d4fb fix(http): reject "." and ".." path values in V3 and V2 (R-3.1)
A path value of "." or ".." survives encodeURIComponent unchanged, and URL
resolution (V3 `new URL`, V2 `pathname =` assignment) then collapses it into
a parent path that still sits under the base URL, so `URL_ORIGIN_ESCAPED`
never fired. `/members/{m}` with m=".." pointed at the project instead.

The V3 bridge now returns PATH_VALUE_INVALID and the V2 request builder
returns PATH_PARAMETER_INVALID for exactly those two values.

RED before fix: tests/unit/http-contract-bridge.test.ts "rejects the dot
segment ..." and tests/unit/request-builder.test.ts "rejects the dot segment
..." returned ok/success with a parent URL.
Gates: check:architecture, check:types:app, check:types:test, lint,
test:component pass. test:unit passes except removal-fixture.test.ts, which
needs generated artifacts/ release evidence absent in a fresh worktree
(unrelated to this change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 12:25:01 +09:00

75 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { buildRequestTarget } from "../../src/adapters/http/request-builder.ts";
import type { ApiOperation } from "../../src/contracts/api-operations.ts";
const operation: ApiOperation = {
method: "GET",
path: "/api/resources/{resourceId}",
operationId: "GET_RESOURCE",
auth: "none",
timeoutMs: null,
idempotency: "safe",
retry: "runtime",
requestSource: "search",
requestSchema: "ResourceQuery",
responseSchema: "ResourcePayload",
owner: "test",
};
describe("deterministic HTTP request target", () => {
it("escapes path values and serializes optional/array search in key order", () => {
const result = buildRequestTarget(
"https://api.test/base/",
operation,
{ resourceId: "folder/item" },
{
tags: ["beta", "alpha"],
omitted: undefined,
limit: 20,
cursor: "next page",
},
);
expect(result.success).toBe(true);
if (!result.success) return;
expect(result.url.href).toBe(
"https://api.test/base/api/resources/folder%2Fitem?cursor=next+page&limit=20&tags=beta&tags=alpha",
);
});
it("fails closed when a path value or scalar search value is invalid", () => {
expect(
buildRequestTarget("https://api.test", operation, {}, {}),
).toEqual({ success: false, code: "PATH_PARAMETER_MISSING" });
expect(
buildRequestTarget(
"https://api.test",
operation,
{ resourceId: "one" },
{ nested: { secret: true } },
),
).toEqual({ success: false, code: "SEARCH_PARAMETER_INVALID" });
expect(
buildRequestTarget(
"https://api.test",
operation,
{ resourceId: "one", unexpected: "two" },
{},
),
).toEqual({ success: false, code: "PATH_PARAMETER_UNEXPECTED" });
expect(
buildRequestTarget("http://api.test", operation, { resourceId: "one" }),
).toEqual({ success: false, code: "BASE_URL_INVALID" });
});
it.each([".", ".."])(
"rejects the dot segment %j instead of letting pathname normalization move the target",
(resourceId) => {
expect(
buildRequestTarget("https://api.test/base/", operation, { resourceId }),
).toEqual({ success: false, code: "PATH_PARAMETER_INVALID" });
},
);
});