github.com/christopherobin/docker@v1.6.2/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  	expectedPath 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  	var (
    18  		urlBuilder *URLBuilder
    19  		err        error
    20  	)
    21  
    22  	testCases := []urlBuilderTestCase{
    23  		{
    24  			description:  "test base url",
    25  			expectedPath: "/v2/",
    26  			build: func() (string, error) {
    27  				return urlBuilder.BuildBaseURL()
    28  			},
    29  		},
    30  		{
    31  			description:  "test tags url",
    32  			expectedPath: "/v2/foo/bar/tags/list",
    33  			build: func() (string, error) {
    34  				return urlBuilder.BuildTagsURL("foo/bar")
    35  			},
    36  		},
    37  		{
    38  			description:  "test manifest url",
    39  			expectedPath: "/v2/foo/bar/manifests/tag",
    40  			build: func() (string, error) {
    41  				return urlBuilder.BuildManifestURL("foo/bar", "tag")
    42  			},
    43  		},
    44  		{
    45  			description:  "build blob url",
    46  			expectedPath: "/v2/foo/bar/blobs/tarsum.v1+sha256:abcdef0123456789",
    47  			build: func() (string, error) {
    48  				return urlBuilder.BuildBlobURL("foo/bar", "tarsum.v1+sha256:abcdef0123456789")
    49  			},
    50  		},
    51  		{
    52  			description:  "build blob upload url",
    53  			expectedPath: "/v2/foo/bar/blobs/uploads/",
    54  			build: func() (string, error) {
    55  				return urlBuilder.BuildBlobUploadURL("foo/bar")
    56  			},
    57  		},
    58  		{
    59  			description:  "build blob upload url with digest and size",
    60  			expectedPath: "/v2/foo/bar/blobs/uploads/?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
    61  			build: func() (string, error) {
    62  				return urlBuilder.BuildBlobUploadURL("foo/bar", url.Values{
    63  					"size":   []string{"10000"},
    64  					"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
    65  				})
    66  			},
    67  		},
    68  		{
    69  			description:  "build blob upload chunk url",
    70  			expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part",
    71  			build: func() (string, error) {
    72  				return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part")
    73  			},
    74  		},
    75  		{
    76  			description:  "build blob upload chunk url with digest and size",
    77  			expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part?digest=tarsum.v1%2Bsha256%3Aabcdef0123456789&size=10000",
    78  			build: func() (string, error) {
    79  				return urlBuilder.BuildBlobUploadChunkURL("foo/bar", "uuid-part", url.Values{
    80  					"size":   []string{"10000"},
    81  					"digest": []string{"tarsum.v1+sha256:abcdef0123456789"},
    82  				})
    83  			},
    84  		},
    85  	}
    86  
    87  	roots := []string{
    88  		"http://example.com",
    89  		"https://example.com",
    90  		"http://localhost:5000",
    91  		"https://localhost:5443",
    92  	}
    93  
    94  	for _, root := range roots {
    95  		urlBuilder, err = NewURLBuilderFromString(root)
    96  		if err != nil {
    97  			t.Fatalf("unexpected error creating urlbuilder: %v", err)
    98  		}
    99  
   100  		for _, testCase := range testCases {
   101  			url, err := testCase.build()
   102  			if err != nil {
   103  				t.Fatalf("%s: error building url: %v", testCase.description, err)
   104  			}
   105  
   106  			expectedURL := root + testCase.expectedPath
   107  
   108  			if url != expectedURL {
   109  				t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
   110  			}
   111  		}
   112  	}
   113  }