Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
  • Loading branch information
roidelapluie committed Mar 7, 2023
1 parent 26800d4 commit c240aee
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 8 deletions.
18 changes: 12 additions & 6 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,13 +1105,16 @@ type ProxyConfig struct {
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *ProxyConfig) Validate() error {
if len(c.ProxyConnectHeader) > 0 && (!c.ProxyFromEnvironment && (c.ProxyURL.URL == nil || c.ProxyURL.String() == "")) {
return fmt.Errorf("if proxy_connect_header is configured proxy_url or proxy_from_environment must also be configured")
return fmt.Errorf("if proxy_connect_header is configured, proxy_url or proxy_from_environment must also be configured")
}
if c.ProxyFromEnvironment && c.ProxyURL.URL != nil && c.ProxyURL.String() != "" {
return fmt.Errorf("if proxy_from_environment is configured proxy_url must not be configured")
return fmt.Errorf("if proxy_from_environment is configured, proxy_url must not be configured")
}
if c.ProxyFromEnvironment && c.NoProxy != "" {
return fmt.Errorf("if proxy_from_environment is configured no_proxy must not be configured")
return fmt.Errorf("if proxy_from_environment is configured, no_proxy must not be configured")
}
if c.ProxyURL.URL == nil && c.NoProxy != "" {
return fmt.Errorf("if no_proxy is configured, proxy_url must also be configured")
}
return nil
}
Expand All @@ -1128,7 +1131,10 @@ func (c *ProxyConfig) Proxy() (fn func(*http.Request) (*url.URL, error)) {
return
}
if c.ProxyFromEnvironment {
c.proxyFunc = http.ProxyFromEnvironment
proxyFn := httpproxy.FromEnvironment().ProxyFunc()
c.proxyFunc = func(req *http.Request) (*url.URL, error) {
return proxyFn(req.URL)
}
return
}
if c.ProxyURL.URL != nil && c.ProxyURL.URL.String() != "" {
Expand All @@ -1141,9 +1147,9 @@ func (c *ProxyConfig) Proxy() (fn func(*http.Request) (*url.URL, error)) {
HTTPSProxy: c.ProxyURL.String(),
NoProxy: c.NoProxy,
}
fn := proxy.ProxyFunc()
proxyFn := proxy.ProxyFunc()
c.proxyFunc = func(req *http.Request) (*url.URL, error) {
return fn(req.URL)
return proxyFn(req.URL)
}
}
return
Expand Down
145 changes: 143 additions & 2 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ var invalidHTTPClientConfigs = []struct {
},
{
httpClientConfigFile: "testdata/http.conf.proxy-from-env.bad.yaml",
errMsg: "if proxy_from_environment is configured proxy_url must not be configured",
errMsg: "if proxy_from_environment is configured, proxy_url must not be configured",
},
{
httpClientConfigFile: "testdata/http.conf.no-proxy.bad.yaml",
errMsg: "if proxy_from_environment is configured no_proxy must not be configured",
errMsg: "if proxy_from_environment is configured, no_proxy must not be configured",
},
{
httpClientConfigFile: "testdata/http.conf.no-proxy-without-proxy-url.bad.yaml",
errMsg: "if no_proxy is configured, proxy_url must also be configured",
},
}

Expand Down Expand Up @@ -1697,3 +1701,140 @@ func loadHTTPConfigJSONFile(filename string) (*HTTPClientConfig, []byte, error)
}
return cfg, content, nil
}

func TestProxyConfig_Proxy(t *testing.T) {
var proxyServer *httptest.Server

defer func() {
if proxyServer != nil {
proxyServer.Close()
}
}()

proxyServerHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s", r.URL.Path)
})

proxyServer = httptest.NewServer(proxyServerHandler)

testCases := []struct {
name string
proxyConfig string
expectedProxyURL string
targetURL string
proxyEnv string
noProxyEnv string
}{
{
name: "proxy from environment",
proxyConfig: `proxy_from_environment: true`,
expectedProxyURL: proxyServer.URL,
proxyEnv: proxyServer.URL,
targetURL: "http://prometheus.io/",
},
{
name: "proxy_from_environment with no_proxy",
proxyConfig: `proxy_from_environment: true`,
expectedProxyURL: "",
proxyEnv: proxyServer.URL,
noProxyEnv: "prometheus.io",
targetURL: "http://prometheus.io/",
},
{
name: "proxy_from_environment and localhost",
proxyConfig: `proxy_from_environment: true`,
expectedProxyURL: "",
proxyEnv: proxyServer.URL,
targetURL: "http://localhost/",
},
{
name: "valid proxy_url and localhost",
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
expectedProxyURL: proxyServer.URL,
targetURL: "http://localhost/",
},
{
name: "valid proxy_url and no_proxy and localhost",
proxyConfig: fmt.Sprintf(`proxy_url: %s
no_proxy: prometheus.io`, proxyServer.URL),
expectedProxyURL: "",
targetURL: "http://localhost/",
},
{
name: "valid proxy_url",
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
expectedProxyURL: proxyServer.URL,
targetURL: "http://prometheus.io/",
},
{
name: "valid proxy url and no_proxy",
proxyConfig: fmt.Sprintf(`proxy_url: %s
no_proxy: prometheus.io`, proxyServer.URL),
expectedProxyURL: "",
targetURL: "http://prometheus.io/",
},
{
name: "valid proxy url and no_proxies",
proxyConfig: fmt.Sprintf(`proxy_url: %s
no_proxy: promcon.io,prometheus.io,cncf.io`, proxyServer.URL),
expectedProxyURL: "",
targetURL: "http://prometheus.io/",
},
{
name: "valid proxy url and no_proxies that do not include target",
proxyConfig: fmt.Sprintf(`proxy_url: %s
no_proxy: promcon.io,cncf.io`, proxyServer.URL),
expectedProxyURL: proxyServer.URL,
targetURL: "http://prometheus.io/",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if proxyServer != nil {
defer proxyServer.Close()
}

var proxyConfig ProxyConfig

err := yaml.Unmarshal([]byte(tc.proxyConfig), &proxyConfig)
if err != nil {
t.Errorf("failed to unmarshal proxy config: %v", err)
return
}

if tc.proxyEnv != "" {
currentProxy := os.Getenv("HTTP_PROXY")
t.Cleanup(func() { os.Setenv("HTTP_PROXY", currentProxy) })
os.Setenv("HTTP_PROXY", tc.proxyEnv)
}

if tc.noProxyEnv != "" {
currentProxy := os.Getenv("NO_PROXY")
t.Cleanup(func() { os.Setenv("NO_PROXY", currentProxy) })
os.Setenv("NO_PROXY", tc.noProxyEnv)
}

req := httptest.NewRequest("GET", tc.targetURL, nil)

proxyFunc := proxyConfig.Proxy()
resultURL, err := proxyFunc(req)

if err != nil {
t.Fatalf("expected no error, but got: %v", err)
return
}
if tc.expectedProxyURL == "" && resultURL != nil {
t.Fatalf("expected no result URL, but got: %s", resultURL.String())
return
}
if tc.expectedProxyURL != "" && resultURL == nil {
t.Fatalf("expected result URL, but got nil")
return
}
if tc.expectedProxyURL != "" && resultURL.String() != tc.expectedProxyURL {
t.Fatalf("expected result URL: %s, but got: %s", tc.expectedProxyURL, resultURL.String())
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no_proxy: 127.0.0.1

0 comments on commit c240aee

Please sign in to comment.