2014-10-03 15:46:42 -04:00
|
|
|
package registry
|
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{IndexServerAddress(), IndexServerAddress()},
|
2014-12-11 20:55:15 -05:00
|
|
|
{"http://0.0.0.0:5000/v1/", "http://0.0.0.0:5000/v1/"},
|
|
|
|
{"http://0.0.0.0:5000/v2/", "http://0.0.0.0:5000/v2/"},
|
|
|
|
{"http://0.0.0.0:5000", "http://0.0.0.0:5000/v0/"},
|
|
|
|
{"0.0.0.0:5000", "https://0.0.0.0:5000/v0/"},
|
2014-10-03 15:46:42 -04:00
|
|
|
}
|
|
|
|
for _, td := range testData {
|
2015-05-15 21:35:04 -04:00
|
|
|
e, err := newEndpoint(td.str, false, 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
|
|
|
|
|
|
|
// Ensure that a registry endpoint that responds with a 401 only is determined
|
|
|
|
// to be a v1 registry unless it includes a valid v2 API header.
|
|
|
|
func TestValidateEndpointAmbiguousAPIVersion(t *testing.T) {
|
|
|
|
requireBasicAuthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("WWW-Authenticate", `Basic realm="localhost"`)
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
})
|
|
|
|
|
|
|
|
requireBasicAuthHandlerV2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-01-21 15:11:53 -05:00
|
|
|
// This mock server supports v2.0, v2.1, v42.0, and v100.0
|
|
|
|
w.Header().Add("Docker-Distribution-API-Version", "registry/100.0 registry/42.0")
|
|
|
|
w.Header().Add("Docker-Distribution-API-Version", "registry/2.0 registry/2.1")
|
2015-01-20 22:37:21 -05:00
|
|
|
requireBasicAuthHandler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
testEndpoint := Endpoint{
|
|
|
|
URL: testServerURL,
|
|
|
|
Version: APIVersionUnknown,
|
2015-05-15 21:35:04 -04:00
|
|
|
client: HTTPClient(NewTransport(ConnectTimeout, false)),
|
2015-01-20 22:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateEndpoint(&testEndpoint); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testEndpoint.Version != APIVersion1 {
|
2015-02-02 17:53:20 -05:00
|
|
|
t.Fatalf("expected endpoint to validate to %d, got %d", APIVersion1, testEndpoint.Version)
|
2015-01-20 22:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a test server which should validate as a v2 server.
|
|
|
|
testServer = httptest.NewServer(requireBasicAuthHandlerV2)
|
|
|
|
defer testServer.Close()
|
|
|
|
|
|
|
|
testServerURL, err = url.Parse(testServer.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testEndpoint.URL = testServerURL
|
|
|
|
testEndpoint.Version = APIVersionUnknown
|
|
|
|
|
|
|
|
if err = validateEndpoint(&testEndpoint); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testEndpoint.Version != APIVersion2 {
|
2015-02-02 17:53:20 -05:00
|
|
|
t.Fatalf("expected endpoint to validate to %d, got %d", APIVersion2, testEndpoint.Version)
|
2015-01-20 22:37:21 -05:00
|
|
|
}
|
|
|
|
}
|