github.com/vvnotw/moby@v1.13.1/client/request_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  // TestSetHostHeader should set fake host for local communications, set real host
    16  // for normal communications.
    17  func TestSetHostHeader(t *testing.T) {
    18  	testURL := "/test"
    19  	testCases := []struct {
    20  		host            string
    21  		expectedHost    string
    22  		expectedURLHost string
    23  	}{
    24  		{
    25  			"unix:///var/run/docker.sock",
    26  			"docker",
    27  			"/var/run/docker.sock",
    28  		},
    29  		{
    30  			"npipe:////./pipe/docker_engine",
    31  			"docker",
    32  			"//./pipe/docker_engine",
    33  		},
    34  		{
    35  			"tcp://0.0.0.0:4243",
    36  			"",
    37  			"0.0.0.0:4243",
    38  		},
    39  		{
    40  			"tcp://localhost:4243",
    41  			"",
    42  			"localhost:4243",
    43  		},
    44  	}
    45  
    46  	for c, test := range testCases {
    47  		proto, addr, basePath, err := ParseHost(test.host)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		client := &Client{
    53  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    54  				if !strings.HasPrefix(req.URL.Path, testURL) {
    55  					return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
    56  				}
    57  				if req.Host != test.expectedHost {
    58  					return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
    59  				}
    60  				if req.URL.Host != test.expectedURLHost {
    61  					return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
    62  				}
    63  				return &http.Response{
    64  					StatusCode: http.StatusOK,
    65  					Body:       ioutil.NopCloser(bytes.NewReader(([]byte("")))),
    66  				}, nil
    67  			}),
    68  
    69  			proto:    proto,
    70  			addr:     addr,
    71  			basePath: basePath,
    72  		}
    73  
    74  		_, err = client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil)
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  	}
    79  }
    80  
    81  // TestPlainTextError tests the server returning an error in plain text for
    82  // backwards compatibility with API versions <1.24. All other tests use
    83  // errors returned as JSON
    84  func TestPlainTextError(t *testing.T) {
    85  	client := &Client{
    86  		client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
    87  	}
    88  	_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
    89  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    90  		t.Fatalf("expected a Server Error, got %v", err)
    91  	}
    92  }