From 053de33fa7938de5d30d472e0e122c07a4859518 Mon Sep 17 00:00:00 2001 From: Richard Robert Reitz Date: Mon, 29 Sep 2025 10:29:11 +0200 Subject: [PATCH] feat(validation): Added validation of baseURL config --- cmd/app.go | 32 ++++++++++++++++++ cmd/app_test.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 cmd/app_test.go diff --git a/cmd/app.go b/cmd/app.go index 8cac4e5..fee8835 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "net/url" "os" "time" @@ -19,11 +20,42 @@ var ( region string ) +func validateBaseURL(baseURL string) error { + url, err := url.Parse(baseURL) + if err != nil { + return fmt.Errorf("Error parsing baseURL: '%s' with error '%s'", baseURL, err.Error()) + } + + if url.Scheme == "" { + return fmt.Errorf("Error parsing baseURL: '%s' baseURL schema should be set", baseURL) + } + + if !(url.Path == "" || url.Path == "/") { + return fmt.Errorf("Error parsing baseURL: '%s' baseURL should not contain any path '%s'", baseURL, url.Path) + } + + if len(url.Query()) > 0 { + return fmt.Errorf("Error parsing baseURL: '%s' baseURL should not contain any queries '%s'", baseURL, url.RawQuery) + } + + if len(url.Fragment) > 0 { + return fmt.Errorf("Error parsing baseURL: '%s' baseURL should not contain any fragment '%s'", baseURL, url.Fragment) + } + + return nil +} + func newSDKClient() *edgeconnect.Client { baseURL := viper.GetString("base_url") username := viper.GetString("username") password := viper.GetString("password") + err := validateBaseURL(baseURL) + if err != nil { + fmt.Printf("Error parsing baseURL: '%s' with error '%s'", baseURL, err.Error()) + os.Exit(1) + } + if username != "" && password != "" { return edgeconnect.NewClientWithCredentials(baseURL, username, password, edgeconnect.WithHTTPClient(&http.Client{Timeout: 30 * time.Second}), diff --git a/cmd/app_test.go b/cmd/app_test.go new file mode 100644 index 0000000..5f10b62 --- /dev/null +++ b/cmd/app_test.go @@ -0,0 +1,89 @@ +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidateBaseURL(t *testing.T) { + tests := []struct { + name string + input string + expectError bool + }{ + { + name: "valid URL", + input: "https://hub.edge.de", + expectError: false, + }, + { + name: "valid URL with slash", + input: "https://hub.edge.de/", + expectError: false, + }, + + { + name: "no schema", + input: "hub.edge.de", + expectError: true, + }, + + { + name: "contains path and query", + input: "http://hub.edge.de/index.html?a=b", + expectError: true, + }, + { + name: "contains query", + input: "http://hub.edge.de/?a=b", + expectError: true, + }, + + { + name: "contains path and fragment", + input: "http://hub.edge.de/index.html#abc", + expectError: true, + }, + { + name: "contains fragment", + input: "http://hub.edge.de/#abc", + expectError: true, + }, + + { + name: "contains path, query and fragment", + input: "http://hub.edge.de/index.html?a=b#abc", + expectError: true, + }, + { + name: "contains query and fragment", + input: "http://hub.edge.de/?a=b#abc", + expectError: true, + }, + + { + name: "contains path, fragment and query", + input: "http://hub.edge.de/index.html#abc?a=b", + expectError: true, + }, + { + name: "contains fragment and query", + input: "http://hub.edge.de/#abc?a=b", + expectError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateBaseURL(tt.input) + + // Verify results + if tt.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +}