client/request_test.go
4f0d95fa
 package client // import "github.com/docker/docker/client"
7c36a1af
 
 import (
 	"bytes"
7d62e40f
 	"context"
7c36a1af
 	"fmt"
 	"io/ioutil"
1db4be0c
 	"math/rand"
7c36a1af
 	"net/http"
 	"strings"
 	"testing"
 
 	"github.com/docker/docker/api/types"
161e0a90
 	"github.com/docker/docker/errdefs"
38457285
 	"gotest.tools/assert"
1db4be0c
 	is "gotest.tools/assert/cmp"
7c36a1af
 )
 
 // TestSetHostHeader should set fake host for local communications, set real host
 // for normal communications.
 func TestSetHostHeader(t *testing.T) {
 	testURL := "/test"
 	testCases := []struct {
 		host            string
 		expectedHost    string
 		expectedURLHost string
 	}{
 		{
 			"unix:///var/run/docker.sock",
 			"docker",
 			"/var/run/docker.sock",
 		},
 		{
 			"npipe:////./pipe/docker_engine",
 			"docker",
 			"//./pipe/docker_engine",
 		},
 		{
 			"tcp://0.0.0.0:4243",
 			"",
 			"0.0.0.0:4243",
 		},
 		{
 			"tcp://localhost:4243",
 			"",
 			"localhost:4243",
 		},
 	}
 
 	for c, test := range testCases {
0a91ba2d
 		hostURL, err := ParseHostURL(test.host)
6be0f709
 		assert.NilError(t, err)
7c36a1af
 
 		client := &Client{
9a072adf
 			client: newMockClient(func(req *http.Request) (*http.Response, error) {
7c36a1af
 				if !strings.HasPrefix(req.URL.Path, testURL) {
 					return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
 				}
 				if req.Host != test.expectedHost {
 					return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
 				}
 				if req.URL.Host != test.expectedURLHost {
 					return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
 				}
 				return &http.Response{
 					StatusCode: http.StatusOK,
f23c00d8
 					Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
7c36a1af
 				}, nil
 			}),
9a072adf
 
0a91ba2d
 			proto:    hostURL.Scheme,
 			addr:     hostURL.Host,
 			basePath: hostURL.Path,
7c36a1af
 		}
 
 		_, err = client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil)
6be0f709
 		assert.NilError(t, err)
7c36a1af
 	}
 }
 
 // TestPlainTextError tests the server returning an error in plain text for
 // backwards compatibility with API versions <1.24. All other tests use
 // errors returned as JSON
 func TestPlainTextError(t *testing.T) {
 	client := &Client{
9a072adf
 		client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
7c36a1af
 	}
 	_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
 	if err == nil || err.Error() != "Error response from daemon: Server error" {
 		t.Fatalf("expected a Server Error, got %v", err)
 	}
161e0a90
 	if !errdefs.IsSystem(err) {
 		t.Fatalf("expected a Server Error, got %T", err)
 	}
7c36a1af
 }
1db4be0c
 
 func TestInfiniteError(t *testing.T) {
 	infinitR := rand.New(rand.NewSource(42))
 	client := &Client{
 		client: newMockClient(func(req *http.Request) (*http.Response, error) {
 			resp := &http.Response{StatusCode: http.StatusInternalServerError}
 			resp.Header = http.Header{}
 			resp.Body = ioutil.NopCloser(infinitR)
 			return resp, nil
 		}),
 	}
 
 	_, err := client.Ping(context.Background())
 	assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
 }