|
| 1 | +import { isValidHostLabel } from "@smithy/util-endpoints"; |
| 2 | +import { describe, expect, test as it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { checkRegion } from "./checkRegion"; |
| 5 | + |
| 6 | +describe("checkRegion", () => { |
| 7 | + const acceptedRegionExamples = [ |
| 8 | + "us-east-1", |
| 9 | + "ap-east-1", |
| 10 | + "ap-southeast-4", |
| 11 | + "ap-northeast-3", |
| 12 | + "ap-northeast-1", |
| 13 | + "eu-west-2", |
| 14 | + "il-central-1", |
| 15 | + "mx-central-1", |
| 16 | + "eu-isoe-santaclaus-125", |
| 17 | + "us-iso-reindeer-3000", |
| 18 | + "eusc-de-gingerbread-8000", |
| 19 | + "abcd", |
| 20 | + "12345", |
| 21 | + ]; |
| 22 | + |
| 23 | + it("does not throw when the region is a valid host label", () => { |
| 24 | + for (const region of acceptedRegionExamples) { |
| 25 | + expect(() => checkRegion(region)).not.toThrow(); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it("throws when the region is not a valid host label", () => { |
| 30 | + for (const region of [ |
| 31 | + "us-east-1-", |
| 32 | + "a".repeat(64), |
| 33 | + "-us-east-1", |
| 34 | + "", |
| 35 | + "!", |
| 36 | + "@", |
| 37 | + "#", |
| 38 | + "$", |
| 39 | + "%", |
| 40 | + "^", |
| 41 | + "&", |
| 42 | + "*", |
| 43 | + "(", |
| 44 | + ")", |
| 45 | + ".", |
| 46 | + "[", |
| 47 | + "]", |
| 48 | + ";", |
| 49 | + `'`, |
| 50 | + "?", |
| 51 | + "/", |
| 52 | + "\\", |
| 53 | + "|", |
| 54 | + "+-*/", |
| 55 | + ]) { |
| 56 | + expect(() => checkRegion(region)).toThrow( |
| 57 | + `Region not accepted: region="${region}" is not a valid hostname component.` |
| 58 | + ); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it("caches accepted regions", () => { |
| 63 | + const di = { |
| 64 | + isValidHostLabel, |
| 65 | + }; |
| 66 | + for (const region of acceptedRegionExamples) { |
| 67 | + expect(() => checkRegion(region, di.isValidHostLabel)).not.toThrow(); |
| 68 | + } |
| 69 | + vi.spyOn(di, "isValidHostLabel").mockImplementation(isValidHostLabel); |
| 70 | + for (const region of acceptedRegionExamples) { |
| 71 | + expect(() => checkRegion(region, di.isValidHostLabel)).not.toThrow(); |
| 72 | + } |
| 73 | + expect(di.isValidHostLabel).toHaveBeenCalledTimes(0); |
| 74 | + expect(() => checkRegion("oh-canada", di.isValidHostLabel)).not.toThrow(); |
| 75 | + expect(di.isValidHostLabel).toHaveBeenCalledTimes(1); |
| 76 | + }); |
| 77 | +}); |
0 commit comments