-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
103 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
package validation | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
// IsURLWithNoFragment is a SchemaValidateFunc which tests if the provided value | ||
// is of type string and a valid URL with no fragment. | ||
func IsURLWithNoFragment(i interface{}, k string) (warnings []string, errors []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) | ||
return | ||
} | ||
|
||
if v == "" { | ||
errors = append(errors, fmt.Errorf("expected %q url to not be empty, got %v", k, i)) | ||
return | ||
} | ||
"strings" | ||
|
||
u, err := url.Parse(v) | ||
if err != nil { | ||
errors = append(errors, fmt.Errorf("expected %q to be a valid url, got %v: %+v", k, v, err)) | ||
return | ||
} | ||
|
||
if u.Host == "" { | ||
errors = append(errors, fmt.Errorf("expected %q to have a host, got %v", k, v)) | ||
return | ||
} | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
if u.Fragment != "" { | ||
errors = append(errors, fmt.Errorf("expected %q to have a url with an empty fragment. %s", k, v)) | ||
func IsURLWithHTTPSorEmptyString(i interface{}, s string) ([]string, []error) { | ||
_, errors := validation.IsURLWithHTTPS(i, s) | ||
for _, err := range errors { | ||
if !strings.Contains(err.Error(), "url to not be empty") { | ||
return nil, errors | ||
} | ||
} | ||
|
||
return | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,58 @@ | ||
package validation | ||
|
||
import "testing" | ||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
func TestIsURLWithNoFragment(t *testing.T) { | ||
for url, valid := range map[string]bool{ | ||
"http://example.com": true, | ||
"http://example.com/foo": true, | ||
"http://example.com#foo": false, | ||
"https://example.com/foo": true, | ||
"https://example.com#foo": false, | ||
} { | ||
_, err := IsURLWithNoFragment(url, "url") | ||
if err != nil && valid { | ||
t.Errorf("IsURLWithNoFragment(%s) produced an unexpected error", url) | ||
} | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsURLWithHTTPSorEmptyString(t *testing.T) { | ||
var testCases = []struct { | ||
inputURL string | ||
expectedErrors []string | ||
}{ | ||
{ | ||
inputURL: "http://example.com", | ||
expectedErrors: []string{ | ||
"expected \"theTestURL\" to have a url with schema of: \"https\", got http://example.com", | ||
}, | ||
}, | ||
{ | ||
inputURL: "http://example.com/foo", | ||
expectedErrors: []string{ | ||
"expected \"theTestURL\" to have a url with schema of: \"https\", got http://example.com/foo", | ||
}, | ||
}, | ||
{ | ||
inputURL: "http://example.com#foo", | ||
expectedErrors: []string{ | ||
"expected \"theTestURL\" to have a url with schema of: \"https\", got http://example.com#foo", | ||
}, | ||
}, | ||
{ | ||
inputURL: "https://example.com/foo", | ||
expectedErrors: nil, | ||
}, | ||
{ | ||
inputURL: "https://example.com#foo", | ||
expectedErrors: nil, | ||
}, | ||
{ | ||
inputURL: "", | ||
expectedErrors: nil, | ||
}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
t.Run(fmt.Sprintf("test case #%d", i), func(t *testing.T) { | ||
var errorsAsString []string | ||
_, actualErrors := IsURLWithHTTPSorEmptyString(testCase.inputURL, "theTestURL") | ||
for _, actualError := range actualErrors { | ||
errorsAsString = append(errorsAsString, actualError.Error()) | ||
} | ||
|
||
assert.Equal(t, testCase.expectedErrors, errorsAsString) | ||
}) | ||
} | ||
} |
Oops, something went wrong.