Skip to content

Commit

Permalink
fix(data_source_github_rest_api): only allow for 404 on err (#2154)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
riezebosch and kfcampbell authored Mar 8, 2024
1 parent 480825c commit 39c5e06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion github/data_source_github_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error
return err
}

resp, _ := client.Do(ctx, req, &body)
resp, err := client.Do(ctx, req, &body)
if err != nil && resp.StatusCode != 404 {
return err
}

h, err := json.Marshal(resp.Header)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions github/data_source_github_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -114,4 +115,41 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
})

})

t.Run("fails for invalid endpoint", func(t *testing.T) {

// 4096 characters is the maximum length for a URL
var endpoint = strings.Repeat("x", 4096)
config := fmt.Sprintf(`
data "github_rest_api" "test" {
endpoint = "/%v"
}
`, endpoint)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("Error: GET https://api.github.com/xx.*: 414"),
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}

0 comments on commit 39c5e06

Please sign in to comment.