2014-12-12 20:27:22 +01:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type urlBuilderTestCase struct {
|
2015-01-31 02:26:00 +01:00
|
|
|
description string
|
|
|
|
expectedPath string
|
|
|
|
build func() (string, error)
|
2014-12-12 20:27:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestURLBuilder tests the various url building functions, ensuring they are
|
|
|
|
// returning the expected values.
|
|
|
|
func TestURLBuilder(t *testing.T) {
|
2015-01-31 02:26:00 +01:00
|
|
|
var (
|
|
|
|
urlBuilder *URLBuilder
|
|
|
|
err error
|
|
|
|
)
|
2014-12-12 20:27:22 +01:00
|
|
|
|
2015-01-31 02:26:00 +01:00
|
|
|
testCases := []urlBuilderTestCase{
|
2014-12-12 20:27:22 +01:00
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "test base url",
|
|
|
|
expectedPath: "/v2/",
|
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBaseURL()
|
|
|
|
},
|
2014-12-12 20:27:22 +01:00
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "test tags url",
|
|
|
|
expectedPath: "/v2/foo/bar/tags/list",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildTagsURL("foo/bar")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "test manifest url",
|
|
|
|
expectedPath: "/v2/foo/bar/manifests/tag",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildManifestURL("foo/bar", "tag")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "build blob url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/tarsum.v1+sha256:abcdef0123456789",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBlobURL("foo/bar", "tarsum.v1+sha256:abcdef0123456789")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "build blob upload url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBlobUploadURL("foo/bar")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "build blob upload url with digest and size",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBlobUploadURL("foo/bar", url.Values{
|
|
|
|
"size": []string{"10000"},
|
|
|
|
"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "build blob upload chunk url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 02:26:00 +01:00
|
|
|
description: "build blob upload chunk url with digest and size",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
|
2014-12-12 20:27:22 +01:00
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part", url.Values{
|
|
|
|
"size": []string{"10000"},
|
|
|
|
"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 02:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
roots := []string{
|
|
|
|
"http://example.com",
|
|
|
|
"https://example.com",
|
|
|
|
"http://localhost:5000",
|
|
|
|
"https://localhost:5443",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, root := range roots {
|
|
|
|
urlBuilder, err = NewURLBuilderFromString(root)
|
2014-12-12 20:27:22 +01:00
|
|
|
if err != nil {
|
2015-01-31 02:26:00 +01:00
|
|
|
t.Fatalf("unexpected error creating urlbuilder: %v", err)
|
2014-12-12 20:27:22 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 02:26:00 +01:00
|
|
|
for _, testCase := range testCases {
|
|
|
|
url, err := testCase.build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: error building url: %v", testCase.description, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedURL := root + testCase.expectedPath
|
|
|
|
|
|
|
|
if url != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
|
|
|
|
}
|
2014-12-12 20:27:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|