Browse Source

client: TestGetAPIPath(): update test to use more realistic results

This test was setting the non-exported `Client.basePath` directly, however,
it was setting it to a value that would never realistically happen, because
`NewClientWithOpts()` initializes the Client with the default API version:
https://github.com/moby/moby/blob/ea5b4765d9d9a5aa5cab39f7119cffe74be874ce/client/client.go#L119-L130

Which is used by `getAPIPath()` to construct the URL/path:
https://github.com/moby/moby/blob/ea5b4765d9d9a5aa5cab39f7119cffe74be874ce/client/client.go#L176-L190

While this didn't render the test "invalid", using a Client that's constructed
in the usual way, makes it more representative.

Given that we deprecated (but still support) the non-versioned API paths, with
the exception of the `/_ping` API endpoint, we should probably change `getAPIPath()`
to default to the "current version", instead of allowing it to use an empty string.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 years ago
parent
commit
25a336ab6a
1 changed files with 8 additions and 7 deletions
  1. 8 7
      client/client_test.go

+ 8 - 7
client/client_test.go

@@ -113,9 +113,9 @@ func TestGetAPIPath(t *testing.T) {
 		query    url.Values
 		expected string
 	}{
-		{"", "/containers/json", nil, "/containers/json"},
-		{"", "/containers/json", url.Values{}, "/containers/json"},
-		{"", "/containers/json", url.Values{"s": []string{"c"}}, "/containers/json?s=c"},
+		{"", "/containers/json", nil, "/v" + api.DefaultVersion + "/containers/json"},
+		{"", "/containers/json", url.Values{}, "/v" + api.DefaultVersion + "/containers/json"},
+		{"", "/containers/json", url.Values{"s": []string{"c"}}, "/v" + api.DefaultVersion + "/containers/json?s=c"},
 		{"1.22", "/containers/json", nil, "/v1.22/containers/json"},
 		{"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
 		{"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
@@ -127,10 +127,11 @@ func TestGetAPIPath(t *testing.T) {
 
 	ctx := context.TODO()
 	for _, tc := range testcases {
-		client := Client{
-			version:  tc.version,
-			basePath: "/",
-		}
+		client, err := NewClientWithOpts(
+			WithVersion(tc.version),
+			WithHost("tcp://localhost:2375"),
+		)
+		assert.NilError(t, err)
 		actual := client.getAPIPath(ctx, tc.path, tc.query)
 		assert.Check(t, is.Equal(actual, tc.expected))
 	}