Last active
April 5, 2016 09:11
-
-
Save duglin/4596113b600bcf44c84edd00b2368aa2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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