forked from TrueCloudLab/distribution
Merge pull request #8387 from vbatts/vbatts-registry_test_enpoint
registry/endpoint: make it testable
This commit is contained in:
commit
5685221c5f
2 changed files with 44 additions and 10 deletions
|
@ -35,16 +35,7 @@ func scanForAPIVersion(hostname string) (string, APIVersion) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEndpoint(hostname string) (*Endpoint, error) {
|
func NewEndpoint(hostname string) (*Endpoint, error) {
|
||||||
var (
|
endpoint, err := newEndpoint(hostname)
|
||||||
endpoint Endpoint
|
|
||||||
trimmedHostname string
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if !strings.HasPrefix(hostname, "http") {
|
|
||||||
hostname = "https://" + hostname
|
|
||||||
}
|
|
||||||
trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
|
|
||||||
endpoint.URL, err = url.Parse(trimmedHostname)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -59,6 +50,22 @@ func NewEndpoint(hostname string) (*Endpoint, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return endpoint, nil
|
||||||
|
}
|
||||||
|
func newEndpoint(hostname string) (*Endpoint, error) {
|
||||||
|
var (
|
||||||
|
endpoint Endpoint
|
||||||
|
trimmedHostname string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if !strings.HasPrefix(hostname, "http") {
|
||||||
|
hostname = "https://" + hostname
|
||||||
|
}
|
||||||
|
trimmedHostname, endpoint.Version = scanForAPIVersion(hostname)
|
||||||
|
endpoint.URL, err = url.Parse(trimmedHostname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &endpoint, nil
|
return &endpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
27
docs/endpoint_test.go
Normal file
27
docs/endpoint_test.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package registry
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestEndpointParse(t *testing.T) {
|
||||||
|
testData := []struct {
|
||||||
|
str string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{IndexServerAddress(), IndexServerAddress()},
|
||||||
|
{"http://0.0.0.0:5000", "http://0.0.0.0:5000/v1/"},
|
||||||
|
{"0.0.0.0:5000", "https://0.0.0.0:5000/v1/"},
|
||||||
|
}
|
||||||
|
for _, td := range testData {
|
||||||
|
e, err := newEndpoint(td.str)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%q: %s", td.str, err)
|
||||||
|
}
|
||||||
|
if e == nil {
|
||||||
|
t.Logf("something's fishy, endpoint for %q is nil", td.str)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if e.String() != td.expected {
|
||||||
|
t.Errorf("expected %q, got %q", td.expected, e.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue