use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.
the class DirectDSLConsumerPactTest 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, (mockServer, context) -> {
Map expectedResponse = new HashMap();
expectedResponse.put("hello", "harry");
try {
assertEquals(new ProviderClient(mockServer.getUrl()).hello("{\"name\": \"harry\"}"), expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
use of au.com.dius.pact.consumer.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, contentType).toPact().asRequestResponsePact().component1();
PactVerificationResult result = runConsumerTest(pact, new MockProviderConfig("localhost", 0, PactSpecVersion.V3), (mockServer, context) -> {
try {
URL url = new URL(mockServer.getUrl() + path);
String response = post(url, contentType, requestBody);
assertEquals(expectedResponseBody, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
use of au.com.dius.pact.consumer.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.V3);
PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, (mockServer, context) -> {
try {
Assert.assertEquals(expectedResponse, new ConsumerClient(mockServer.getUrl()).post(path, body, ContentType.APPLICATION_JSON));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
return true;
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.
the class MimeTypeTest method runTest.
private void runTest(BasePact pact, final String body, final String expectedResponse, final ContentType mimeType) {
MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
try {
assertEquals(new ConsumerClient(mockServer.getUrl()).postBody("/hello", body, mimeType), expectedResponse);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
return true;
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
use of au.com.dius.pact.consumer.model.MockProviderConfig in project pact-jvm by DiUS.
the class PactQueryParameterTest method verifyRequestMatches.
private void verifyRequestMatches(BasePact pact, String fullPath) {
MockProviderConfig config = MockProviderConfig.createDefault();
PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
String uri = mockServer.getUrl() + fullPath;
Request.get(uri).execute().handleResponse(httpResponse -> {
String content = EntityUtils.toString(httpResponse.getEntity());
if (httpResponse.getCode() == 500) {
Map map = new ObjectMapper().readValue(content, Map.class);
Assert.fail((String) map.get("error"));
}
return null;
});
return true;
});
if (result instanceof PactVerificationResult.Error) {
Throwable error = ((PactVerificationResult.Error) result).getError();
if (error instanceof RuntimeException) {
throw (RuntimeException) error;
} else {
throw new RuntimeException(error);
}
}
assertThat(result, is(instanceOf(PactVerificationResult.Ok.class)));
}
Aggregations