From 60dc1d5c7adbef40827eae93318894ebd35df9e8 Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 20 Sep 2024 16:03:35 +0000 Subject: [PATCH] add unit test, changeset --- .changeset/gold-bugs-sneeze.md | 5 +++++ .../src/node-http-handler.spec.ts | 18 ++++++++++++++++++ .../node-http-handler/src/node-http-handler.ts | 5 +++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 .changeset/gold-bugs-sneeze.md diff --git a/.changeset/gold-bugs-sneeze.md b/.changeset/gold-bugs-sneeze.md new file mode 100644 index 00000000000..ba76b122381 --- /dev/null +++ b/.changeset/gold-bugs-sneeze.md @@ -0,0 +1,5 @@ +--- +"@smithy/node-http-handler": patch +--- + +remove brackets from hostname diff --git a/packages/node-http-handler/src/node-http-handler.spec.ts b/packages/node-http-handler/src/node-http-handler.spec.ts index 54add403667..c32339cdfc1 100644 --- a/packages/node-http-handler/src/node-http-handler.spec.ts +++ b/packages/node-http-handler/src/node-http-handler.spec.ts @@ -153,6 +153,24 @@ describe("NodeHttpHandler", () => { expect(hRequestSpy.mock.calls[0][0]?.port).toEqual(1234); expect(hRequestSpy.mock.calls[0][0]?.path).toEqual("/some/path?some=query#fragment"); }); + + it("removes brackets from hostname", async () => { + const nodeHttpHandler = new NodeHttpHandler({}); + const httpRequest = { + protocol: "http:", + username: "username", + password: "password", + hostname: "[host]", + port: 1234, + path: "/some/path", + query: { + some: "query", + }, + fragment: "fragment", + }; + await nodeHttpHandler.handle(httpRequest as any); + expect(hRequestSpy.mock.calls[0][0]?.host).toEqual("host"); + }); }); }); diff --git a/packages/node-http-handler/src/node-http-handler.ts b/packages/node-http-handler/src/node-http-handler.ts index 57c40f61bec..2e4233af724 100644 --- a/packages/node-http-handler/src/node-http-handler.ts +++ b/packages/node-http-handler/src/node-http-handler.ts @@ -4,6 +4,7 @@ import type { Logger, NodeHttpHandlerOptions } from "@smithy/types"; import { HttpHandlerOptions, Provider } from "@smithy/types"; import { Agent as hAgent, request as hRequest } from "http"; import { Agent as hsAgent, request as hsRequest, RequestOptions } from "https"; +import { urlToHttpOptions } from "url"; import { NODEJS_TIMEOUT_ERROR_CODES } from "./constants"; import { getTransformedHeaders } from "./get-transformed-headers"; @@ -210,8 +211,8 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf path += `#${request.fragment}`; } - let hostname: string; - if (request.hostname.startsWith("[") && request.hostname.endsWith("]")) { + let hostname = request.hostname ?? ""; + if (hostname[0] === "[" && hostname.endsWith("]")) { hostname = request.hostname.slice(1, -1); } else { hostname = request.hostname;