https://pact.io logo
m

Mark Wilson

07/26/2022, 2:20 PM
Hey does anyone know how to set the port for the mock server during the pact setup in golang?
m

Matt (pactflow.io / pact-js / pact-go)

07/26/2022, 10:48 PM
In the 1.x.x branch you can set
AllowedMockServerPorts
but really it’s best practice to have it set dynamically (https://github.com/pact-foundation/pact-go/blob/master/dsl/pact.go#L88)
m

Mark Wilson

07/27/2022, 12:38 PM
So our issue is that we are trying to run the pact mock server on a function that exists in a different package so we don't have access to the client object within that other package. What would be the best practice there? In the example here https://github.com/pact-foundation/pact-workshop-go/blob/step3/consumer/client/client.go the client.go and test file are within the same package so the baseurl of the mock service can be passed into it easily.
The function in the different package allows a string and a jwt value and we call a get httprequest within it. So we would look to update that URL to be the baseurl from the pact mock server. We are trying to hardcode the URL for POC testing but obviously having issues if we don't know which port it is going to be ran on
m

Matt (pactflow.io / pact-js / pact-go)

07/27/2022, 12:55 PM
If you’re new to Pact, i’d look start straight away with the 2.x.x package, which is almost stable. You can specify a port in that.
Or you can used the
AllowedMockServerPorts
and just pass a single port for the 1.x.x
m

Mark Wilson

07/28/2022, 12:52 PM
The AllowedMockServerPorts does not work - when we set this it still selects a random port
Copy code
func createPact() dsl.Pact {
    return dsl.Pact{
        Consumer:               "car_config",
        Provider:               "vehicle_targeting",
        LogDir:                 "/home/mwilso29/git/car-configuration/tests/contract/consumer/logs",
        PactDir:                "/home/mwilso29/git/car-configuration/tests/contract/consumer/pacts",
        LogLevel:               "INFO",
        Host:                   "localhost",
        AllowedMockServerPorts: "8111",
    }
}
I will take a look at the 2.x.x package now
4 Views