Skip to content

Commit

Permalink
Fix update tests when using ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
code-asher committed Sep 21, 2023
1 parent 73e583f commit 7a36b02
Showing 1 changed file with 42 additions and 49 deletions.
91 changes: 42 additions & 49 deletions test/unit/node/update.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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`,
})
})
})

0 comments on commit 7a36b02

Please sign in to comment.