feat(validation): Added validation of baseURL config
This commit is contained in:
parent
68b792dca4
commit
03942636c8
2 changed files with 121 additions and 0 deletions
32
cmd/app.go
32
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}),
|
||||
|
|
|
|||
89
cmd/app_test.go
Normal file
89
cmd/app_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue