mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-07 10:05:48 +08:00
Remove u.Scheme as we are handling that just before parsing (#266)
Added tests
This commit is contained in:
committed by
Victor Vrantchan
parent
a0b092af5c
commit
9c9455330a
@@ -96,25 +96,34 @@ func setCmd(cfg *ClientConfig, args []string) error {
|
||||
cfg.APIToken = *flToken
|
||||
}
|
||||
|
||||
if *flServerURL != "" {
|
||||
if !(strings.HasPrefix(*flServerURL, "http") ||
|
||||
strings.HasPrefix(*flServerURL, "https")) {
|
||||
*flServerURL = "https://" + *flServerURL
|
||||
}
|
||||
u, err := url.Parse(*flServerURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.Scheme = "https"
|
||||
u.Path = "/"
|
||||
cfg.ServerURL = u.String()
|
||||
validatedURL, err := validateServerURL(*flServerURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.ServerURL = validatedURL
|
||||
|
||||
cfg.SkipVerify = *flSkipVerify
|
||||
|
||||
return SaveClientConfig(cfg)
|
||||
}
|
||||
|
||||
func validateServerURL(serverURL string) (string, error) {
|
||||
if serverURL != "" {
|
||||
if !(strings.HasPrefix(serverURL, "http") ||
|
||||
strings.HasPrefix(serverURL, "https")) {
|
||||
serverURL = "https://" + serverURL
|
||||
}
|
||||
u, err := url.Parse(serverURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u.Path = "/"
|
||||
serverURL = u.String()
|
||||
}
|
||||
return serverURL, nil
|
||||
|
||||
}
|
||||
|
||||
func clientConfigPath() (string, error) {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
|
||||
41
cmd/mdmctl/config_test.go
Normal file
41
cmd/mdmctl/config_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateServerURL(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "http",
|
||||
input: "http://localhost:8000",
|
||||
expected: "http://localhost:8000/",
|
||||
},
|
||||
{
|
||||
name: "https",
|
||||
input: "https://localhost:8000",
|
||||
expected: "https://localhost:8000/",
|
||||
},
|
||||
{
|
||||
name: "trailing_slash",
|
||||
input: "https://localhost:8000/",
|
||||
expected: "https://localhost:8000/",
|
||||
},
|
||||
{
|
||||
name: "no_prefix",
|
||||
input: "localhost:8000",
|
||||
expected: "https://localhost:8000/",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actualURL, _ := validateServerURL(tt.input)
|
||||
if have, want := actualURL, tt.expected; have != want {
|
||||
t.Errorf("have %s, want %s", have, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user