2015-02-11 02:32:22 +01:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2015-12-15 23:35:23 +01:00
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
2015-02-11 02:32:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type urlBuilderTestCase struct {
|
|
|
|
description string
|
|
|
|
expectedPath string
|
|
|
|
build func() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeURLBuilderTestCases(urlBuilder *URLBuilder) []urlBuilderTestCase {
|
2015-12-15 23:35:23 +01:00
|
|
|
fooBarRef, _ := reference.ParseNamed("foo/bar")
|
2015-02-11 02:32:22 +01:00
|
|
|
return []urlBuilderTestCase{
|
|
|
|
{
|
|
|
|
description: "test base url",
|
|
|
|
expectedPath: "/v2/",
|
|
|
|
build: urlBuilder.BuildBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "test tags url",
|
|
|
|
expectedPath: "/v2/foo/bar/tags/list",
|
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildTagsURL(fooBarRef)
|
2015-02-11 02:32:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "test manifest url",
|
|
|
|
expectedPath: "/v2/foo/bar/manifests/tag",
|
|
|
|
build: func() (string, error) {
|
2015-12-16 01:43:13 +01:00
|
|
|
ref, _ := reference.WithTag(fooBarRef, "tag")
|
|
|
|
return urlBuilder.BuildManifestURL(ref)
|
2015-02-11 02:32:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "build blob url",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
|
2015-02-11 02:32:22 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-16 01:43:13 +01:00
|
|
|
ref, _ := reference.WithDigest(fooBarRef, "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5")
|
|
|
|
return urlBuilder.BuildBlobURL(ref)
|
2015-02-11 02:32:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "build blob upload url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/",
|
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadURL(fooBarRef)
|
2015-02-11 02:32:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "build blob upload url with digest and size",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/?digest=sha256%3A3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5&size=10000",
|
2015-02-11 02:32:22 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadURL(fooBarRef, url.Values{
|
2015-02-11 02:32:22 +01:00
|
|
|
"size": []string{"10000"},
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": []string{"sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"},
|
2015-02-11 02:32:22 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "build blob upload chunk url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part",
|
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadChunkURL(fooBarRef, "uuid-part")
|
2015-02-11 02:32:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "build blob upload chunk url with digest and size",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part?digest=sha256%3A3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5&size=10000",
|
2015-02-11 02:32:22 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadChunkURL(fooBarRef, "uuid-part", url.Values{
|
2015-02-11 02:32:22 +01:00
|
|
|
"size": []string{"10000"},
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": []string{"sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"},
|
2015-02-11 02:32:22 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestURLBuilder tests the various url building functions, ensuring they are
|
|
|
|
// returning the expected values.
|
|
|
|
func TestURLBuilder(t *testing.T) {
|
|
|
|
roots := []string{
|
|
|
|
"http://example.com",
|
|
|
|
"https://example.com",
|
|
|
|
"http://localhost:5000",
|
|
|
|
"https://localhost:5443",
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, root := range roots {
|
|
|
|
urlBuilder, err := NewURLBuilderFromString(root, relative)
|
2015-02-11 02:32:22 +01:00
|
|
|
if err != nil {
|
2016-02-23 02:49:23 +01:00
|
|
|
t.Fatalf("unexpected error creating urlbuilder: %v", err)
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
|
|
|
|
url, err := testCase.build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: error building url: %v", testCase.description, err)
|
|
|
|
}
|
|
|
|
expectedURL := testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = root + expectedURL
|
|
|
|
}
|
2015-02-11 02:32:22 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
if url != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
|
|
|
|
}
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 23:59:01 +01:00
|
|
|
func TestURLBuilderWithPrefix(t *testing.T) {
|
|
|
|
roots := []string{
|
|
|
|
"http://example.com/prefix/",
|
|
|
|
"https://example.com/prefix/",
|
|
|
|
"http://localhost:5000/prefix/",
|
|
|
|
"https://localhost:5443/prefix/",
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, root := range roots {
|
|
|
|
urlBuilder, err := NewURLBuilderFromString(root, relative)
|
2015-02-24 23:59:01 +01:00
|
|
|
if err != nil {
|
2016-02-23 02:49:23 +01:00
|
|
|
t.Fatalf("unexpected error creating urlbuilder: %v", err)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
|
|
|
|
url, err := testCase.build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: error building url: %v", testCase.description, err)
|
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL := testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = root[0:len(root)-1] + expectedURL
|
|
|
|
}
|
|
|
|
if url != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
|
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 02:32:22 +01:00
|
|
|
type builderFromRequestTestCase struct {
|
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderFromRequest(t *testing.T) {
|
|
|
|
u, err := url.Parse("http://example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardedProtoHeader := make(http.Header, 1)
|
|
|
|
forwardedProtoHeader.Set("X-Forwarded-Proto", "https")
|
|
|
|
|
2015-04-24 23:04:48 +02:00
|
|
|
forwardedHostHeader1 := make(http.Header, 1)
|
|
|
|
forwardedHostHeader1.Set("X-Forwarded-Host", "first.example.com")
|
|
|
|
|
|
|
|
forwardedHostHeader2 := make(http.Header, 1)
|
|
|
|
forwardedHostHeader2.Set("X-Forwarded-Host", "first.example.com, proxy1.example.com")
|
|
|
|
|
2015-02-11 02:32:22 +01:00
|
|
|
testRequests := []struct {
|
2015-09-18 20:03:15 +02:00
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
configHost url.URL
|
2015-02-11 02:32:22 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host},
|
|
|
|
base: "http://example.com",
|
|
|
|
},
|
2015-10-15 10:56:28 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "http://example.com",
|
|
|
|
},
|
2015-02-11 02:32:22 +01:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "https://example.com",
|
|
|
|
},
|
2015-04-24 23:04:48 +02:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedHostHeader1},
|
|
|
|
base: "http://first.example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedHostHeader2},
|
|
|
|
base: "http://first.example.com",
|
|
|
|
},
|
2015-09-18 20:03:15 +02:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedHostHeader2},
|
|
|
|
base: "https://third.example.com:5000",
|
|
|
|
configHost: url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "third.example.com:5000",
|
|
|
|
},
|
|
|
|
},
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, tr := range testRequests {
|
|
|
|
var builder *URLBuilder
|
|
|
|
if tr.configHost.Scheme != "" && tr.configHost.Host != "" {
|
|
|
|
builder = NewURLBuilder(&tr.configHost, relative)
|
|
|
|
} else {
|
|
|
|
builder = NewURLBuilderFromRequest(tr.request, relative)
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(builder) {
|
|
|
|
buildURL, err := testCase.build()
|
2015-10-15 10:56:28 +02:00
|
|
|
if err != nil {
|
2016-02-23 02:49:23 +01:00
|
|
|
t.Fatalf("%s: error building url: %v", testCase.description, err)
|
2015-10-15 10:56:28 +02:00
|
|
|
}
|
2015-02-11 02:32:22 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
var expectedURL string
|
|
|
|
proto, ok := tr.request.Header["X-Forwarded-Proto"]
|
|
|
|
if !ok {
|
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = tr.base + expectedURL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
urlBase, err := url.Parse(tr.base)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
urlBase.Scheme = proto[0]
|
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = urlBase.String() + expectedURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildURL != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
|
|
|
|
}
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2015-02-11 02:32:22 +01:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
|
|
|
func TestBuilderFromRequestWithPrefix(t *testing.T) {
|
|
|
|
u, err := url.Parse("http://example.com/prefix/v2/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardedProtoHeader := make(http.Header, 1)
|
|
|
|
forwardedProtoHeader.Set("X-Forwarded-Proto", "https")
|
|
|
|
|
|
|
|
testRequests := []struct {
|
2015-09-18 20:03:15 +02:00
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
configHost url.URL
|
2015-02-24 23:59:01 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host},
|
|
|
|
base: "http://example.com/prefix/",
|
|
|
|
},
|
2015-10-15 10:56:28 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "http://example.com/prefix/",
|
|
|
|
},
|
2015-02-24 23:59:01 +01:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "https://example.com/prefix/",
|
|
|
|
},
|
2015-09-18 20:03:15 +02:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "https://subdomain.example.com/prefix/",
|
|
|
|
configHost: url.URL{
|
|
|
|
Scheme: "https",
|
2015-10-25 18:01:45 +01:00
|
|
|
Host: "subdomain.example.com",
|
|
|
|
Path: "/prefix/",
|
2015-09-18 20:03:15 +02:00
|
|
|
},
|
|
|
|
},
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
var relative bool
|
2015-02-24 23:59:01 +01:00
|
|
|
for _, tr := range testRequests {
|
2015-09-18 20:03:15 +02:00
|
|
|
var builder *URLBuilder
|
|
|
|
if tr.configHost.Scheme != "" && tr.configHost.Host != "" {
|
2016-02-23 02:49:23 +01:00
|
|
|
builder = NewURLBuilder(&tr.configHost, false)
|
2015-09-18 20:03:15 +02:00
|
|
|
} else {
|
2016-02-23 02:49:23 +01:00
|
|
|
builder = NewURLBuilderFromRequest(tr.request, false)
|
2015-09-18 20:03:15 +02:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
|
|
|
for _, testCase := range makeURLBuilderTestCases(builder) {
|
2015-10-15 10:56:28 +02:00
|
|
|
buildURL, err := testCase.build()
|
2015-02-24 23:59:01 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: error building url: %v", testCase.description, err)
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
var expectedURL string
|
|
|
|
proto, ok := tr.request.Header["X-Forwarded-Proto"]
|
|
|
|
if !ok {
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = tr.base[0:len(tr.base)-1] + expectedURL
|
|
|
|
}
|
2015-10-15 10:56:28 +02:00
|
|
|
} else {
|
|
|
|
urlBase, err := url.Parse(tr.base)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
urlBase.Scheme = proto[0]
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = urlBase.String()[0:len(urlBase.String())-1] + expectedURL
|
|
|
|
}
|
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
if buildURL != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|