@@ -3,6 +3,7 @@ package github
33import (
44 "fmt"
55 "regexp"
6+ "strings"
67 "testing"
78
89 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -33,7 +34,7 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
3334 resource .TestMatchResourceAttr (
3435 "data.github_rest_api.test" , "status" , regexp .MustCompile ("200 OK" ),
3536 ),
36- resource .TestCheckResourceAttrSet ("data.github_rest_api.test" , "body" ),
37+ resource .TestMatchResourceAttr ("data.github_rest_api.test" , "body" , regexp . MustCompile ( ".*refs/heads/.*" ) ),
3738 resource .TestCheckResourceAttrSet ("data.github_rest_api.test" , "headers" ),
3839 )
3940
@@ -64,6 +65,50 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
6465
6566 })
6667
68+ t .Run ("queries a collection without error" , func (t * testing.T ) {
69+
70+ config := fmt .Sprintf (`
71+ resource "github_repository" "test" {
72+ name = "tf-acc-test-%[1]s"
73+ auto_init = true
74+ }
75+
76+ data "github_rest_api" "test" {
77+ endpoint = "repos/${github_repository.test.full_name}/git/refs/heads/"
78+ }
79+ ` , randomID )
80+
81+ check := resource .ComposeTestCheckFunc (
82+ resource .TestMatchResourceAttr ("data.github_rest_api.test" , "body" , regexp .MustCompile (`\[.*refs/heads/.*\]` )),
83+ )
84+
85+ testCase := func (t * testing.T , mode string ) {
86+ resource .Test (t , resource.TestCase {
87+ PreCheck : func () { skipUnlessMode (t , mode ) },
88+ Providers : testAccProviders ,
89+ Steps : []resource.TestStep {
90+ {
91+ Config : config ,
92+ Check : check ,
93+ },
94+ },
95+ })
96+ }
97+
98+ t .Run ("with an anonymous account" , func (t * testing.T ) {
99+ t .Skip ("anonymous account not supported for this operation" )
100+ })
101+
102+ t .Run ("with an individual account" , func (t * testing.T ) {
103+ testCase (t , individual )
104+ })
105+
106+ t .Run ("with an organization account" , func (t * testing.T ) {
107+ testCase (t , organization )
108+ })
109+
110+ })
111+
67112 t .Run ("queries an invalid branch without error" , func (t * testing.T ) {
68113
69114 config := fmt .Sprintf (`
@@ -114,4 +159,41 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
114159 })
115160
116161 })
162+
163+ t .Run ("fails for invalid endpoint" , func (t * testing.T ) {
164+
165+ // 4096 characters is the maximum length for a URL
166+ var endpoint = strings .Repeat ("x" , 4096 )
167+ config := fmt .Sprintf (`
168+ data "github_rest_api" "test" {
169+ endpoint = "/%v"
170+ }
171+ ` , endpoint )
172+
173+ testCase := func (t * testing.T , mode string ) {
174+ resource .Test (t , resource.TestCase {
175+ PreCheck : func () { skipUnlessMode (t , mode ) },
176+ Providers : testAccProviders ,
177+ Steps : []resource.TestStep {
178+ {
179+ Config : config ,
180+ ExpectError : regexp .MustCompile ("Error: GET https://api.github.com/xx.*: 414" ),
181+ },
182+ },
183+ })
184+ }
185+
186+ t .Run ("with an anonymous account" , func (t * testing.T ) {
187+ t .Skip ("anonymous account not supported for this operation" )
188+ })
189+
190+ t .Run ("with an individual account" , func (t * testing.T ) {
191+ testCase (t , individual )
192+ })
193+
194+ t .Run ("with an organization account" , func (t * testing.T ) {
195+ testCase (t , organization )
196+ })
197+
198+ })
117199}
0 commit comments