From 7a36b02eddb167be87dc7f868f5f7beedba8fa49 Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 21 Sep 2023 14:13:22 -0800 Subject: [PATCH] Fix update tests when using ipv6 --- test/unit/node/update.test.ts | 91 ++++++++++++++++------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/test/unit/node/update.test.ts b/test/unit/node/update.test.ts index 50d88deea3c0..c70c429b626c 100644 --- a/test/unit/node/update.test.ts +++ b/test/unit/node/update.test.ts @@ -1,6 +1,5 @@ import { logger } from "@coder/logger" import * as http from "http" -import { AddressInfo } from "net" import * as path from "path" import { SettingsProvider, UpdateSettings } from "../../../src/node/settings" import { LatestResponse, UpdateProvider } from "../../../src/node/update" @@ -79,7 +78,7 @@ describe("update", () => { } let _provider: UpdateProvider | undefined - let _address: string | AddressInfo | null + let _address: string | undefined const provider = (): UpdateProvider => { if (!_provider) { throw new Error("Update provider has not been created") @@ -105,12 +104,14 @@ describe("update", () => { }) }) - _address = server.address() - if (!isAddressInfo(_address)) { - throw new Error("unexpected address") + const addr = server.address() + if (!isAddressInfo(addr)) { + throw new Error("invalid address: ${addr}") } - _provider = new UpdateProvider(`http://${_address?.address}:${_address?.port}/latest`, _settings) + const host = addr.family === "IPv6" ? `[${addr.address}]` : addr.address + _address = `http://${host}:${addr.port}` + _provider = new UpdateProvider(`${_address}/latest`, _settings) }) afterAll(() => { @@ -220,59 +221,51 @@ describe("update", () => { }) it("should reject if response has status code 500", async () => { - if (isAddressInfo(_address)) { - const mockURL = `http://${_address.address}:${_address.port}/reject-status-code` - const provider = new UpdateProvider(mockURL, settings()) - const update = await provider.getUpdate(true) - - expect(update.version).toBe("unknown") - expect(logger.error).toHaveBeenCalled() - expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { - identifier: "error", - value: `${mockURL}: 500`, - }) - } + const mockURL = `${_address}/reject-status-code` + const provider = new UpdateProvider(mockURL, settings()) + const update = await provider.getUpdate(true) + + expect(update.version).toBe("unknown") + expect(logger.error).toHaveBeenCalled() + expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { + identifier: "error", + value: `${mockURL}: 500`, + }) }) it("should reject if no location header provided", async () => { - if (isAddressInfo(_address)) { - const mockURL = `http://${_address.address}:${_address.port}/no-location-header` - const provider = new UpdateProvider(mockURL, settings()) - const update = await provider.getUpdate(true) - - expect(update.version).toBe("unknown") - expect(logger.error).toHaveBeenCalled() - expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { - identifier: "error", - value: `received redirect with no location header`, - }) - } + const mockURL = `${_address}/no-location-header` + const provider = new UpdateProvider(mockURL, settings()) + const update = await provider.getUpdate(true) + + expect(update.version).toBe("unknown") + expect(logger.error).toHaveBeenCalled() + expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { + identifier: "error", + value: `received redirect with no location header`, + }) }) it("should resolve the request with response.headers.location", async () => { version = "4.1.1" - if (isAddressInfo(_address)) { - const mockURL = `http://${_address.address}:${_address.port}/with-location-header` - const provider = new UpdateProvider(mockURL, settings()) - const update = await provider.getUpdate(true) + const mockURL = `${_address}/with-location-header` + const provider = new UpdateProvider(mockURL, settings()) + const update = await provider.getUpdate(true) - expect(logger.error).not.toHaveBeenCalled() - expect(update.version).toBe("4.1.1") - } + expect(logger.error).not.toHaveBeenCalled() + expect(update.version).toBe("4.1.1") }) it("should reject if more than 10 redirects", async () => { - if (isAddressInfo(_address)) { - const mockURL = `http://${_address.address}:${_address.port}/redirect/11` - const provider = new UpdateProvider(mockURL, settings()) - const update = await provider.getUpdate(true) - - expect(update.version).toBe("unknown") - expect(logger.error).toHaveBeenCalled() - expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { - identifier: "error", - value: `reached max redirects`, - }) - } + const mockURL = `${_address}/redirect/11` + const provider = new UpdateProvider(mockURL, settings()) + const update = await provider.getUpdate(true) + + expect(update.version).toBe("unknown") + expect(logger.error).toHaveBeenCalled() + expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", { + identifier: "error", + value: `reached max redirects`, + }) }) })