2018-02-05 21:05:59 +00:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2014-10-03 19:46:42 +00:00
|
|
|
|
2015-01-21 03:37:21 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
2014-10-03 19:46:42 +00:00
|
|
|
|
|
|
|
func TestEndpointParse(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
str string
|
|
|
|
expected string
|
|
|
|
}{
|
2015-07-21 19:40:36 +00:00
|
|
|
{IndexServer, IndexServer},
|
2014-12-12 01:55:15 +00:00
|
|
|
{"http://0.0.0.0:5000/v1/", "http://0.0.0.0:5000/v1/"},
|
2016-03-01 07:07:41 +00:00
|
|
|
{"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/"},
|
|
|
|
{"http://0.0.0.0:5000/nonversion/", "http://0.0.0.0:5000/nonversion/v1/"},
|
|
|
|
{"http://0.0.0.0:5000/v0/", "http://0.0.0.0:5000/v0/v1/"},
|
2014-10-03 19:46:42 +00:00
|
|
|
}
|
|
|
|
for _, td := range testData {
|
2016-03-01 07:07:41 +00:00
|
|
|
e, err := newV1EndpointFromStr(td.str, nil, "", nil)
|
2014-10-03 19:46:42 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 03:37:21 +00:00
|
|
|
|
2016-03-01 07:07:41 +00:00
|
|
|
func TestEndpointParseInvalid(t *testing.T) {
|
|
|
|
testData := []string{
|
|
|
|
"http://0.0.0.0:5000/v2/",
|
|
|
|
}
|
|
|
|
for _, td := range testData {
|
|
|
|
e, err := newV1EndpointFromStr(td, nil, "", nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error parsing %q: parsed as %q", td, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 03:37:21 +00:00
|
|
|
// Ensure that a registry endpoint that responds with a 401 only is determined
|
2016-03-01 07:07:41 +00:00
|
|
|
// to be a valid v1 registry endpoint
|
|
|
|
func TestValidateEndpoint(t *testing.T) {
|
2015-01-21 03:37:21 +00:00
|
|
|
requireBasicAuthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("WWW-Authenticate", `Basic realm="localhost"`)
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make a test server which should validate as a v1 server.
|
|
|
|
testServer := httptest.NewServer(requireBasicAuthHandler)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
testServerURL, err := url.Parse(testServer.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-01 07:07:41 +00:00
|
|
|
testEndpoint := V1Endpoint{
|
|
|
|
URL: testServerURL,
|
2022-02-25 22:58:25 +00:00
|
|
|
client: httpClient(newTransport(nil)),
|
2015-01-21 03:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateEndpoint(&testEndpoint); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-01 07:07:41 +00:00
|
|
|
if testEndpoint.URL.Scheme != "http" {
|
|
|
|
t.Fatalf("expecting to validate endpoint as http, got url %s", testEndpoint.String())
|
2015-01-21 03:37:21 +00:00
|
|
|
}
|
|
|
|
}
|