2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-04-08 08:14:07 -04:00
|
|
|
"context"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2019-04-08 08:14:07 -04:00
|
|
|
"strings"
|
2016-09-06 14:46:37 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-02-19 03:43:08 -05:00
|
|
|
"github.com/docker/docker/api"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/env"
|
|
|
|
"gotest.tools/v3/skip"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
func TestNewClientWithOpsFromEnv(t *testing.T) {
|
2017-09-28 17:10:06 -04:00
|
|
|
skip.If(t, runtime.GOOS == "windows")
|
2017-09-19 18:00:54 -04:00
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
doc string
|
2016-09-06 14:46:37 -04:00
|
|
|
envs map[string]string
|
|
|
|
expectedError string
|
|
|
|
expectedVersion string
|
|
|
|
}{
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid cert path",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "invalid/path",
|
|
|
|
},
|
2017-02-25 15:17:23 -05:00
|
|
|
expectedError: "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory",
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
2016-09-21 22:16:44 -04:00
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path and tls verify",
|
2016-09-21 22:16:44 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
"DOCKER_TLS_VERIFY": "1",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-21 22:16:44 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "default api version with cert path and host",
|
2016-09-21 22:16:44 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_CERT_PATH": "testdata/",
|
|
|
|
"DOCKER_HOST": "https://notaunixsocket",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-21 22:16:44 -04:00
|
|
|
},
|
2016-09-06 14:46:37 -04:00
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid docker host",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_HOST": "host",
|
|
|
|
},
|
|
|
|
expectedError: "unable to parse docker host `host`",
|
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "invalid docker host, with good format",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_HOST": "invalid://url",
|
|
|
|
},
|
2017-02-19 03:43:08 -05:00
|
|
|
expectedVersion: api.DefaultVersion,
|
2016-09-06 14:46:37 -04:00
|
|
|
},
|
|
|
|
{
|
2017-09-19 18:00:54 -04:00
|
|
|
doc: "override api version",
|
2016-09-06 14:46:37 -04:00
|
|
|
envs: map[string]string{
|
|
|
|
"DOCKER_API_VERSION": "1.22",
|
|
|
|
},
|
|
|
|
expectedVersion: "1.22",
|
|
|
|
},
|
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
|
2017-09-28 17:10:06 -04:00
|
|
|
defer env.PatchAll(t, nil)()
|
2022-03-07 04:14:15 -05:00
|
|
|
for _, tc := range testcases {
|
|
|
|
env.PatchAll(t, tc.envs)
|
|
|
|
client, err := NewClientWithOpts(FromEnv)
|
|
|
|
if tc.expectedError != "" {
|
|
|
|
assert.Check(t, is.Error(err, tc.expectedError), tc.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
} else {
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Check(t, err, tc.doc)
|
|
|
|
assert.Check(t, is.Equal(client.ClientVersion(), tc.expectedVersion), tc.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2016-09-21 22:16:44 -04:00
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
if tc.envs["DOCKER_TLS_VERIFY"] != "" {
|
2016-09-21 22:16:44 -04:00
|
|
|
// pedantic checking that this is handled correctly
|
2022-03-07 04:14:15 -05:00
|
|
|
tr := client.client.Transport.(*http.Transport)
|
|
|
|
assert.Assert(t, tr.TLSClientConfig != nil, tc.doc)
|
|
|
|
assert.Check(t, is.Equal(tr.TLSClientConfig.InsecureSkipVerify, false), tc.doc)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAPIPath(t *testing.T) {
|
2017-09-07 13:46:23 -04:00
|
|
|
testcases := []struct {
|
|
|
|
version string
|
|
|
|
path string
|
|
|
|
query url.Values
|
|
|
|
expected string
|
2016-09-06 14:46:37 -04:00
|
|
|
}{
|
2022-03-07 05:12:32 -05:00
|
|
|
{"", "/containers/json", nil, "/v" + api.DefaultVersion + "/containers/json"},
|
|
|
|
{"", "/containers/json", url.Values{}, "/v" + api.DefaultVersion + "/containers/json"},
|
|
|
|
{"", "/containers/json", url.Values{"s": []string{"c"}}, "/v" + api.DefaultVersion + "/containers/json?s=c"},
|
2016-09-06 14:46:37 -04:00
|
|
|
{"1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
|
|
|
{"1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
|
|
|
{"1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
|
|
|
{"v1.22", "/containers/json", nil, "/v1.22/containers/json"},
|
|
|
|
{"v1.22", "/containers/json", url.Values{}, "/v1.22/containers/json"},
|
|
|
|
{"v1.22", "/containers/json", url.Values{"s": []string{"c"}}, "/v1.22/containers/json?s=c"},
|
|
|
|
{"v1.22", "/networks/kiwl$%^", nil, "/v1.22/networks/kiwl$%25%5E"},
|
|
|
|
}
|
|
|
|
|
2019-04-08 08:14:07 -04:00
|
|
|
ctx := context.TODO()
|
2022-03-07 04:14:15 -05:00
|
|
|
for _, tc := range testcases {
|
2022-03-07 05:12:32 -05:00
|
|
|
client, err := NewClientWithOpts(
|
|
|
|
WithVersion(tc.version),
|
|
|
|
WithHost("tcp://localhost:2375"),
|
|
|
|
)
|
|
|
|
assert.NilError(t, err)
|
2022-03-07 04:14:15 -05:00
|
|
|
actual := client.getAPIPath(ctx, tc.path, tc.query)
|
|
|
|
assert.Check(t, is.Equal(actual, tc.expected))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:22:11 -04:00
|
|
|
func TestParseHostURL(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
host string
|
|
|
|
expected *url.URL
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
host: "",
|
|
|
|
expectedErr: "unable to parse docker host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "foobar",
|
|
|
|
expectedErr: "unable to parse docker host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "foo://bar",
|
|
|
|
expected: &url.URL{Scheme: "foo", Host: "bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "tcp://localhost:2476",
|
|
|
|
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
host: "tcp://localhost:2476/path",
|
|
|
|
expected: &url.URL{Scheme: "tcp", Host: "localhost:2476", Path: "/path"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
actual, err := ParseHostURL(testcase.host)
|
|
|
|
if testcase.expectedErr != "" {
|
2018-05-20 18:06:50 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, testcase.expectedErr))
|
2017-09-07 12:22:11 -04:00
|
|
|
}
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Check(t, is.DeepEqual(actual, testcase.expected))
|
2017-09-07 12:22:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
|
2017-09-28 17:10:06 -04:00
|
|
|
defer env.PatchAll(t, map[string]string{
|
2017-06-21 01:58:16 -04:00
|
|
|
"DOCKER_HOST": "",
|
|
|
|
"DOCKER_API_VERSION": "",
|
|
|
|
"DOCKER_TLS_VERIFY": "",
|
|
|
|
"DOCKER_CERT_PATH": "",
|
2017-09-28 17:10:06 -04:00
|
|
|
})()
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := NewClientWithOpts(FromEnv)
|
2017-06-21 01:58:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Check(t, is.Equal(client.ClientVersion(), api.DefaultVersion))
|
2016-09-06 14:46:37 -04:00
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
const expected = "1.22"
|
|
|
|
_ = os.Setenv("DOCKER_API_VERSION", expected)
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err = NewClientWithOpts(FromEnv)
|
2017-06-21 01:58:16 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Check(t, is.Equal(client.ClientVersion(), expected))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
// TestNegotiateAPIVersionEmpty asserts that client.Client version negotiation
|
|
|
|
// downgrades to the correct API version if the API's ping response does not
|
|
|
|
// return an API version.
|
2017-06-21 01:58:16 -04:00
|
|
|
func TestNegotiateAPIVersionEmpty(t *testing.T) {
|
2018-06-07 22:58:28 -04:00
|
|
|
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": ""})()
|
2017-06-21 01:58:16 -04:00
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := NewClientWithOpts(FromEnv)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
// set our version to something new
|
|
|
|
client.version = "1.25"
|
|
|
|
|
|
|
|
// if no version from server, expect the earliest
|
|
|
|
// version before APIVersion was implemented
|
2022-03-07 04:14:15 -05:00
|
|
|
const expected = "1.24"
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
// test downgrade
|
2022-03-07 04:14:15 -05:00
|
|
|
client.NegotiateAPIVersionPing(types.Ping{})
|
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestNegotiateAPIVersion asserts that client.Client can
|
|
|
|
// negotiate a compatible APIVersion with the server
|
|
|
|
func TestNegotiateAPIVersion(t *testing.T) {
|
2022-03-07 05:13:52 -05:00
|
|
|
tests := []struct {
|
|
|
|
doc string
|
|
|
|
clientVersion string
|
|
|
|
pingVersion string
|
|
|
|
expectedVersion string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// client should downgrade to the version reported by the daemon.
|
|
|
|
doc: "downgrade from default",
|
|
|
|
pingVersion: "1.21",
|
|
|
|
expectedVersion: "1.21",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// client should not downgrade to the version reported by the
|
|
|
|
// daemon if a custom version was set.
|
|
|
|
doc: "no downgrade from custom version",
|
|
|
|
clientVersion: "1.25",
|
|
|
|
pingVersion: "1.21",
|
|
|
|
expectedVersion: "1.25",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// client should downgrade to the last version before version
|
|
|
|
// negotiation was added (1.24) if the daemon does not report
|
|
|
|
// a version.
|
|
|
|
doc: "downgrade legacy",
|
|
|
|
pingVersion: "",
|
|
|
|
expectedVersion: "1.24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// client should downgrade to the version reported by the daemon.
|
|
|
|
// version negotiation was added in API 1.25, so this is theoretical,
|
|
|
|
// but it should negotiate to versions before that if the daemon
|
|
|
|
// gives that as a response.
|
|
|
|
doc: "downgrade old",
|
|
|
|
pingVersion: "1.19",
|
|
|
|
expectedVersion: "1.19",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// client should not upgrade to a newer version if a version was set,
|
|
|
|
// even if both the daemon and the client support it.
|
|
|
|
doc: "no upgrade",
|
|
|
|
clientVersion: "1.20",
|
|
|
|
pingVersion: "1.21",
|
|
|
|
expectedVersion: "1.20",
|
|
|
|
},
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2022-03-07 05:13:52 -05:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
opts := make([]Opt, 0)
|
|
|
|
if tc.clientVersion != "" {
|
|
|
|
// Note that this check is redundant, as WithVersion() considers
|
|
|
|
// an empty version equivalent to "not setting a version", but
|
|
|
|
// doing this just to be explicit we are using the default.
|
|
|
|
opts = append(opts, WithVersion(tc.clientVersion))
|
|
|
|
}
|
|
|
|
client, err := NewClientWithOpts(opts...)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
client.NegotiateAPIVersionPing(types.Ping{APIVersion: tc.pingVersion})
|
|
|
|
assert.Equal(t, tc.expectedVersion, client.ClientVersion())
|
|
|
|
})
|
|
|
|
}
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
// TestNegotiateAPIVersionOverride asserts that we honor the DOCKER_API_VERSION
|
|
|
|
// environment variable when negotiating versions.
|
2017-06-21 01:58:16 -04:00
|
|
|
func TestNegotiateAPVersionOverride(t *testing.T) {
|
2022-03-07 04:14:15 -05:00
|
|
|
const expected = "9.99"
|
2017-09-28 17:10:06 -04:00
|
|
|
defer env.PatchAll(t, map[string]string{"DOCKER_API_VERSION": expected})()
|
2017-06-21 01:58:16 -04:00
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := NewClientWithOpts(FromEnv)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-21 01:58:16 -04:00
|
|
|
|
|
|
|
// test that we honored the env var
|
2022-03-07 04:14:15 -05:00
|
|
|
client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.24"})
|
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2017-06-21 01:58:16 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:14:07 -04:00
|
|
|
func TestNegotiateAPIVersionAutomatic(t *testing.T) {
|
|
|
|
var pingVersion string
|
|
|
|
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}}
|
|
|
|
resp.Header.Set("API-Version", pingVersion)
|
2021-08-24 06:10:50 -04:00
|
|
|
resp.Body = io.NopCloser(strings.NewReader("OK"))
|
2019-04-08 08:14:07 -04:00
|
|
|
return resp, nil
|
|
|
|
})
|
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
ctx := context.Background()
|
2019-04-08 08:14:07 -04:00
|
|
|
client, err := NewClientWithOpts(
|
|
|
|
WithHTTPClient(httpClient),
|
|
|
|
WithAPIVersionNegotiation(),
|
|
|
|
)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
// Client defaults to use api.DefaultVersion before version-negotiation.
|
|
|
|
expected := api.DefaultVersion
|
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2019-04-08 08:14:07 -04:00
|
|
|
|
|
|
|
// First request should trigger negotiation
|
|
|
|
pingVersion = "1.35"
|
2022-03-07 04:14:15 -05:00
|
|
|
expected = "1.35"
|
2019-04-08 08:14:07 -04:00
|
|
|
_, _ = client.Info(ctx)
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2019-04-08 08:14:07 -04:00
|
|
|
|
|
|
|
// Once successfully negotiated, subsequent requests should not re-negotiate
|
|
|
|
pingVersion = "1.25"
|
2022-03-07 04:14:15 -05:00
|
|
|
expected = "1.35"
|
2019-04-08 08:14:07 -04:00
|
|
|
_, _ = client.Info(ctx)
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2019-04-08 08:14:07 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:54:16 -04:00
|
|
|
// TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client
|
|
|
|
// with an empty version string does still allow API-version negotiation
|
|
|
|
func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) {
|
|
|
|
client, err := NewClientWithOpts(WithVersion(""))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-03-07 04:14:15 -05:00
|
|
|
const expected = "1.35"
|
|
|
|
client.NegotiateAPIVersionPing(types.Ping{APIVersion: expected})
|
|
|
|
assert.Equal(t, client.ClientVersion(), expected)
|
2019-04-09 18:54:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client
|
2022-03-07 04:14:15 -05:00
|
|
|
// with a fixed version disables API-version negotiation
|
2019-04-09 18:54:16 -04:00
|
|
|
func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) {
|
2022-03-07 04:14:15 -05:00
|
|
|
const customVersion = "1.35"
|
|
|
|
client, err := NewClientWithOpts(WithVersion(customVersion))
|
2019-04-09 18:54:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.31"})
|
2022-03-07 04:14:15 -05:00
|
|
|
assert.Equal(t, client.ClientVersion(), customVersion)
|
2019-04-09 18:54:16 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 05:05:35 -04:00
|
|
|
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
return rtf(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
type bytesBufferClose struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bbc bytesBufferClose) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientRedirect(t *testing.T) {
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: CheckRedirect,
|
|
|
|
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
|
|
|
if req.URL.String() == "/bla" {
|
|
|
|
return &http.Response{StatusCode: 404}, nil
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: 301,
|
|
|
|
Header: map[string][]string{"Location": {"/bla"}},
|
|
|
|
Body: bytesBufferClose{bytes.NewBuffer(nil)},
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
httpMethod string
|
2017-12-22 16:30:49 -05:00
|
|
|
expectedErr *url.Error
|
2017-03-27 05:05:35 -04:00
|
|
|
statusCode int
|
|
|
|
}{
|
|
|
|
{http.MethodGet, nil, 301},
|
|
|
|
{http.MethodPost, &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
{http.MethodPut, &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
{http.MethodDelete, &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, 301},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2022-03-07 05:13:52 -05:00
|
|
|
tc := tc
|
|
|
|
t.Run(tc.httpMethod, func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest(tc.httpMethod, "/redirectme", nil)
|
|
|
|
assert.Check(t, err)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert.Check(t, is.Equal(resp.StatusCode, tc.statusCode))
|
|
|
|
if tc.expectedErr == nil {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
urlError, ok := err.(*url.Error)
|
|
|
|
assert.Assert(t, ok, "%T is not *url.Error", err)
|
|
|
|
assert.Check(t, is.Equal(*urlError, *tc.expectedErr))
|
|
|
|
}
|
|
|
|
})
|
2017-03-27 05:05:35 -04:00
|
|
|
}
|
|
|
|
}
|