-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathkernel_proxy_test.go
More file actions
93 lines (88 loc) · 2.88 KB
/
Copy pathkernel_proxy_test.go
File metadata and controls
93 lines (88 loc) · 2.88 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
package dbsql
import (
"errors"
"net/http"
"net/url"
"testing"
"github.com/databricks/databricks-sql-go/internal/config"
)
// proxyForEndpointFunc maps the injected resolver's decision to a proxy URL
// string (or "" for direct), and returns "" for an unbuildable endpoint. The
// production path uses http.ProxyFromEnvironment, whose env is snapshotted once
// per process (sync.Once) and so can't be re-driven mid-test; the resolver seam
// lets us assert every branch deterministically. Each case mirrors an
// httpproxy-style outcome: proxy set for this host, host excluded by NO_PROXY,
// no proxy configured, and an unbuildable endpoint. Pure Go, so it runs in the
// default CGO_ENABLED=0 build (no kernel lib required).
func TestProxyForEndpoint(t *testing.T) {
validCfg := func() *config.Config {
c := config.WithDefaults()
c.Host = "my-workspace.databricks.com"
c.Port = 443
c.HTTPPath = "/sql/1.0/warehouses/abc"
return c
}
proxyURL, _ := url.Parse("http://corp-proxy:3128")
cases := []struct {
name string
cfg *config.Config
resolve func(*http.Request) (*url.URL, error)
want string
}{
{
name: "proxy set for host",
cfg: validCfg(),
resolve: func(*http.Request) (*url.URL, error) { return proxyURL, nil },
want: "http://corp-proxy:3128",
},
{
// Warehouse-id addressing mode (HTTPPath == "") is what the kernel
// prefers; the proxy must still resolve. Previously ToEndpointURL errored
// on the empty path and the proxy was silently dropped to "" (direct).
name: "warehouse-id mode (no HTTPPath) still resolves proxy",
cfg: func() *config.Config {
c := config.WithDefaults()
c.Host = "my-workspace.databricks.com"
c.Port = 443
c.WarehouseID = "abc" // no HTTPPath
return c
}(),
resolve: func(*http.Request) (*url.URL, error) { return proxyURL, nil },
want: "http://corp-proxy:3128",
},
{
name: "host excluded by NO_PROXY -> direct",
cfg: validCfg(),
resolve: func(*http.Request) (*url.URL, error) { return nil, nil },
want: "",
},
{
name: "resolver error -> direct",
cfg: validCfg(),
resolve: func(*http.Request) (*url.URL, error) { return nil, errors.New("bad proxy url") },
want: "",
},
{
name: "unbuildable endpoint -> direct (resolver never consulted)",
cfg: func() *config.Config {
c := config.WithDefaults()
c.Host = ""
return c
}(),
resolve: func(*http.Request) (*url.URL, error) {
t.Error("resolver must not be called for an unbuildable endpoint")
return proxyURL, nil
},
want: "",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := proxyForEndpointFunc(tc.cfg, tc.resolve); got != tc.want {
t.Errorf("proxyForEndpointFunc = %q, want %q", got, tc.want)
}
})
}
// The production wrapper wires http.ProxyFromEnvironment and must not panic.
_ = proxyForEndpoint(validCfg())
}