2018-02-05 16:05:59 -05:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2014-10-03 15:46:42 -04:00
|
|
|
|
2015-01-20 22:37:21 -05:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
2014-10-03 15:46:42 -04:00
|
|
|
|
|
|
|
func TestEndpointParse(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
str string
|
|
|
|
expected string
|
|
|
|
}{
|
2015-07-21 15:40:36 -04:00
|
|
|
{IndexServer, IndexServer},
|
2014-12-11 20:55:15 -05:00
|
|
|
{"http://0.0.0.0:5000/v1/", "http://0.0.0.0:5000/v1/"},
|
2016-03-01 02:07:41 -05: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 15:46:42 -04:00
|
|
|
}
|
|
|
|
for _, td := range testData {
|
2016-03-01 02:07:41 -05:00
|
|
|
e, err := newV1EndpointFromStr(td.str, nil, "", nil)
|
2014-10-03 15:46:42 -04: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-20 22:37:21 -05:00
|
|
|
|
2016-03-01 02:07:41 -05: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-20 22:37:21 -05:00
|
|
|
// Ensure that a registry endpoint that responds with a 401 only is determined
|
2016-03-01 02:07:41 -05:00
|
|
|
// to be a valid v1 registry endpoint
|
|
|
|
func TestValidateEndpoint(t *testing.T) {
|
2015-01-20 22:37:21 -05: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 02:07:41 -05:00
|
|
|
testEndpoint := V1Endpoint{
|
|
|
|
URL: testServerURL,
|
2022-02-25 17:58:25 -05:00
|
|
|
client: httpClient(newTransport(nil)),
|
2015-01-20 22:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateEndpoint(&testEndpoint); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
if testEndpoint.URL.Scheme != "http" {
|
|
|
|
t.Fatalf("expecting to validate endpoint as http, got url %s", testEndpoint.String())
|
2015-01-20 22:37:21 -05:00
|
|
|
}
|
|
|
|
}
|