Skip to content

Instantly share code, notes, and snippets.

@duglin
Last active April 5, 2016 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duglin/4596113b600bcf44c84edd00b2368aa2 to your computer and use it in GitHub Desktop.
Save duglin/4596113b600bcf44c84edd00b2368aa2 to your computer and use it in GitHub Desktop.
package client
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
)
// 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",
"docker",
},
{
"npipe:////./pipe/docker_engine",
"docker",
"docker",
},
{
"tcp://0.0.0.0:4243",
"0.0.0.0:4243",
"0.0.0.0:4243",
},
{
"tcp://localhost:4243",
"localhost:4243",
"localhost:4243",
},
}
for c, test := range testCases {
client, _ := NewClient(test.host, "", nil, nil)
client.transport = newMockClient(nil, func(req *http.Request) (*http.Response, error) {
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,
Body: ioutil.NopCloser(bytes.NewReader(([]byte("")))),
}, nil
})
_, err := client.sendRequest(context.Background(), "GET", testURL, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment