Skip to content

Commit

Permalink
use the ProviderBaseURL's host when verifying the provider is alive 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
fewstera committed Nov 26, 2018
1 parent 2acf846 commit 24ee70f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
19 changes: 16 additions & 3 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 24ee70f

Please sign in to comment.