Broker Connection
Code snippets to connect to your broker
Code snippets to connect to your broker
Pact
Pactflow
Pact JS (Node JS)
- Pact-JS documentation for all the pact verification options.
const { Verifier } = require("@pact-foundation/pact");
return new Verifier().verifyProvider({
provider: "<Your provider name here>",
providerBaseUrl: "http://localhost:8081",
// Fetch pacts from broker
pactBrokerUrl: "https://<YOUR_BROKER>.pactflow.io/",
pactBrokerToken: "<TOKEN>",
publishVerificationResult: process.env.CI === "true",
providerVersion: process.env.GIT_COMMIT,
});
Java / JUnit 5
- Pact-JVM documentation for all the pact verification options.
@Provider("<Your provider name here>")
@PactBroker(host = "<YOUR_BROKER>.pactflow.io", scheme = "https",
authentication = @PactBrokerAuth(scheme = "bearer", username = "<TOKEN>", password = ""))
public class PactJUnitBrokerTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(Pact pact, Interaction interaction, HttpRequest request, PactVerificationContext context) {
context.verifyInteraction();
}
}
Java / JUnit 4
- Pact-JVM documentation for all the pact verification options.
@RunWith(PactRunner.class)
@Provider("<Your provider name here>")
@PactBroker(host = "<YOUR_BROKER>.pactflow.io", scheme = "https",
authentication = @PactBrokerAuth(scheme = "bearer", username = "<TOKEN>", password = ""))
public class PactJUnitBrokerTest {
@TestTarget
public final Target target = new HttpTarget(8080);
}
Java / Gradle
- Pact-JVM documentation for all the pact verification options.
// To turn on the verification publishing,
// set the project property `pact.verifier.publishResults` to `true`
pact {
serviceProviders {
'<Your provider name here>' {
providerVersion = { '<GIT_COMMIT>' }
hasPactsFromPactBroker('https://<YOUR_BROKER>.pactflow.io/',
authentication: ['Bearer', '<TOKEN>'])
}
}
}
Golang
_, err := pact.VerifyProvider(t, types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port),
BrokerURL: "https://<YOUR_BROKER>.pactflow.io",
BrokerToken: "<TOKEN>",
PublishVerificationResults: true,
ProviderVersion: "<GIT_COMMIT>"
})
Ruby
- Pact Ruby documentation for all the verification options.
spec/pact_helper.rb
Pact.service_provider "<Your provider name here>" do
app_version ENV['GIT_COMMIT']
publish_verification_results ENV['CI'] == 'true'
honours_pacts_from_pact_broker do
pact_broker_base_url "https://<YOUR_BROKER>.pactflow.io", { token: "<TOKEN>" }
end
end
.NET
PactNet documentation for all the pact verification options.
Provider verification options
var ProviderVersion = Environment.GetEnvironmentVariable("GIT_COMMIT");
var PublishVerificationResults = "true".Equals(Environment.GetEnvironmentVariable("CI"));
var config = new PactVerifierConfig
{
ProviderVersion, //NOTE: Setting a provider version is required for publishing verification results
PublishVerificationResults
};
IPactVerifier pactVerifier = new PactVerifier(config);
pactVerifier
.ServiceProvider("<Your provider name here>", "http://your-test-provider-url")
.PactBroker("https://<YOUR_BROKER>.pactflow.io", uriOptions: new PactUriOptions("<TOKEN>"))
.Verify();
Docker
docker-compose-verify.yml
version: "3"
services:
api:
image: "your image"
expose:
- "9292"
pact_verifier:
image: pactfoundation/pact-cli:latest
depends_on:
- api
environment:
- PACT_BROKER_BASE_URL="https://<YOUR_BROKER>.pactflow.io"
- PACT_BROKER_TOKEN="<TOKEN>"
command: >
verify
--provider-base-url http://api:9292
--provider "<Your provider name here>"
To run
docker-compose -f docker-compose-verify.yml up \
--build --abort-on-container-exit --exit-code-from pact_verifier
Pactflow
Pact JS (Node JS)
- Pact-JS documentation for all the pact verification options.
const { Verifier } = require("@pact-foundation/pact");
return new Verifier().verifyProvider({
provider: "<Your provider name here>",
providerBaseUrl: "http://localhost:8081",
// Fetch pacts from broker
pactBrokerUrl: "https://<YOUR_BROKER>.pactflow.io/",
pactBrokerToken: "<TOKEN>",
publishVerificationResult: process.env.CI === "true",
providerVersion: process.env.GIT_COMMIT,
});
Java / JUnit 5
- Pact-JVM documentation for all the pact verification options.
@Provider("<Your provider name here>")
@PactBroker(host = "<YOUR_BROKER>.pactflow.io", scheme = "https",
authentication = @PactBrokerAuth(scheme = "bearer", username = "<TOKEN>", password = ""))
public class PactJUnitBrokerTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(Pact pact, Interaction interaction, HttpRequest request, PactVerificationContext context) {
context.verifyInteraction();
}
}
Java / JUnit 4
- Pact-JVM documentation for all the pact verification options.
@RunWith(PactRunner.class)
@Provider("<Your provider name here>")
@PactBroker(host = "<YOUR_BROKER>.pactflow.io", scheme = "https",
authentication = @PactBrokerAuth(scheme = "bearer", username = "<TOKEN>", password = ""))
public class PactJUnitBrokerTest {
@TestTarget
public final Target target = new HttpTarget(8080);
}
Java / Gradle
- Pact-JVM documentation for all the pact verification options.
// To turn on the verification publishing,
// set the project property `pact.verifier.publishResults` to `true`
pact {
serviceProviders {
'<Your provider name here>' {
providerVersion = { '<GIT_COMMIT>' }
hasPactsFromPactBroker('https://<YOUR_BROKER>.pactflow.io/',
authentication: ['Bearer', '<TOKEN>'])
}
}
}
Golang
_, err := pact.VerifyProvider(t, types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port),
BrokerURL: "https://<YOUR_BROKER>.pactflow.io",
BrokerToken: "<TOKEN>",
PublishVerificationResults: true,
ProviderVersion: "<GIT_COMMIT>"
})
Ruby
- Pact Ruby documentation for all the verification options.
spec/pact_helper.rb
Pact.service_provider "<Your provider name here>" do
app_version ENV['GIT_COMMIT']
publish_verification_results ENV['CI'] == 'true'
honours_pacts_from_pact_broker do
pact_broker_base_url "https://<YOUR_BROKER>.pactflow.io", { token: "<TOKEN>" }
end
end
.NET
PactNet documentation for all the pact verification options.
Provider verification options
var ProviderVersion = Environment.GetEnvironmentVariable("GIT_COMMIT");
var PublishVerificationResults = "true".Equals(Environment.GetEnvironmentVariable("CI"));
var config = new PactVerifierConfig
{
ProviderVersion, //NOTE: Setting a provider version is required for publishing verification results
PublishVerificationResults
};
IPactVerifier pactVerifier = new PactVerifier(config);
pactVerifier
.ServiceProvider("<Your provider name here>", "http://your-test-provider-url")
.PactBroker("https://<YOUR_BROKER>.pactflow.io", uriOptions: new PactUriOptions("<TOKEN>"))
.Verify();
Docker
docker-compose-verify.yml
version: "3"
services:
api:
image: "your image"
expose:
- "9292"
pact_verifier:
image: pactfoundation/pact-cli:latest
depends_on:
- api
environment:
- PACT_BROKER_BASE_URL="https://<YOUR_BROKER>.pactflow.io"
- PACT_BROKER_TOKEN="<TOKEN>"
command: >
verify
--provider-base-url http://api:9292
--provider "<Your provider name here>"
To run
docker-compose -f docker-compose-verify.yml up \
--build --abort-on-container-exit --exit-code-from pact_verifier