Browse code

Update authz plugin test to not use httputil

Signed-off-by: Derek McGowan <derek@mcg.dev>

Derek McGowan authored on 2023/12/12 04:30:28
Showing 1 changed files
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"io"
9 9
 	"net"
10 10
 	"net/http"
11
-	"net/http/httputil"
12 11
 	"net/url"
13 12
 	"os"
14 13
 	"path/filepath"
... ...
@@ -25,6 +24,7 @@ import (
25 25
 	"github.com/docker/docker/pkg/archive"
26 26
 	"github.com/docker/docker/pkg/authorization"
27 27
 	"github.com/docker/docker/testutil/environment"
28
+	"github.com/docker/go-connections/sockets"
28 29
 	"gotest.tools/v3/assert"
29 30
 	"gotest.tools/v3/skip"
30 31
 )
... ...
@@ -81,6 +81,17 @@ func isAllowed(reqURI string) bool {
81 81
 	return false
82 82
 }
83 83
 
84
+func socketHTTPClient(u *url.URL) (*http.Client, error) {
85
+	transport := &http.Transport{}
86
+	err := sockets.ConfigureTransport(transport, u.Scheme, u.Path)
87
+	if err != nil {
88
+		return nil, err
89
+	}
90
+	return &http.Client{
91
+		Transport: transport,
92
+	}, nil
93
+}
94
+
84 95
 func TestAuthZPluginAllowRequest(t *testing.T) {
85 96
 	ctx := setupTestV1(t)
86 97
 
... ...
@@ -176,15 +187,17 @@ func TestAuthZPluginAPIDenyResponse(t *testing.T) {
176 176
 	daemonURL, err := url.Parse(d.Sock())
177 177
 	assert.NilError(t, err)
178 178
 
179
-	conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
179
+	socketClient, err := socketHTTPClient(daemonURL)
180 180
 	assert.NilError(t, err)
181
-	c := httputil.NewClientConn(conn, nil)
182
-	req, err := http.NewRequest(http.MethodGet, "/version", nil)
181
+
182
+	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil)
183 183
 	assert.NilError(t, err)
184
-	req = req.WithContext(ctx)
185
-	resp, err := c.Do(req)
184
+	req.URL.Scheme = "http"
185
+	req.URL.Host = client.DummyHost
186 186
 
187
+	resp, err := socketClient.Do(req)
187 188
 	assert.NilError(t, err)
189
+
188 190
 	assert.DeepEqual(t, http.StatusForbidden, resp.StatusCode)
189 191
 }
190 192
 
... ...
@@ -471,13 +484,15 @@ func TestAuthZPluginHeader(t *testing.T) {
471 471
 	daemonURL, err := url.Parse(d.Sock())
472 472
 	assert.NilError(t, err)
473 473
 
474
-	conn, err := net.DialTimeout(daemonURL.Scheme, daemonURL.Path, time.Second*10)
474
+	socketClient, err := socketHTTPClient(daemonURL)
475 475
 	assert.NilError(t, err)
476
-	client := httputil.NewClientConn(conn, nil)
477
-	req, err := http.NewRequest(http.MethodGet, "/version", nil)
476
+
477
+	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/version", nil)
478 478
 	assert.NilError(t, err)
479
-	req = req.WithContext(ctx)
480
-	resp, err := client.Do(req)
479
+	req.URL.Scheme = "http"
480
+	req.URL.Host = client.DummyHost
481
+
482
+	resp, err := socketClient.Do(req)
481 483
 	assert.NilError(t, err)
482 484
 	assert.Equal(t, "application/json", resp.Header["Content-Type"][0])
483 485
 }