1package agent
2
3import (
4 "os"
5 "testing"
6)
7
8func TestApplyConfiguredHTTPProxy(t *testing.T) {
9 t.Setenv("ZOT_HOME", t.TempDir())
10 for _, key := range []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"} {
11 t.Setenv(key, "")
12 }
13 if err := SaveConfig(Config{HTTPProxy: " http://proxy.example:8080 "}); err != nil {
14 t.Fatal(err)
15 }
16
17 applyConfiguredHTTPProxy()
18
19 if got := getenvForTest("HTTP_PROXY"); got != "http://proxy.example:8080" {
20 t.Fatalf("HTTP_PROXY = %q", got)
21 }
22 if got := getenvForTest("HTTPS_PROXY"); got != "http://proxy.example:8080" {
23 t.Fatalf("HTTPS_PROXY = %q", got)
24 }
25}
26
27func TestApplyConfiguredHTTPProxyPreservesUppercaseEnvironment(t *testing.T) {
28 t.Setenv("ZOT_HOME", t.TempDir())
29 // Set lowercase first because environment variable names are
30 // case-insensitive on Windows.
31 t.Setenv("http_proxy", "")
32 t.Setenv("https_proxy", "")
33 t.Setenv("HTTP_PROXY", "http://http-env.example:8080")
34 t.Setenv("HTTPS_PROXY", "http://https-env.example:8080")
35 if err := SaveConfig(Config{HTTPProxy: "http://config.example:8080"}); err != nil {
36 t.Fatal(err)
37 }
38
39 applyConfiguredHTTPProxy()
40
41 if got := getenvForTest("HTTP_PROXY"); got != "http://http-env.example:8080" {
42 t.Fatalf("HTTP_PROXY = %q", got)
43 }
44 if got := getenvForTest("HTTPS_PROXY"); got != "http://https-env.example:8080" {
45 t.Fatalf("HTTPS_PROXY = %q", got)
46 }
47}
48
49func TestApplyConfiguredHTTPProxyPreservesLowercaseEnvironment(t *testing.T) {
50 t.Setenv("ZOT_HOME", t.TempDir())
51 // Set uppercase first because environment variable names are
52 // case-insensitive on Windows.
53 t.Setenv("HTTP_PROXY", "")
54 t.Setenv("HTTPS_PROXY", "")
55 t.Setenv("http_proxy", "http://http-env.example:8080")
56 t.Setenv("https_proxy", "http://https-env.example:8080")
57 if err := SaveConfig(Config{HTTPProxy: "http://config.example:8080"}); err != nil {
58 t.Fatal(err)
59 }
60
61 applyConfiguredHTTPProxy()
62
63 if got := getenvForTest("http_proxy"); got != "http://http-env.example:8080" {
64 t.Fatalf("http_proxy = %q", got)
65 }
66 if got := getenvForTest("https_proxy"); got != "http://https-env.example:8080" {
67 t.Fatalf("https_proxy = %q", got)
68 }
69}
70
71func TestApplyConfiguredHTTPProxyIgnoresEmptySetting(t *testing.T) {
72 t.Setenv("ZOT_HOME", t.TempDir())
73 for _, key := range []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"} {
74 t.Setenv(key, "")
75 }
76 if err := SaveConfig(Config{HTTPProxy: " "}); err != nil {
77 t.Fatal(err)
78 }
79
80 applyConfiguredHTTPProxy()
81
82 if got := getenvForTest("HTTP_PROXY"); got != "" {
83 t.Fatalf("HTTP_PROXY = %q", got)
84 }
85 if got := getenvForTest("HTTPS_PROXY"); got != "" {
86 t.Fatalf("HTTPS_PROXY = %q", got)
87 }
88}
89
90func getenvForTest(key string) string {
91 return os.Getenv(key)
92}