github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/request_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"fmt"
     8  	"io"
     9  	"math/rand"
    10  	"net/http"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/docker/docker/api/types"
    16  	"github.com/docker/docker/errdefs"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  )
    20  
    21  // TestSetHostHeader should set fake host for local communications, set real host
    22  // for normal communications.
    23  func TestSetHostHeader(t *testing.T) {
    24  	testURL := "/test"
    25  	testCases := []struct {
    26  		host            string
    27  		expectedHost    string
    28  		expectedURLHost string
    29  	}{
    30  		{
    31  			"unix:///var/run/docker.sock",
    32  			"docker",
    33  			"/var/run/docker.sock",
    34  		},
    35  		{
    36  			"npipe:////./pipe/docker_engine",
    37  			"docker",
    38  			"//./pipe/docker_engine",
    39  		},
    40  		{
    41  			"tcp://0.0.0.0:4243",
    42  			"",
    43  			"0.0.0.0:4243",
    44  		},
    45  		{
    46  			"tcp://localhost:4243",
    47  			"",
    48  			"localhost:4243",
    49  		},
    50  	}
    51  
    52  	for c, test := range testCases {
    53  		hostURL, err := ParseHostURL(test.host)
    54  		assert.NilError(t, err)
    55  
    56  		client := &Client{
    57  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    58  				if !strings.HasPrefix(req.URL.Path, testURL) {
    59  					return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
    60  				}
    61  				if req.Host != test.expectedHost {
    62  					return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
    63  				}
    64  				if req.URL.Host != test.expectedURLHost {
    65  					return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
    66  				}
    67  				return &http.Response{
    68  					StatusCode: http.StatusOK,
    69  					Body:       io.NopCloser(bytes.NewReader([]byte(""))),
    70  				}, nil
    71  			}),
    72  
    73  			proto:    hostURL.Scheme,
    74  			addr:     hostURL.Host,
    75  			basePath: hostURL.Path,
    76  		}
    77  
    78  		_, err = client.sendRequest(context.Background(), http.MethodGet, testURL, nil, nil, nil)
    79  		assert.NilError(t, err)
    80  	}
    81  }
    82  
    83  // TestPlainTextError tests the server returning an error in plain text for
    84  // backwards compatibility with API versions <1.24. All other tests use
    85  // errors returned as JSON
    86  func TestPlainTextError(t *testing.T) {
    87  	client := &Client{
    88  		client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
    89  	}
    90  	_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
    91  	if !errdefs.IsSystem(err) {
    92  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    93  	}
    94  }
    95  
    96  func TestInfiniteError(t *testing.T) {
    97  	infinitR := rand.New(rand.NewSource(42))
    98  	client := &Client{
    99  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   100  			resp := &http.Response{StatusCode: http.StatusInternalServerError}
   101  			resp.Header = http.Header{}
   102  			resp.Body = io.NopCloser(infinitR)
   103  			return resp, nil
   104  		}),
   105  	}
   106  
   107  	_, err := client.Ping(context.Background())
   108  	assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
   109  }
   110  
   111  func TestCanceledContext(t *testing.T) {
   112  	testURL := "/test"
   113  
   114  	client := &Client{
   115  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   116  			assert.Equal(t, req.Context().Err(), context.Canceled)
   117  
   118  			return &http.Response{}, context.Canceled
   119  		}),
   120  	}
   121  
   122  	ctx, cancel := context.WithCancel(context.Background())
   123  	cancel()
   124  
   125  	_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
   126  	assert.Equal(t, true, errdefs.IsCancelled(err))
   127  	assert.Equal(t, true, errors.Is(err, context.Canceled))
   128  }
   129  
   130  func TestDeadlineExceededContext(t *testing.T) {
   131  	testURL := "/test"
   132  
   133  	client := &Client{
   134  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   135  			assert.Equal(t, req.Context().Err(), context.DeadlineExceeded)
   136  
   137  			return &http.Response{}, context.DeadlineExceeded
   138  		}),
   139  	}
   140  
   141  	ctx, cancel := context.WithDeadline(context.Background(), time.Now())
   142  	defer cancel()
   143  
   144  	<-ctx.Done()
   145  
   146  	_, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
   147  	assert.Equal(t, true, errdefs.IsDeadline(err))
   148  	assert.Equal(t, true, errors.Is(err, context.DeadlineExceeded))
   149  }