Repository navigation
Expand file tree
/
Copy pathproxy_env_test.go
More file actions
140 lines (126 loc) · 4.83 KB
/
Copy pathproxy_env_test.go
File metadata and controls
140 lines (126 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2026 Columnar Technologies Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbc_test
import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
"github.com/columnar-tech/dbc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const proxyHelperTargetEnv = "DBC_PROXY_TEST_TARGET"
// proxyEnvVars are all the variables that influence net/http proxy selection.
var proxyEnvVars = []string{
"HTTP_PROXY", "http_proxy",
"HTTPS_PROXY", "https_proxy",
"NO_PROXY", "no_proxy",
}
// TestProxyEnvHelper is not a real test. It is re-executed in a subprocess by
// TestProxyEnv because net/http reads the proxy environment variables only
// once per process, so they cannot be varied between subtests in-process.
func TestProxyEnvHelper(t *testing.T) {
target := os.Getenv(proxyHelperTargetEnv)
if target == "" {
t.Skip("only runs as a subprocess of TestProxyEnv")
}
c, err := dbc.NewClient()
require.NoError(t, err)
// The outcome is observed by the parent through the proxy; the request is
// expected to fail since the target host does not exist.
resp, err := c.HTTPClient().Get(target)
if err == nil {
resp.Body.Close()
}
}
func TestProxyEnv(t *testing.T) {
if runtime.GOOS == "js" {
t.Skip("runs itself as a subprocess, which js/wasm cannot start")
}
const (
httpTarget = "http://dbc-proxy-test.invalid/index.yaml"
httpsTarget = "https://dbc-proxy-test.invalid/index.yaml"
)
tests := []struct {
name string
env map[string]string
target string
wantProxy bool
wantMeth string // method the proxy should see
}{
{"HTTP_PROXY used for http", map[string]string{"HTTP_PROXY": "{proxy}"}, httpTarget, true, http.MethodGet},
{"http_proxy used for http", map[string]string{"http_proxy": "{proxy}"}, httpTarget, true, http.MethodGet},
{"HTTPS_PROXY used for https", map[string]string{"HTTPS_PROXY": "{proxy}"}, httpsTarget, true, http.MethodConnect},
{"https_proxy used for https", map[string]string{"https_proxy": "{proxy}"}, httpsTarget, true, http.MethodConnect},
{"HTTPS_PROXY ignored for http", map[string]string{"HTTPS_PROXY": "{proxy}"}, httpTarget, false, ""},
{"HTTP_PROXY ignored for https", map[string]string{"HTTP_PROXY": "{proxy}"}, httpsTarget, false, ""},
{"no proxy variables", nil, httpsTarget, false, ""},
{"NO_PROXY bypasses HTTP_PROXY", map[string]string{"HTTP_PROXY": "{proxy}", "NO_PROXY": "dbc-proxy-test.invalid"}, httpTarget, false, ""},
{"no_proxy bypasses HTTPS_PROXY", map[string]string{"HTTPS_PROXY": "{proxy}", "no_proxy": "dbc-proxy-test.invalid"}, httpsTarget, false, ""},
{"NO_PROXY domain suffix", map[string]string{"HTTPS_PROXY": "{proxy}", "NO_PROXY": ".invalid"}, httpsTarget, false, ""},
{"NO_PROXY wildcard", map[string]string{"HTTPS_PROXY": "{proxy}", "NO_PROXY": "*"}, httpsTarget, false, ""},
{"NO_PROXY for other host", map[string]string{"HTTPS_PROXY": "{proxy}", "NO_PROXY": "example.com"}, httpsTarget, true, http.MethodConnect},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
mu sync.Mutex
seen []*http.Request
)
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, r)
mu.Unlock()
// Refuse everything; seeing the request is all that matters.
w.WriteHeader(http.StatusForbidden)
}))
defer proxy.Close()
cmd := exec.Command(os.Args[0], "-test.run=^TestProxyEnvHelper$")
for _, e := range os.Environ() {
name, _, _ := strings.Cut(e, "=")
if !isProxyEnvVar(name) {
cmd.Env = append(cmd.Env, e)
}
}
cmd.Env = append(cmd.Env, proxyHelperTargetEnv+"="+tt.target)
for k, v := range tt.env {
cmd.Env = append(cmd.Env, k+"="+strings.ReplaceAll(v, "{proxy}", proxy.URL))
}
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
mu.Lock()
defer mu.Unlock()
if !tt.wantProxy {
assert.Empty(t, seen, "request unexpectedly went through the proxy")
return
}
require.Len(t, seen, 1, "expected exactly one request via the proxy")
assert.Equal(t, tt.wantMeth, seen[0].Method)
assert.Equal(t, "dbc-proxy-test.invalid", strings.Split(seen[0].Host, ":")[0])
})
}
}
func isProxyEnvVar(name string) bool {
for _, v := range proxyEnvVars {
if strings.EqualFold(name, v) {
return true
}
}
return false
}