MCPcopy
hub / github.com/moby/moby / TestSetHostHeader

Function TestSetHostHeader

client/request_test.go:21–70  ·  view source on GitHub ↗

TestSetHostHeader should set fake host for local communications, set real host for normal communications.

(t *testing.T)

Source from the content-addressed store, hash-verified

19// TestSetHostHeader should set fake host for local communications, set real host
20// for normal communications.
21func TestSetHostHeader(t *testing.T) {
22 const testEndpoint = "/test"
23 testCases := []struct {
24 host string
25 expectedHost string
26 expectedURLHost string
27 }{
28 {
29 host: "unix:///var/run/docker.sock",
30 expectedHost: DummyHost,
31 expectedURLHost: "/var/run/docker.sock",
32 },
33 {
34 host: "npipe:////./pipe/docker_engine",
35 expectedHost: DummyHost,
36 expectedURLHost: "//./pipe/docker_engine",
37 },
38 {
39 host: "tcp://0.0.0.0:4243",
40 expectedHost: "",
41 expectedURLHost: "0.0.0.0:4243",
42 },
43 {
44 host: "tcp://localhost:4243",
45 expectedHost: "",
46 expectedURLHost: "localhost:4243",
47 },
48 }
49
50 for _, tc := range testCases {
51 t.Run(tc.host, func(t *testing.T) {
52 client, err := New(WithMockClient(func(req *http.Request) (*http.Response, error) {
53 if err := assertRequest(req, http.MethodGet, testEndpoint); err != nil {
54 return nil, err
55 }
56 if req.Host != tc.expectedHost {
57 return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host)
58 }
59 if req.URL.Host != tc.expectedURLHost {
60 return nil, fmt.Errorf("expected URL host %q, got %q", tc.expectedURLHost, req.URL.Host)
61 }
62 return mockResponse(http.StatusOK, nil, "")(req)
63 }), WithHost(tc.host))
64 assert.NilError(t, err)
65
66 _, err = client.sendRequest(t.Context(), http.MethodGet, testEndpoint, nil, nil, nil)
67 assert.NilError(t, err)
68 })
69 }
70}
71
72// TestPlainTextError tests the server returning an error in plain text.
73// API versions < 1.24 returned plain text errors, but we may encounter

Callers

nothing calls this directly

Calls 9

WithMockClientFunction · 0.85
assertRequestFunction · 0.85
mockResponseFunction · 0.85
WithHostFunction · 0.85
ErrorfMethod · 0.80
sendRequestMethod · 0.80
NewFunction · 0.70
RunMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…