Skip to content

Commit

Permalink
add url checking to config validation for address
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsochat@stanford.edu>
  • Loading branch information
vsoch committed Oct 19, 2020
1 parent 8c13b87 commit 099b77b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/asaskevich/govalidator"
"github.com/go-resty/resty/v2"
)

Expand Down Expand Up @@ -42,6 +43,13 @@ func (c *clientConfig) validate() error {
if c.Address == "" {
return fmt.Errorf("Address is required")
}

// Validate the url
isValid := govalidator.IsURL(c.Address)
if !isValid {
return fmt.Errorf("%s is not a valid URL", c.Address)
}

if c.UserAgent == "" {
return fmt.Errorf("UserAgent is required")
}
Expand Down
11 changes: 3 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,10 @@ func TestClient(t *testing.T) {
t.Fatalf("Expected error with missing session id")
}

// bad address on client
badClient, err := NewClient("xwejknxw://jshnws")
if err != nil {
t.Fatalf("Errors creating client with bad address: %s", err)
}
req = badClient.NewRequest(GET, "/v2/<name>/tags/list", WithName("customname"))
resp, err = badClient.Do(req)
// bad address on client should not create
_, err = NewClient("xwejknxw://jshnws")
if err == nil {
t.Fatalf("Expected error with bad address")
t.Fatalf("Should be errors creating client with bad address: %s", err)
}

// Make sure headers and body match after going through auth
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bloodorangeio/reggie
go 1.13

require (
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef
github.com/go-resty/resty/v2 v2.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/opencontainers/distribution-spec v1.0.0-rc0.0.20200108182153-219f20cbcfa1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/go-resty/resty/v2 v2.1.0 h1:Z6IefCpUMfnvItVJaJXWv/pMiiD11So35QgwEELsldE=
github.com/go-resty/resty/v2 v2.1.0/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
Expand Down

0 comments on commit 099b77b

Please sign in to comment.