|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFrameworkIsURLWithHTTPSorEmptyString(t *testing.T) { |
| 16 | + var testCases = []struct { |
| 17 | + inputURL interface{} |
| 18 | + expectedErrors []string |
| 19 | + }{ |
| 20 | + { |
| 21 | + inputURL: "http://example.com", |
| 22 | + expectedErrors: []string{ |
| 23 | + "Attribute theTestURLPath invalid URL, expected protocol scheme \"https\", got: http://example.com", |
| 24 | + }, |
| 25 | + }, |
| 26 | + { |
| 27 | + inputURL: "http://example.com/foo", |
| 28 | + expectedErrors: []string{ |
| 29 | + "Attribute theTestURLPath invalid URL, expected protocol scheme \"https\", got: http://example.com/foo", |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + inputURL: "http://example.com#foo", |
| 34 | + expectedErrors: []string{ |
| 35 | + "Attribute theTestURLPath invalid URL, expected protocol scheme \"https\", got: http://example.com#foo", |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + inputURL: "https://example.com/foo", |
| 40 | + expectedErrors: nil, |
| 41 | + }, |
| 42 | + { |
| 43 | + inputURL: "https://example.com#foo", |
| 44 | + expectedErrors: nil, |
| 45 | + }, |
| 46 | + { |
| 47 | + inputURL: "", |
| 48 | + expectedErrors: nil, |
| 49 | + }, |
| 50 | + { |
| 51 | + inputURL: "broken/url", |
| 52 | + expectedErrors: []string{ |
| 53 | + "Attribute theTestURLPath invalid URL, missing host, got: broken/url", |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + inputURL: nil, |
| 58 | + expectedErrors: nil, |
| 59 | + }, |
| 60 | + { |
| 61 | + inputURL: "://example.com", |
| 62 | + expectedErrors: []string{ |
| 63 | + "Attribute theTestURLPath invalid URL: parse \"://example.com\": missing protocol scheme, got: ://example.com", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + urlValidator := IsURLWithHTTPSorEmptyString() |
| 68 | + |
| 69 | + for i, testCase := range testCases { |
| 70 | + t.Run(fmt.Sprintf("test case #%d", i), func(t *testing.T) { |
| 71 | + response := validator.StringResponse{} |
| 72 | + request := validator.StringRequest{ |
| 73 | + Path: path.Root("theTestURLPath"), |
| 74 | + } |
| 75 | + if testCase.inputURL == nil { |
| 76 | + request.ConfigValue = types.StringNull() |
| 77 | + } else { |
| 78 | + request.ConfigValue = types.StringValue(testCase.inputURL.(string)) |
| 79 | + } |
| 80 | + urlValidator.ValidateString(context.Background(), request, &response) |
| 81 | + assert.Equal(t, len(testCase.expectedErrors), len(response.Diagnostics)) |
| 82 | + for i, diagnostic := range response.Diagnostics { |
| 83 | + assert.Regexp(t, regexp.MustCompile(testCase.expectedErrors[i]), diagnostic.Detail()) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments