TestSetHostHeader should set fake host for local communications, set real host for normal communications.
(t *testing.T)
| 19 | // TestSetHostHeader should set fake host for local communications, set real host |
| 20 | // for normal communications. |
| 21 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…