Skip to content

Commit

Permalink
Accounts for ipv6 addresses for http endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska committed Dec 5, 2023
1 parent daaf83a commit bfdc37a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/network/HttpEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
import { Endpoint } from "./AbstractEndpoint";
import { URL } from "url";
import { URIParseConfig } from "./Network.consts";
import {log} from "util";

Check failure on line 17 in src/network/HttpEndpoint.ts

View workflow job for this annotation

GitHub Actions / build (16.14.0)

'log' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 17 in src/network/HttpEndpoint.ts

View workflow job for this annotation

GitHub Actions / test-e2e

'log' is defined but never used. Allowed unused vars must match /^_/u

export class HttpEndpoint extends Endpoint {
constructor(url: string) {
Expand All @@ -22,11 +23,12 @@ export class HttpEndpoint extends Endpoint {
try {
const parsedUrl = new URL(HttpEndpoint.preprocessUri(url));
this._scheme = parsedUrl.protocol.replace(":", "");
this._hostname = parsedUrl.hostname.replace("[", "");
this._hostname = this._hostname.replace("]", "");
this._hostname = parsedUrl.hostname.replace(/[\]\[]/gi, "");

Check failure on line 26 in src/network/HttpEndpoint.ts

View workflow job for this annotation

GitHub Actions / build (16.14.0)

Unnecessary escape character: \[

Check failure on line 26 in src/network/HttpEndpoint.ts

View workflow job for this annotation

GitHub Actions / test-e2e

Unnecessary escape character: \[
this._port = parseInt(parsedUrl.port) || (this._scheme == "https" ? 443 : 80);
this._tls = this._scheme == "https";
this._endpoint = this._scheme + "://" + this._hostname + ":" + this._port.toString();
// Remove brackets if it's a IPv6 addresses
const hostPart = parsedUrl.hostname.includes("[") ? `[${this._hostname}]` : this._hostname;
this._endpoint = `${this._scheme}://${hostPart}:${this._port}`;
} catch (error) {
throw new Error(`Invalid address: ${url}`);
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/utils/Network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ describe("Client.util", () => {
port: 5000,
endpoint: "https://myhost:5000",
},
{
url: "https://[2001:db8:1f70:0:999:de8:7648:6e8]:5000",
error: false,
secure: true,
scheme: "",
host: "2001:db8:1f70:0:999:de8:7648:6e8",
port: 5000,
endpoint: "https://[2001:db8:1f70:0:999:de8:7648:6e8]:5000",
},
];

testCases.forEach((testCase) => {
Expand Down

0 comments on commit bfdc37a

Please sign in to comment.