2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-13 19:28:34 +00:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2017-09-19 20:12:29 +00:00
|
|
|
hostURL, err := ParseHostURL(test.host)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
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,
|
2018-05-19 11:38:54 +00:00
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
2016-09-09 03:44:25 +00:00
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
proto: hostURL.Scheme,
|
|
|
|
addr: hostURL.Host,
|
|
|
|
basePath: hostURL.Path,
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, 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)
|
|
|
|
}
|
|
|
|
}
|