mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: WithMockClient: match version behavior of actual client
The WithMockClient option was explicitly resetting the client's API version (see [1]), which differs from the regular client, which is initialized with the current API version used by the client (see [2]). This patch: - reduces the `WithMockClient` to only set the custom HTTP client, leaving other fields un-touched. - adds a test utility and updates tests to handle the API-version prefix - removes redundant uses of `WithVersion()` in tests; for most test-cases it was used to make sure a current API version is used that supports the feature being tested, but there was no test to verify the behavior for lower API versions, so we may as well test against "latest". [1]:5a582729d8/client/client_mock_test.go (L22-L36)[2]:5a582729d8/client/client.go (L167-L190)Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -3,10 +3,8 @@ package client
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
@@ -24,14 +22,11 @@ func TestSwarmUpdateError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSwarmUpdate(t *testing.T) {
|
||||
expectedURL := "/swarm/update"
|
||||
const expectedURL = "/swarm/update"
|
||||
|
||||
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
||||
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
||||
}
|
||||
if req.Method != http.MethodPost {
|
||||
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
||||
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
|
||||
Reference in New Issue
Block a user