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