use of au.com.dius.pact.model.MockProviderConfig in project pact-jvm by DiUS.
the class PactDefectTest method test.
private void test(final String requestBody, final String expectedResponseBody, final String contentType) {
RequestResponsePact pact = ConsumerPactBuilder.consumer("ping_consumer").hasPactWith("ping_provider").uponReceiving("Ping with " + contentType).path(path).method(method).body(requestBody, contentType).willRespondWith().status(200).body(expectedResponseBody).toPact();
PactVerificationResult result = runConsumerTest(pact, new MockProviderConfig("localhost", 0, PactSpecVersion.V2), new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
try {
URL url = new URL(mockServer.getUrl() + path);
String response = post(url, contentType, requestBody);
assertEquals(expectedResponseBody, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
Assert.assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
use of au.com.dius.pact.model.MockProviderConfig in project pact-jvm by DiUS.
the class PactTest method testPact.
@Test
public void testPact() {
RequestResponsePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving("a request to say Hello").path("/hello").method("POST").body("{\"name\": \"harry\"}").willRespondWith().status(200).body("{\"hello\": \"harry\"}").toPact();
MockProviderConfig config = MockProviderConfig.createDefault();
PactVerificationResult result = runConsumerTest(pact, config, new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
Map expectedResponse = new HashMap();
expectedResponse.put("hello", "harry");
assertEquals(expectedResponse, new ConsumerClient(mockServer.getUrl()).post("/hello", "{\"name\": \"harry\"}", ContentType.APPLICATION_JSON));
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
use of au.com.dius.pact.model.MockProviderConfig in project pact-jvm by DiUS.
the class ConsumerPactTestMk2 method testPact.
@Test
public void testPact() throws Throwable {
RequestResponsePact pact = createPact(ConsumerPactBuilder.consumer(consumerName()).hasPactWith(providerName()));
final MockProviderConfig config = MockProviderConfig.createDefault(getSpecificationVersion());
PactVerificationResult result = runConsumerTest(pact, config, this::runTest);
if (!result.equals(PactVerificationResult.Ok.INSTANCE)) {
if (result instanceof PactVerificationResult.Error) {
PactVerificationResult.Error error = (PactVerificationResult.Error) result;
if (error.getMockServerState() != PactVerificationResult.Ok.INSTANCE) {
throw new AssertionError("Pact Test function failed with an exception, possibly due to " + error.getMockServerState(), ((PactVerificationResult.Error) result).getError());
} else {
throw new AssertionError("Pact Test function failed with an exception: " + error.getError().getMessage(), error.getError());
}
} else {
throw new PactMismatchesException(result);
}
}
}
use of au.com.dius.pact.model.MockProviderConfig in project pact-jvm by DiUS.
the class MatchingTest method runTest.
private void runTest(PactDslResponse pactFragment, final String body, final Map expectedResponse, final String path) {
MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V2);
PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
try {
Assert.assertEquals(expectedResponse, new ConsumerClient(config.url()).post(path, body, ContentType.APPLICATION_JSON));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
Assert.assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
Aggregations