From 24ee70f6032e6e26430ed99eed632cde58d1cfca Mon Sep 17 00:00:00 2001 From: Aidan Fewster Date: Mon, 26 Nov 2018 12:33:30 +0000 Subject: [PATCH] =?UTF-8?q?use=20the=20ProviderBaseURL's=20host=20when=20v?= =?UTF-8?q?erifying=20the=20provider=20is=20alive=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dsl/client.go | 19 ++++++++++++++++--- dsl/client_test.go | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/dsl/client.go b/dsl/client.go index 38b96f4f7..00253c23e 100644 --- a/dsl/client.go +++ b/dsl/client.go @@ -120,9 +120,10 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) (types.Provider return response, err } + address := getAddress(request.ProviderBaseURL) port := getPort(request.ProviderBaseURL) - waitForPort(port, p.getNetworkInterface(), p.Address, p.TimeoutDuration, + waitForPort(port, p.getNetworkInterface(), address, p.TimeoutDuration, fmt.Sprintf(`Timed out waiting for Provider API to start on port %d - are you sure it's running?`, port)) // Run command, splitting out stderr and stdout. The command can fail for @@ -295,8 +296,9 @@ func (p *PactClient) ReifyMessage(request *types.PactReificationRequest) (res *t func getPort(rawURL string) int { parsedURL, err := url.Parse(rawURL) if err == nil { - if len(strings.Split(parsedURL.Host, ":")) == 2 { - port, err := strconv.Atoi(strings.Split(parsedURL.Host, ":")[1]) + splitHost := strings.Split(parsedURL.Host, ":") + if len(splitHost) == 2 { + port, err := strconv.Atoi(splitHost[1]) if err == nil { return port } @@ -310,6 +312,17 @@ func getPort(rawURL string) int { return -1 } +// Get the address given a URL +func getAddress(rawURL string) string { + parsedURL, err := url.Parse(rawURL) + if err != nil { + return "" + } + + splitHost := strings.Split(parsedURL.Host, ":") + return splitHost[0] +} + // Use this to wait for a port to be running prior // to running tests. var waitForPort = func(port int, network string, address string, timeoutDuration time.Duration, message string) error { diff --git a/dsl/client_test.go b/dsl/client_test.go index 476abf4ab..59f5a9bc3 100644 --- a/dsl/client_test.go +++ b/dsl/client_test.go @@ -135,6 +135,21 @@ func TestClient_getPort(t *testing.T) { } } +func TestClient_getAddress(t *testing.T) { + testCases := map[string]string{ + "http://localhost:8000": "localhost", + "http://localhost": "localhost", + "http://127.0.0.1": "127.0.0.1", + ":::::": "", + } + + for host, address := range testCases { + if getAddress(host) != address { + t.Fatalf("Expected host '%s' to return address '%s' but got '%s'", host, address, getAddress(host)) + } + } +} + func TestClient_sanitiseRubyResponse(t *testing.T) { var tests = map[string]string{ "this is a sentence with a hash # so it should be in tact": "this is a sentence with a hash # so it should be in tact",