Skip to content

Commit 30ae936

Browse files
authored
fix: return error on client creation with empty API URL (scaleway#2533)
1 parent bcd7095 commit 30ae936

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

scw/client_option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ func (s *settings) validate() error {
264264

265265
// API URL.
266266
if !validation.IsURL(s.apiURL) {
267-
return NewInvalidClientOptionError("invalid url %s", s.apiURL)
267+
return NewInvalidClientOptionError("invalid API url '%s'", s.apiURL)
268268
}
269269
if s.apiURL[len(s.apiURL)-1:] == "/" {
270-
return NewInvalidClientOptionError("invalid url %s it should not have a trailing slash", s.apiURL)
270+
return NewInvalidClientOptionError("invalid API url '%s' it should not have a trailing slash", s.apiURL)
271271
}
272272

273273
// TODO: check for max s.defaultPageSize

scw/client_option_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ func TestClientOptions(t *testing.T) {
6767
s.apiURL = ":test"
6868
s.token = auth.NewToken(v2ValidAccessKey, v2ValidSecretKey)
6969
},
70-
errStr: "scaleway-sdk-go: invalid url :test",
70+
errStr: "scaleway-sdk-go: invalid API url ':test'",
71+
},
72+
{
73+
name: "Should throw an empty url error",
74+
clientOption: func(s *settings) {
75+
s.apiURL = ""
76+
s.token = auth.NewToken(v2ValidAccessKey, v2ValidSecretKey)
77+
},
78+
errStr: "scaleway-sdk-go: invalid API url ''",
7179
},
7280
{
7381
name: "Should throw an empty organization ID error",

validation/is.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func IsZone(s string) bool {
5151

5252
// IsURL returns true if the given string has a valid URL format.
5353
func IsURL(s string) bool {
54+
if s == "" {
55+
return false
56+
}
57+
5458
_, err := url.Parse(s)
5559
return err == nil
5660
}

0 commit comments

Comments
 (0)