Skip to content

Commit

Permalink
Use built-in library for URL parsing
Browse files Browse the repository at this point in the history
Signed-off-by: jingfu wang <jingfu.j.wang@ibm.com>
  • Loading branch information
GeekArthur committed Apr 30, 2020
1 parent cd66e5c commit 0e34086
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
6 changes: 3 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,13 @@ func CheckKubeConfigExist() bool {
}

// ValidateURL validates the URL
func ValidateURL(url string) error {
re, err := regexp.Compile(`^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$`)
func ValidateURL(sourceURL string) error {
u, err := url.Parse(sourceURL)
if err != nil {
return err
}

if !re.MatchString(url) {
if len(u.Host) == 0 || len(u.Scheme) == 0 {
return errors.New("URL is invalid")
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,17 +1623,17 @@ func TestValidateURL(t *testing.T) {
}{
{
name: "Case 1: Valid URL",
url: "http://www.example.com/product",
url: "http://www.example.com/",
wantErr: false,
},
{
name: "Case 2: Valid URL with parameters",
url: "http://www.example.com/products?id=1&page=2",
wantErr: false,
name: "Case 2: Invalid URL - No host",
url: "http://",
wantErr: true,
},
{
name: "Case 3: Invalid URL",
url: "fakeURL",
name: "Case 3: Invalid URL - No scheme",
url: "://www.example.com/",
wantErr: true,
},
}
Expand Down
31 changes: 17 additions & 14 deletions tests/integration/devfile/cmd_devfile_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var _ = Describe("odo devfile registry command tests", func() {
var project string
var context string
var currentWorkingDirectory string
const registryName string = "RegistryName"
const addRegistryURL string = "http://www.example.com/"
const updateRegistryURL string = "http://www.example.com/update"

// This is run after every Spec (It)
var _ = BeforeEach(func() {
Expand Down Expand Up @@ -50,39 +53,39 @@ var _ = Describe("odo devfile registry command tests", func() {

Context("When executing registry commands with the registry is not present", func() {
It("Should successfully add the registry", func() {
helper.CmdShouldPass("odo", "registry", "add", "TestRegistryName", "TestRegistryURL")
helper.CmdShouldPass("odo", "registry", "add", registryName, addRegistryURL)
output := helper.CmdShouldPass("odo", "registry", "list")
helper.MatchAllInOutput(output, []string{"TestRegistryName", "TestRegistryURL"})
helper.CmdShouldPass("odo", "registry", "delete", "TestRegistryName", "-f")
helper.MatchAllInOutput(output, []string{registryName, addRegistryURL})
helper.CmdShouldPass("odo", "registry", "delete", registryName, "-f")
})

It("Should fail to update the registry", func() {
helper.CmdShouldFail("odo", "registry", "update", "TestRegistryName", "TestRegistryURL", "-f")
helper.CmdShouldFail("odo", "registry", "update", registryName, updateRegistryURL, "-f")
})

It("Should fail to delete the registry", func() {
helper.CmdShouldFail("odo", "registry", "delete", "TestRegistryName", "-f")
helper.CmdShouldFail("odo", "registry", "delete", registryName, "-f")
})
})

Context("When executing registry commands with the registry is present", func() {
It("Should fail to add the registry", func() {
helper.CmdShouldPass("odo", "registry", "add", "TestRegistryName", "TestRegistryURL")
helper.CmdShouldFail("odo", "registry", "add", "TestRegistryName", "NewTestRegistryURL")
helper.CmdShouldPass("odo", "registry", "delete", "TestRegistryName", "-f")
helper.CmdShouldPass("odo", "registry", "add", registryName, addRegistryURL)
helper.CmdShouldFail("odo", "registry", "add", registryName, addRegistryURL)
helper.CmdShouldPass("odo", "registry", "delete", registryName, "-f")
})

It("Should successfully update the registry", func() {
helper.CmdShouldPass("odo", "registry", "add", "TestRegistryName", "TestRegistryURL")
helper.CmdShouldPass("odo", "registry", "update", "TestRegistryName", "NewTestRegistryURL", "-f")
helper.CmdShouldPass("odo", "registry", "add", registryName, addRegistryURL)
helper.CmdShouldPass("odo", "registry", "update", registryName, updateRegistryURL, "-f")
output := helper.CmdShouldPass("odo", "registry", "list")
helper.MatchAllInOutput(output, []string{"TestRegistryName", "NewTestRegistryURL"})
helper.CmdShouldPass("odo", "registry", "delete", "TestRegistryName", "-f")
helper.MatchAllInOutput(output, []string{registryName, updateRegistryURL})
helper.CmdShouldPass("odo", "registry", "delete", registryName, "-f")
})

It("Should successfully delete the registry", func() {
helper.CmdShouldPass("odo", "registry", "add", "TestRegistryName", "TestRegistryURL")
helper.CmdShouldPass("odo", "registry", "delete", "TestRegistryName", "-f")
helper.CmdShouldPass("odo", "registry", "add", registryName, addRegistryURL)
helper.CmdShouldPass("odo", "registry", "delete", registryName, "-f")
})
})
})

0 comments on commit 0e34086

Please sign in to comment.