Skip to content

Commit

Permalink
Merge pull request #1481 from gruntwork-io/http-global-proxy
Browse files Browse the repository at this point in the history
httpdo* with global proxy
  • Loading branch information
james03160927 authored Dec 7, 2024
2 parents a990ca7 + b803727 commit 6c46211
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 2 additions & 3 deletions modules/http-helper/http_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ func HTTPDoWithOptionsE(
) (int, string, error) {
logger.Default.Logf(t, "Making an HTTP %s call to URL %s", options.Method, options.Url)

tr := &http.Transport{
TLSClientConfig: options.TlsConfig,
}
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = options.TlsConfig

client := http.Client{
// By default, Go does not impose a timeout, so an HTTP connection attempt can hang for a LONG time.
Expand Down
24 changes: 24 additions & 0 deletions modules/http-helper/http_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -204,3 +205,26 @@ func failRetryHandler(w http.ResponseWriter, r *http.Request) {
w.Write(bytes)
}
}

func TestGlobalProxy(t *testing.T) {
proxiedURL := ""
httpProxy := getTestServerForFunction(func(w http.ResponseWriter, r *http.Request) {
proxiedURL = r.RequestURI
bodyCopyHandler(w, r)
})
t.Cleanup(httpProxy.Close)

t.Setenv("HTTP_PROXY", httpProxy.URL)
targetURL := "http://www.notexist.com/"
body := "should be copied"

st, b, err := HTTPDoWithOptionsE(t, HttpDoOptions{
Url: targetURL,
Method: http.MethodPost,
Body: strings.NewReader(body),
})
require.NoError(t, err)
assert.Equal(t, http.StatusOK, st)
assert.Equal(t, targetURL, proxiedURL)
assert.Equal(t, body, b)
}

0 comments on commit 6c46211

Please sign in to comment.