|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import Testing |
| 19 | + |
| 20 | +@testable import ContainerizationExtras |
| 21 | + |
| 22 | +struct ProxyUtilsTests { |
| 23 | + |
| 24 | + @Test("HTTP proxy resolution") |
| 25 | + func testHttpProxy() { |
| 26 | + let env = ["http_proxy": "http://proxy.local:8080"] |
| 27 | + let proxy = ProxyUtils.proxy(for: "example.com", env: env) |
| 28 | + #expect(proxy?.absoluteString == "http://proxy.local:8080") |
| 29 | + } |
| 30 | + |
| 31 | + @Test("HTTPS proxy resolution") |
| 32 | + func testHttpsProxy() { |
| 33 | + let env = ["https_proxy": "https://secureproxy.local:8443"] |
| 34 | + let proxy = ProxyUtils.proxy(for: "secure.com", env: env) |
| 35 | + #expect(proxy?.absoluteString == "https://secureproxy.local:8443") |
| 36 | + } |
| 37 | + |
| 38 | + @Test("NO_PROXY exact match") |
| 39 | + func testNoProxyExactMatch() { |
| 40 | + let env = [ |
| 41 | + "http_proxy": "http://proxy.local:8080", |
| 42 | + "NO_PROXY": "example.com", |
| 43 | + ] |
| 44 | + let proxy = ProxyUtils.proxy(for: "example.com", env: env) |
| 45 | + #expect(proxy == nil) |
| 46 | + } |
| 47 | + |
| 48 | + @Test("Uppercase HTTP_PROXY is respected") |
| 49 | + func testUppercaseHttpProxy() { |
| 50 | + let env = ["HTTP_PROXY": "http://upper.local:8081"] |
| 51 | + let proxy = ProxyUtils.proxy(for: "upper.com", env: env) |
| 52 | + #expect(proxy?.absoluteString == "http://upper.local:8081") |
| 53 | + } |
| 54 | + |
| 55 | + @Test("Lowercase no_proxy is respected") |
| 56 | + func testLowercaseNoProxy() { |
| 57 | + let env = [ |
| 58 | + "http_proxy": "http://proxy.local:8080", |
| 59 | + "no_proxy": "lower.com", |
| 60 | + ] |
| 61 | + let proxy = ProxyUtils.proxy(for: "lower.com", env: env) |
| 62 | + #expect(proxy == nil) |
| 63 | + } |
| 64 | + |
| 65 | + @Test("HTTPS proxy has higher priority than HTTP proxy") |
| 66 | + func testHttpsPreferredOverHttp() { |
| 67 | + let env = [ |
| 68 | + "http_proxy": "http://proxy.local:8080", |
| 69 | + "https_proxy": "https://secureproxy.local:8443", |
| 70 | + ] |
| 71 | + let proxy = ProxyUtils.proxy(for: "secure.com", env: env) |
| 72 | + #expect(proxy?.absoluteString == "https://secureproxy.local:8443") |
| 73 | + } |
| 74 | + |
| 75 | + @Test("Uppercase HTTP_PROXY overrides lowercase http_proxy") |
| 76 | + func testUppercaseOverridesLowercaseHttp() { |
| 77 | + let env = [ |
| 78 | + "http_proxy": "http://lower.local:8080", |
| 79 | + "HTTP_PROXY": "http://upper.local:8081", |
| 80 | + ] |
| 81 | + let proxy = ProxyUtils.proxy(for: "example.com", env: env) |
| 82 | + #expect(proxy?.absoluteString == "http://upper.local:8081") |
| 83 | + } |
| 84 | + |
| 85 | + @Test("Uppercase HTTPS_PROXY overrides lowercase https_proxy") |
| 86 | + func testUppercaseOverridesLowercaseHttps() { |
| 87 | + let env = [ |
| 88 | + "https_proxy": "https://lower.local:8443", |
| 89 | + "HTTPS_PROXY": "https://upper.local:8444", |
| 90 | + ] |
| 91 | + let proxy = ProxyUtils.proxy(for: "secure.com", env: env) |
| 92 | + #expect(proxy?.absoluteString == "https://upper.local:8444") |
| 93 | + } |
| 94 | + |
| 95 | + @Test("Uppercase NO_PROXY overrides lowercase no_proxy") |
| 96 | + func testUppercaseOverridesLowercaseNoProxy() { |
| 97 | + let env = [ |
| 98 | + "http_proxy": "http://proxy.local:8080", |
| 99 | + "no_proxy": "foo.com", |
| 100 | + "NO_PROXY": "bar.com", |
| 101 | + ] |
| 102 | + let proxyFoo = ProxyUtils.proxy(for: "foo.com", env: env) |
| 103 | + let proxyBar = ProxyUtils.proxy(for: "bar.com", env: env) |
| 104 | + |
| 105 | + #expect(proxyFoo != nil) |
| 106 | + #expect(proxyBar == nil) |
| 107 | + } |
| 108 | +} |
0 commit comments