Browse code

client/network_test: Use functional option to create mock client

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/08/29 22:17:22
Showing 7 changed files
... ...
@@ -17,11 +17,10 @@ import (
17 17
 )
18 18
 
19 19
 func TestNetworkConnectError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
23 22
 
24
-	err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
23
+	err = client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
25 24
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
26 25
 
27 26
 	// Empty network ID or container ID
... ...
@@ -37,78 +36,76 @@ func TestNetworkConnectError(t *testing.T) {
37 37
 func TestNetworkConnectEmptyNilEndpointSettings(t *testing.T) {
38 38
 	expectedURL := "/networks/network_id/connect"
39 39
 
40
-	client := &Client{
41
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
42
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
43
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
44
-			}
45
-
46
-			if req.Method != http.MethodPost {
47
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
48
-			}
49
-
50
-			var connect NetworkConnectOptions
51
-			if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
52
-				return nil, err
53
-			}
54
-
55
-			if connect.Container != "container_id" {
56
-				return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
57
-			}
58
-
59
-			if connect.EndpointConfig != nil {
60
-				return nil, fmt.Errorf("expected connect.EndpointConfig to be nil, got %v", connect.EndpointConfig)
61
-			}
62
-
63
-			return &http.Response{
64
-				StatusCode: http.StatusOK,
65
-				Body:       io.NopCloser(bytes.NewReader([]byte(""))),
66
-			}, nil
67
-		}),
68
-	}
69
-
70
-	err := client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
40
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
41
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
42
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
43
+		}
44
+
45
+		if req.Method != http.MethodPost {
46
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
47
+		}
48
+
49
+		var connect NetworkConnectOptions
50
+		if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
51
+			return nil, err
52
+		}
53
+
54
+		if connect.Container != "container_id" {
55
+			return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
56
+		}
57
+
58
+		if connect.EndpointConfig != nil {
59
+			return nil, fmt.Errorf("expected connect.EndpointConfig to be nil, got %v", connect.EndpointConfig)
60
+		}
61
+
62
+		return &http.Response{
63
+			StatusCode: http.StatusOK,
64
+			Body:       io.NopCloser(bytes.NewReader([]byte(""))),
65
+		}, nil
66
+	}))
67
+	assert.NilError(t, err)
68
+
69
+	err = client.NetworkConnect(context.Background(), "network_id", "container_id", nil)
71 70
 	assert.NilError(t, err)
72 71
 }
73 72
 
74 73
 func TestNetworkConnect(t *testing.T) {
75 74
 	expectedURL := "/networks/network_id/connect"
76 75
 
77
-	client := &Client{
78
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
79
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
80
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
81
-			}
82
-
83
-			if req.Method != http.MethodPost {
84
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
85
-			}
86
-
87
-			var connect NetworkConnectOptions
88
-			if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
89
-				return nil, err
90
-			}
91
-
92
-			if connect.Container != "container_id" {
93
-				return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
94
-			}
95
-
96
-			if connect.EndpointConfig == nil {
97
-				return nil, fmt.Errorf("expected connect.EndpointConfig to be not nil, got %v", connect.EndpointConfig)
98
-			}
99
-
100
-			if connect.EndpointConfig.NetworkID != "NetworkID" {
101
-				return nil, fmt.Errorf("expected 'NetworkID', got %s", connect.EndpointConfig.NetworkID)
102
-			}
103
-
104
-			return &http.Response{
105
-				StatusCode: http.StatusOK,
106
-				Body:       io.NopCloser(bytes.NewReader([]byte(""))),
107
-			}, nil
108
-		}),
109
-	}
110
-
111
-	err := client.NetworkConnect(context.Background(), "network_id", "container_id", &network.EndpointSettings{
76
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
77
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
78
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
79
+		}
80
+
81
+		if req.Method != http.MethodPost {
82
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
83
+		}
84
+
85
+		var connect NetworkConnectOptions
86
+		if err := json.NewDecoder(req.Body).Decode(&connect); err != nil {
87
+			return nil, err
88
+		}
89
+
90
+		if connect.Container != "container_id" {
91
+			return nil, fmt.Errorf("expected 'container_id', got %s", connect.Container)
92
+		}
93
+
94
+		if connect.EndpointConfig == nil {
95
+			return nil, fmt.Errorf("expected connect.EndpointConfig to be not nil, got %v", connect.EndpointConfig)
96
+		}
97
+
98
+		if connect.EndpointConfig.NetworkID != "NetworkID" {
99
+			return nil, fmt.Errorf("expected 'NetworkID', got %s", connect.EndpointConfig.NetworkID)
100
+		}
101
+
102
+		return &http.Response{
103
+			StatusCode: http.StatusOK,
104
+			Body:       io.NopCloser(bytes.NewReader([]byte(""))),
105
+		}, nil
106
+	}))
107
+	assert.NilError(t, err)
108
+
109
+	err = client.NetworkConnect(context.Background(), "network_id", "container_id", &network.EndpointSettings{
112 110
 		NetworkID: "NetworkID",
113 111
 	})
114 112
 	assert.NilError(t, err)
... ...
@@ -17,11 +17,10 @@ import (
17 17
 )
18 18
 
19 19
 func TestNetworkCreateError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
23 22
 
24
-	_, err := client.NetworkCreate(context.Background(), "mynetwork", NetworkCreateOptions{})
23
+	_, err = client.NetworkCreate(context.Background(), "mynetwork", NetworkCreateOptions{})
25 24
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
26 25
 }
27 26
 
... ...
@@ -40,29 +39,28 @@ func TestNetworkCreateConnectionError(t *testing.T) {
40 40
 func TestNetworkCreate(t *testing.T) {
41 41
 	expectedURL := "/networks/create"
42 42
 
43
-	client := &Client{
44
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
45
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
46
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
47
-			}
43
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
44
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
45
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
46
+		}
48 47
 
49
-			if req.Method != http.MethodPost {
50
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
51
-			}
48
+		if req.Method != http.MethodPost {
49
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
50
+		}
52 51
 
53
-			content, err := json.Marshal(network.CreateResponse{
54
-				ID:      "network_id",
55
-				Warning: "warning",
56
-			})
57
-			if err != nil {
58
-				return nil, err
59
-			}
60
-			return &http.Response{
61
-				StatusCode: http.StatusOK,
62
-				Body:       io.NopCloser(bytes.NewReader(content)),
63
-			}, nil
64
-		}),
65
-	}
52
+		content, err := json.Marshal(network.CreateResponse{
53
+			ID:      "network_id",
54
+			Warning: "warning",
55
+		})
56
+		if err != nil {
57
+			return nil, err
58
+		}
59
+		return &http.Response{
60
+			StatusCode: http.StatusOK,
61
+			Body:       io.NopCloser(bytes.NewReader(content)),
62
+		}, nil
63
+	}))
64
+	assert.NilError(t, err)
66 65
 
67 66
 	enableIPv6 := true
68 67
 	networkResponse, err := client.NetworkCreate(context.Background(), "mynetwork", NetworkCreateOptions{
... ...
@@ -16,11 +16,10 @@ import (
16 16
 )
17 17
 
18 18
 func TestNetworkDisconnectError(t *testing.T) {
19
-	client := &Client{
20
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
21
-	}
19
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
20
+	assert.NilError(t, err)
22 21
 
23
-	err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", false)
22
+	err = client.NetworkDisconnect(context.Background(), "network_id", "container_id", false)
24 23
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
25 24
 
26 25
 	// Empty network ID or container ID
... ...
@@ -36,36 +35,35 @@ func TestNetworkDisconnectError(t *testing.T) {
36 36
 func TestNetworkDisconnect(t *testing.T) {
37 37
 	expectedURL := "/networks/network_id/disconnect"
38 38
 
39
-	client := &Client{
40
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
41
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
42
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
43
-			}
39
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
40
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
41
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
42
+		}
44 43
 
45
-			if req.Method != http.MethodPost {
46
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
47
-			}
44
+		if req.Method != http.MethodPost {
45
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
46
+		}
48 47
 
49
-			var disconnect NetworkDisconnectOptions
50
-			if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
51
-				return nil, err
52
-			}
48
+		var disconnect NetworkDisconnectOptions
49
+		if err := json.NewDecoder(req.Body).Decode(&disconnect); err != nil {
50
+			return nil, err
51
+		}
53 52
 
54
-			if disconnect.Container != "container_id" {
55
-				return nil, fmt.Errorf("expected 'container_id', got %s", disconnect.Container)
56
-			}
53
+		if disconnect.Container != "container_id" {
54
+			return nil, fmt.Errorf("expected 'container_id', got %s", disconnect.Container)
55
+		}
57 56
 
58
-			if !disconnect.Force {
59
-				return nil, fmt.Errorf("expected Force to be true, got %v", disconnect.Force)
60
-			}
57
+		if !disconnect.Force {
58
+			return nil, fmt.Errorf("expected Force to be true, got %v", disconnect.Force)
59
+		}
61 60
 
62
-			return &http.Response{
63
-				StatusCode: http.StatusOK,
64
-				Body:       io.NopCloser(bytes.NewReader([]byte(""))),
65
-			}, nil
66
-		}),
67
-	}
61
+		return &http.Response{
62
+			StatusCode: http.StatusOK,
63
+			Body:       io.NopCloser(bytes.NewReader([]byte(""))),
64
+		}, nil
65
+	}))
66
+	assert.NilError(t, err)
68 67
 
69
-	err := client.NetworkDisconnect(context.Background(), "network_id", "container_id", true)
68
+	err = client.NetworkDisconnect(context.Background(), "network_id", "container_id", true)
70 69
 	assert.NilError(t, err)
71 70
 }
... ...
@@ -17,54 +17,53 @@ import (
17 17
 )
18 18
 
19 19
 func TestNetworkInspect(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
22
-			if req.Method != http.MethodGet {
23
-				return nil, errors.New("expected GET method, got " + req.Method)
20
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
21
+		if req.Method != http.MethodGet {
22
+			return nil, errors.New("expected GET method, got " + req.Method)
23
+		}
24
+		if req.URL.Path == "/networks/" {
25
+			return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
26
+		}
27
+		if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
28
+			return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
29
+		}
30
+		if strings.HasPrefix(req.URL.Path, "/networks/test-500-response") {
31
+			return errorMock(http.StatusInternalServerError, "Server error")(req)
32
+		}
33
+		// other test-cases all use "network_id"
34
+		if !strings.HasPrefix(req.URL.Path, "/networks/network_id") {
35
+			return nil, errors.New("expected URL '/networks/network_id', got " + req.URL.Path)
36
+		}
37
+		if strings.Contains(req.URL.RawQuery, "scope=global") {
38
+			return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
39
+		}
40
+		var (
41
+			content []byte
42
+			err     error
43
+		)
44
+		if strings.Contains(req.URL.RawQuery, "verbose=true") {
45
+			s := map[string]network.ServiceInfo{
46
+				"web": {},
24 47
 			}
25
-			if req.URL.Path == "/networks/" {
26
-				return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
27
-			}
28
-			if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
29
-				return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
30
-			}
31
-			if strings.HasPrefix(req.URL.Path, "/networks/test-500-response") {
32
-				return errorMock(http.StatusInternalServerError, "Server error")(req)
33
-			}
34
-			// other test-cases all use "network_id"
35
-			if !strings.HasPrefix(req.URL.Path, "/networks/network_id") {
36
-				return nil, errors.New("expected URL '/networks/network_id', got " + req.URL.Path)
37
-			}
38
-			if strings.Contains(req.URL.RawQuery, "scope=global") {
39
-				return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
40
-			}
41
-			var (
42
-				content []byte
43
-				err     error
44
-			)
45
-			if strings.Contains(req.URL.RawQuery, "verbose=true") {
46
-				s := map[string]network.ServiceInfo{
47
-					"web": {},
48
-				}
49
-				content, err = json.Marshal(network.Inspect{
50
-					Name:     "mynetwork",
51
-					Services: s,
52
-				})
53
-			} else {
54
-				content, err = json.Marshal(network.Inspect{
55
-					Name: "mynetwork",
56
-				})
57
-			}
58
-			if err != nil {
59
-				return nil, err
60
-			}
61
-			return &http.Response{
62
-				Header:     http.Header{"Content-Type": []string{"application/json"}},
63
-				StatusCode: http.StatusOK,
64
-				Body:       io.NopCloser(bytes.NewReader(content)),
65
-			}, nil
66
-		}),
67
-	}
48
+			content, err = json.Marshal(network.Inspect{
49
+				Name:     "mynetwork",
50
+				Services: s,
51
+			})
52
+		} else {
53
+			content, err = json.Marshal(network.Inspect{
54
+				Name: "mynetwork",
55
+			})
56
+		}
57
+		if err != nil {
58
+			return nil, err
59
+		}
60
+		return &http.Response{
61
+			Header:     http.Header{"Content-Type": []string{"application/json"}},
62
+			StatusCode: http.StatusOK,
63
+			Body:       io.NopCloser(bytes.NewReader(content)),
64
+		}, nil
65
+	}))
66
+	assert.NilError(t, err)
68 67
 
69 68
 	t.Run("empty ID", func(t *testing.T) {
70 69
 		// verify that the client does not create a request if the network-ID/name is empty.
... ...
@@ -18,11 +18,10 @@ import (
18 18
 )
19 19
 
20 20
 func TestNetworkListError(t *testing.T) {
21
-	client := &Client{
22
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23
-	}
21
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
22
+	assert.NilError(t, err)
24 23
 
25
-	_, err := client.NetworkList(context.Background(), NetworkListOptions{})
24
+	_, err = client.NetworkList(context.Background(), NetworkListOptions{})
26 25
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
27 26
 }
28 27
 
... ...
@@ -61,34 +60,33 @@ func TestNetworkList(t *testing.T) {
61 61
 	}
62 62
 
63 63
 	for _, listCase := range listCases {
64
-		client := &Client{
65
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
66
-				if !strings.HasPrefix(req.URL.Path, expectedURL) {
67
-					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
68
-				}
69
-				if req.Method != http.MethodGet {
70
-					return nil, fmt.Errorf("expected GET method, got %s", req.Method)
71
-				}
72
-				query := req.URL.Query()
73
-				actualFilters := query.Get("filters")
74
-				if actualFilters != listCase.expectedFilters {
75
-					return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
76
-				}
77
-				content, err := json.Marshal([]network.Summary{
78
-					{
79
-						Name:   "network",
80
-						Driver: "bridge",
81
-					},
82
-				})
83
-				if err != nil {
84
-					return nil, err
85
-				}
86
-				return &http.Response{
87
-					StatusCode: http.StatusOK,
88
-					Body:       io.NopCloser(bytes.NewReader(content)),
89
-				}, nil
90
-			}),
91
-		}
64
+		client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
65
+			if !strings.HasPrefix(req.URL.Path, expectedURL) {
66
+				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
67
+			}
68
+			if req.Method != http.MethodGet {
69
+				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
70
+			}
71
+			query := req.URL.Query()
72
+			actualFilters := query.Get("filters")
73
+			if actualFilters != listCase.expectedFilters {
74
+				return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
75
+			}
76
+			content, err := json.Marshal([]network.Summary{
77
+				{
78
+					Name:   "network",
79
+					Driver: "bridge",
80
+				},
81
+			})
82
+			if err != nil {
83
+				return nil, err
84
+			}
85
+			return &http.Response{
86
+				StatusCode: http.StatusOK,
87
+				Body:       io.NopCloser(bytes.NewReader(content)),
88
+			}, nil
89
+		}))
90
+		assert.NilError(t, err)
92 91
 
93 92
 		networkResources, err := client.NetworkList(context.Background(), listCase.options)
94 93
 		assert.NilError(t, err)
... ...
@@ -18,12 +18,13 @@ import (
18 18
 )
19 19
 
20 20
 func TestNetworksPruneError(t *testing.T) {
21
-	client := &Client{
22
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23
-		version: "1.25",
24
-	}
21
+	client, err := NewClientWithOpts(
22
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23
+		WithVersion("1.25"),
24
+	)
25
+	assert.NilError(t, err)
25 26
 
26
-	_, err := client.NetworksPrune(context.Background(), filters.NewArgs())
27
+	_, err = client.NetworksPrune(context.Background(), filters.NewArgs())
27 28
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
28 29
 }
29 30
 
... ...
@@ -72,8 +73,8 @@ func TestNetworksPrune(t *testing.T) {
72 72
 		},
73 73
 	}
74 74
 	for _, listCase := range listCases {
75
-		client := &Client{
76
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
75
+		client, err := NewClientWithOpts(
76
+			WithMockClient(func(req *http.Request) (*http.Response, error) {
77 77
 				if !strings.HasPrefix(req.URL.Path, expectedURL) {
78 78
 					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
79 79
 				}
... ...
@@ -93,8 +94,9 @@ func TestNetworksPrune(t *testing.T) {
93 93
 					Body:       io.NopCloser(bytes.NewReader(content)),
94 94
 				}, nil
95 95
 			}),
96
-			version: "1.25",
97
-		}
96
+			WithVersion("1.25"),
97
+		)
98
+		assert.NilError(t, err)
98 99
 
99 100
 		report, err := client.NetworksPrune(context.Background(), listCase.filters)
100 101
 		assert.NilError(t, err)
... ...
@@ -15,11 +15,10 @@ import (
15 15
 )
16 16
 
17 17
 func TestNetworkRemoveError(t *testing.T) {
18
-	client := &Client{
19
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
20
-	}
18
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
19
+	assert.NilError(t, err)
21 20
 
22
-	err := client.NetworkRemove(context.Background(), "network_id")
21
+	err = client.NetworkRemove(context.Background(), "network_id")
23 22
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
24 23
 
25 24
 	err = client.NetworkRemove(context.Background(), "")
... ...
@@ -34,21 +33,20 @@ func TestNetworkRemoveError(t *testing.T) {
34 34
 func TestNetworkRemove(t *testing.T) {
35 35
 	expectedURL := "/networks/network_id"
36 36
 
37
-	client := &Client{
38
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
39
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
40
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
41
-			}
42
-			if req.Method != http.MethodDelete {
43
-				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
44
-			}
45
-			return &http.Response{
46
-				StatusCode: http.StatusOK,
47
-				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
48
-			}, nil
49
-		}),
50
-	}
51
-
52
-	err := client.NetworkRemove(context.Background(), "network_id")
37
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
38
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
39
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
40
+		}
41
+		if req.Method != http.MethodDelete {
42
+			return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
43
+		}
44
+		return &http.Response{
45
+			StatusCode: http.StatusOK,
46
+			Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
47
+		}, nil
48
+	}))
49
+	assert.NilError(t, err)
50
+
51
+	err = client.NetworkRemove(context.Background(), "network_id")
53 52
 	assert.NilError(t, err)
54 53
 }