Skip to content

Commit

Permalink
use internal function to be able to unit test the behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
intxgo committed Feb 5, 2025
1 parent 3fb97d6 commit 04d509d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
"github.com/elastic/elastic-agent/pkg/component"
"golang.org/x/net/http/httpproxy"
)

// InjectProxyEndpointModifier injects a proxy_url value into endpoint's output config if one is not set.
Expand Down Expand Up @@ -84,7 +85,8 @@ func injectProxyURL(m map[string]interface{}, hosts []string) {
if err != nil {
continue
}
proxyURL, err := http.ProxyFromEnvironment(request)
// not using http.ProxyFromEnvironment() to be able to change the environment in unit tests
proxyURL, err := httpproxy.FromEnvironment().ProxyFunc()(request.URL)
if err != nil {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ func TestInjectProxyEndpointModifier(t *testing.T) {
}

func Test_injectProxyURL(t *testing.T) {
t.Setenv("HTTPS_PROXY", "https://localhost:8080")
t.Setenv("HTTP_PROXY", "http://localhost:8081")
t.Setenv("NO_PROXY", "do.not.inject.proxy.for.me")

tests := []struct {
name string
m map[string]interface{}
Expand Down Expand Up @@ -356,10 +360,6 @@ func Test_injectProxyURL(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("HTTPS_PROXY", "https://localhost:8080")
t.Setenv("HTTP_PROXY", "http://localhost:8081")
t.Setenv("NO_PROXY", "do.not.inject.proxy.for.me")

injectProxyURL(tc.m, tc.hosts)
require.Equal(t, tc.expect, tc.m)
})
Expand Down

0 comments on commit 04d509d

Please sign in to comment.