Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use http.DefaultTransport with custom TLS config #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions modules/http-helper/http_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ func HttpGetE(t testing.TestingT, url string, tlsConfig *tls.Config) (int, strin
func HttpGetWithOptionsE(t testing.TestingT, options HttpGetOptions) (int, string, error) {
logger.Logf(t, "Making an HTTP GET call to URL %s", options.Url)

// Set HTTP client transport config
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.
Timeout: time.Duration(options.Timeout) * time.Second,
// Include the previously created transport config
Transport: tr,
// Use http.DefaultTransport with custom TLS configuration applied
Transport: getDefaultHttpTransportWithTlsConfig(options.TlsConfig),
}

resp, err := client.Get(options.Url)
Expand Down Expand Up @@ -263,14 +259,11 @@ func HTTPDoWithOptionsE(
) (int, string, error) {
logger.Logf(t, "Making an HTTP %s call to URL %s", options.Method, options.Url)

tr := &http.Transport{
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.
Timeout: time.Duration(options.Timeout) * time.Second,
Transport: tr,
Timeout: time.Duration(options.Timeout) * time.Second,
// Use http.DefaultTransport with custom TLS configuration applied
Transport: getDefaultHttpTransportWithTlsConfig(options.TlsConfig),
}

req := newRequest(options.Method, options.Url, options.Body, options.Headers)
Expand Down Expand Up @@ -534,6 +527,13 @@ func HTTPDoWithCustomValidationWithOptionsE(t testing.TestingT, options HttpDoOp
return nil
}

func getDefaultHttpTransportWithTlsConfig(config *tls.Config) *http.Transport {
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = config

return tr
}

func newRequest(method string, url string, body io.Reader, headers map[string]string) *http.Request {
req, err := http.NewRequest(method, url, body)
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions modules/http-helper/http_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package http_helper

import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -19,6 +21,56 @@ func getTestServerForFunction(handler func(w http.ResponseWriter,
return httptest.NewServer(http.HandlerFunc(handler))
}

func getTLSTestServerForFunction(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
server := httptest.NewUnstartedServer(http.HandlerFunc(handler))
server.EnableHTTP2 = true
server.StartTLS()

return server
}

func getTLSConfigForTestServer(server *httptest.Server) *tls.Config {
certpool := x509.NewCertPool()
certpool.AddCert(server.Certificate())
return &tls.Config{
RootCAs: certpool,
}
}

func TestHttpGet(t *testing.T) {
expectedBody := "Hello, Terratest!"
t.Parallel()
ts := getTestServerForFunction(getHandler(expectedBody))
defer ts.Close()
url := ts.URL
statusCode, respBody := HttpGet(t, url, nil)

expectedCode := 200
if statusCode != expectedCode {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, expectedCode)
}
if respBody != expectedBody {
t.Errorf("handler returned wrong body: got %v want %v", respBody, expectedBody)
}
}

func TestTLSHttpGet(t *testing.T) {
expectedBody := "Hello, Terratest!"
t.Parallel()
ts := getTLSTestServerForFunction(getHandler(expectedBody))
defer ts.Close()
url := ts.URL
statusCode, respBody := HttpGet(t, url, getTLSConfigForTestServer(ts))

expectedCode := 200
if statusCode != expectedCode {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, expectedCode)
}
if respBody != expectedBody {
t.Errorf("handler returned wrong body: got %v want %v", respBody, expectedBody)
}
}

func TestOkBody(t *testing.T) {
t.Parallel()
ts := getTestServerForFunction(bodyCopyHandler)
Expand All @@ -37,6 +89,24 @@ func TestOkBody(t *testing.T) {
}
}

func TestTLSOkBody(t *testing.T) {
t.Parallel()
ts := getTLSTestServerForFunction(bodyCopyHandler)
defer ts.Close()
url := ts.URL
expectedBody := "Hello, Terratest!"
body := bytes.NewReader([]byte(expectedBody))
statusCode, respBody := HTTPDo(t, "POST", url, body, nil, getTLSConfigForTestServer(ts))

expectedCode := 200
if statusCode != expectedCode {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, expectedCode)
}
if respBody != expectedBody {
t.Errorf("handler returned wrong body: got %v want %v", respBody, expectedBody)
}
}

func TestHTTPDoWithValidation(t *testing.T) {
t.Parallel()
ts := getTestServerForFunction(bodyCopyHandler)
Expand Down Expand Up @@ -139,6 +209,13 @@ func TestErrorWithRetry(t *testing.T) {
}
}

func getHandler(body string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(body))
}
}

func bodyCopyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
body, _ := ioutil.ReadAll(r.Body)
Expand Down