Skip to content

Commit 2caff35

Browse files
srlynch1rainkwan
authored andcommitted
Implement follow_redirects feature in HTTP data source
- Added a new optional attribute `follow_redirects` to control HTTP redirect behavior. - Updated the `Read` method to handle the no-follow behavior when `follow_redirects` is set to false. - Introduced a new test `TestDataSource_FollowRedirects` to verify the functionality of the redirect handling.
1 parent 2619776 commit 2caff35

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

internal/provider/data_source_http.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ a 5xx-range (except 501) status code is received. For further details see
171171
Description: `The HTTP response status code.`,
172172
Computed: true,
173173
},
174+
175+
"follow_redirects": schema.BoolAttribute{
176+
Description: "If false, do not follow HTTP redirects. Defaults to true.",
177+
Optional: true,
178+
},
174179
},
175180

176181
Blocks: map[string]schema.Block{
@@ -295,6 +300,13 @@ func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
295300
retryClient := retryablehttp.NewClient()
296301
retryClient.HTTPClient.Transport = clonedTr
297302

303+
// Configure no-follow behavior when follow_redirects is explicitly false
304+
if !model.FollowRedirects.IsNull() && !model.FollowRedirects.ValueBool() {
305+
retryClient.HTTPClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
306+
return http.ErrUseLastResponse
307+
}
308+
}
309+
298310
var timeout time.Duration
299311

300312
if model.RequestTimeout.ValueInt64() > 0 {
@@ -437,6 +449,7 @@ type modelV0 struct {
437449
Body types.String `tfsdk:"body"`
438450
ResponseBodyBase64 types.String `tfsdk:"response_body_base64"`
439451
StatusCode types.Int64 `tfsdk:"status_code"`
452+
FollowRedirects types.Bool `tfsdk:"follow_redirects"`
440453
}
441454

442455
type retryModel struct {

internal/provider/data_source_http_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,52 @@ func TestDataSource_ResponseBodyBinary(t *testing.T) {
10201020
})
10211021
}
10221022

1023+
// TestDataSource_FollowRedirects verifies that the follow_redirects flag controls redirect behavior.
1024+
func TestDataSource_FollowRedirects(t *testing.T) {
1025+
// Target server returns 200 OK
1026+
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1027+
w.Header().Set("Content-Type", "text/plain")
1028+
w.WriteHeader(http.StatusOK)
1029+
_, _ = w.Write([]byte("OK"))
1030+
}))
1031+
defer target.Close()
1032+
1033+
// Redirect server sends 302 to target
1034+
redirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1035+
http.Redirect(w, r, target.URL, http.StatusFound)
1036+
}))
1037+
defer redirect.Close()
1038+
1039+
resource.UnitTest(t, resource.TestCase{
1040+
ProtoV5ProviderFactories: protoV5ProviderFactories(),
1041+
Steps: []resource.TestStep{
1042+
{
1043+
Config: fmt.Sprintf(`
1044+
data "http" "http_test" {
1045+
url = %q
1046+
follow_redirects = false
1047+
}
1048+
`, redirect.URL),
1049+
Check: resource.ComposeTestCheckFunc(
1050+
resource.TestCheckResourceAttr("data.http.http_test", "status_code", fmt.Sprintf("%d", http.StatusFound)),
1051+
resource.TestCheckResourceAttr("data.http.http_test", "response_headers.Location", target.URL),
1052+
),
1053+
},
1054+
{
1055+
Config: fmt.Sprintf(`
1056+
data "http" "http_test" {
1057+
url = %q
1058+
}
1059+
`, redirect.URL),
1060+
Check: resource.ComposeTestCheckFunc(
1061+
resource.TestCheckResourceAttr("data.http.http_test", "status_code", fmt.Sprintf("%d", http.StatusOK)),
1062+
resource.TestCheckResourceAttr("data.http.http_test", "response_body", "OK"),
1063+
),
1064+
},
1065+
},
1066+
})
1067+
}
1068+
10231069
func checkServerAndProxyRequestCount(proxyRequestCount, serverRequestCount *int) resource.TestCheckFunc {
10241070
return func(_ *terraform.State) error {
10251071
if *proxyRequestCount != *serverRequestCount {

0 commit comments

Comments
 (0)