diff --git a/internal/provider/data_source.go b/internal/provider/data_source.go index 65fd57a8..0249b74a 100644 --- a/internal/provider/data_source.go +++ b/internal/provider/data_source.go @@ -50,7 +50,7 @@ func dataSource() *schema.Resource { Elem: &schema.Schema{ Type: schema.TypeString, }, - + }, "ca_certificate": { Type: schema.TypeString, Optional: true, diff --git a/internal/provider/data_source_test.go b/internal/provider/data_source_test.go index cdad29ce..33e8197b 100644 --- a/internal/provider/data_source_test.go +++ b/internal/provider/data_source_test.go @@ -1,10 +1,13 @@ package provider import ( + "crypto/x509" + "encoding/pem" "fmt" "net/http" "net/http/httptest" "regexp" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -254,42 +257,193 @@ func TestDataSource_utf16(t *testing.T) { // }, // }) // } +const testDataSourceConfig_basic_TLS_insecure = ` +data "http" "http_test" { + url = "%s/meta_%d.txt" + insecure = true +} + +output "body" { + value = data.http.http_test.body +} + +output "response_headers" { + value = data.http.http_test.response_headers +} +` + +func TestDataSource_http200_TLS_insecure(t *testing.T) { + testHttpMock := setUpMockHttpTLSServer() + + defer testHttpMock.server.Close() + + resource.UnitTest(t, resource.TestCase{ + Providers: testProviders, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(testDataSourceConfig_basic_TLS_insecure, testHttpMock.server.URL, 200), + Check: func(s *terraform.State) error { + _, ok := s.RootModule().Resources["data.http.http_test"] + if !ok { + return fmt.Errorf("missing data resource") + } + + outputs := s.RootModule().Outputs + + if outputs["body"].Value != "1.0.0" { + return fmt.Errorf( + `'body' output is %s; want '1.0.0'`, + outputs["body"].Value, + ) + } + + response_headers := outputs["response_headers"].Value.(map[string]interface{}) + + if response_headers["X-Single"].(string) != "foobar" { + return fmt.Errorf( + `'X-Single' response header is %s; want 'foobar'`, + response_headers["X-Single"].(string), + ) + } + + if response_headers["X-Double"].(string) != "1, 2" { + return fmt.Errorf( + `'X-Double' response header is %s; want '1, 2'`, + response_headers["X-Double"].(string), + ) + } + + return nil + }, + }, + }, + }) +} + +const testDataSourceConfig_basic_TLS_CA = ` +data "http" "http_test" { + url = "%s/meta_%d.txt" + ca_certificate = <