github.com/pritambaral/docker@v1.4.2-0.20150120174542-b2fe1b3dd952/registry/v2/urls_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  )
     7  
     8  type urlBuilderTestCase struct {
     9  	description string
    10  	expected    string
    11  	build       func() (string, error)
    12  }
    13  
    14  // TestURLBuilder tests the various url building functions, ensuring they are
    15  // returning the expected values.
    16  func TestURLBuilder(t *testing.T) {
    17  
    18  	root := "http://localhost:5000/"
    19  	urlBuilder, err := NewURLBuilderFromString(root)
    20  	if err != nil {
    21  		t.Fatalf("unexpected error creating urlbuilder: %v", err)
    22  	}
    23  
    24  	for _, testcase := range []struct {
    25  		description string
    26  		expected    string
    27  		build       func() (string, error)
    28  	}{
    29  		{
    30  			description: "test base url",
    31  			expected:    "http://localhost:5000/v2/",
    32  			build:       urlBuilder.BuildBaseURL,
    33  		},
    34  		{
    35  			description: "test tags url",
    36  			expected:    "http://localhost:5000/v2/foo/bar/tags/list",
    37  			build: func() (string, error) {
    38  				return urlBuilder.BuildTagsURL("foo/bar")
    39  			},
    40  		},
    41  		{
    42  			description: "test manifest url",
    43  			expected:    "http://localhost:5000/v2/foo/bar/manifests/tag",
    44  			build: func() (string, error) {
    45  				return urlBuilder.BuildManifestURL("foo/bar", "tag")
    46  			},
    47  		},
    48  		{
    49  			description: "build blob url",
    50  			expected:    "http://localhost:5000/v2/foo/bar/blobs/tarsum.v1+sha256:abcdef0123456789",
    51  			build: func() (string, error) {
    52  				return urlBuilder.BuildBlobURL("foo/bar", "tarsum.v1+sha256:abcdef0123456789")
    53  			},
    54  		},
    55  		{
    56  			description: "build blob upload url",
    57  			expected:    "http://localhost:5000/v2/foo/bar/blobs/uploads/",
    58  			build: func() (string, error) {
    59  				return urlBuilder.BuildBlobUploadURL("foo/bar")
    60  			},
    61  		},
    62  		{
    63  			description: "build blob upload url with digest and size",
    64  			expected:    "http://localhost:5000/v2/foo/bar/blobs/uploads/?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
    65  			build: func() (string, error) {
    66  				return urlBuilder.BuildBlobUploadURL("foo/bar", url.Values{
    67  					"size":   []string{"10000"},
    68  					"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
    69  				})
    70  			},
    71  		},
    72  		{
    73  			description: "build blob upload chunk url",
    74  			expected:    "http://localhost:5000/v2/foo/bar/blobs/uploads/uuid-part",
    75  			build: func() (string, error) {
    76  				return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part")
    77  			},
    78  		},
    79  		{
    80  			description: "build blob upload chunk url with digest and size",
    81  			expected:    "http://localhost:5000/v2/foo/bar/blobs/uploads/uuid-part?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
    82  			build: func() (string, error) {
    83  				return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part", url.Values{
    84  					"size":   []string{"10000"},
    85  					"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
    86  				})
    87  			},
    88  		},
    89  	} {
    90  		u, err := testcase.build()
    91  		if err != nil {
    92  			t.Fatalf("%s: error building url: %v", testcase.description, err)
    93  		}
    94  
    95  		if u != testcase.expected {
    96  			t.Fatalf("%s: %q != %q", testcase.description, u, testcase.expected)
    97  		}
    98  	}
    99  
   100  }