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

Allow Host request header to override the http request host #440

Merged
merged 9 commits into from
Jul 23, 2024
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240719-143606.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'data-source/http: allow the `Host` request header to be sent as the http request
host.'
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
time: 2024-07-19T14:36:06.795798-04:00
custom:
Issue: "440"
5 changes: 5 additions & 0 deletions internal/provider/data_source_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
request.Header.Set(name, header)
}

hostHeader := request.Header.Get("Host")
if hostHeader != "" {
request.Host = hostHeader
}

SBGoods marked this conversation as resolved.
Show resolved Hide resolved
response, err := retryClient.Do(request)
if err != nil {
target := &url.Error{}
Expand Down
43 changes: 43 additions & 0 deletions internal/provider/data_source_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,49 @@ func TestDataSource_UnsupportedInsecureCaCert(t *testing.T) {
})
}

func TestDataSource_HostRequestHeaderOverride_200(t *testing.T) {
altHost := "alt-test-host"

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Host != altHost {
w.WriteHeader(404)
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
return
}

w.Header().Set("Content-Type", "text/plain")
w.Header().Set("X-Single", "foobar")
w.Header().Add("X-Double", "1")
w.Header().Add("X-Double", "2")
_, err := w.Write([]byte("1.0.0"))
if err != nil {
t.Errorf("error writing body: %s", err)
}
}))
defer testServer.Close()

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "http" "http_test" {
url = "%s"
request_headers = {
"Host" = "%s"
}
}`, testServer.URL, altHost),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.http.http_test", "status_code", "200"),
resource.TestCheckResourceAttr("data.http.http_test", "response_body", "1.0.0"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.Content-Type", "text/plain"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.X-Single", "foobar"),
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.X-Double", "1, 2"),
),
},
},
})
}

// testProxiedURL is a hardcoded URL used in acceptance testing where it is
// expected that a locally started HTTP proxy will handle the request.
//
Expand Down
Loading