use of au.com.dius.pact.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class TodoXmlTest method projects.
// body: <?xml version="1.0" encoding="UTF-8"?>
// <projects foo="bar">
// <project id="1" name="Project 1" due="2016-02-11T09:46:56.023Z">
// <tasks>
// <task id="1" name="Do the laundry" done="true"/>
// <task id="2" name="Do the dishes" done="false"/>
// <task id="3" name="Do the backyard" done="false"/>
// <task id="4" name="Do nothing" done="false"/>
// </tasks>
// </project>
// <project/>
// </projects>
@Pact(provider = "TodoProvider", consumer = "TodoConsumer")
public RequestResponsePact projects(PactDslWithProvider builder) {
return builder.given("i have a list of projects").uponReceiving("a request for projects in XML").path("/projects").query("from=today").headers(mapOf("Accept", "application/xml")).willRespondWith().headers(mapOf("Content-Type", "application/xml")).status(200).body(new PactXmlBuilder("projects", "http://some.namespace/and/more/stuff").build(root -> {
root.setAttributes(mapOf("id", "1234"));
root.eachLike("project", 2, mapOf("id", integer(), "type", "activity", "name", string("Project 1"), "due", timestamp("yyyy-MM-dd'T'HH:mm:ss.SSSX", "2016-02-11T09:46:56.023Z")), project -> {
project.appendElement("tasks", Collections.emptyMap(), task -> {
task.eachLike("task", 5, mapOf("id", integer(), "name", string("Task 1"), "done", bool(true)));
});
});
})).toPact();
}
use of au.com.dius.pact.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class Issue1176Test method validCredentials.
@Pact(provider = "config-service", consumer = "test-integration")
public RequestResponsePact validCredentials(PactDslWithProvider builder) {
Map<String, String> headers = Collections.singletonMap("Content-Type", ContentType.TEXT.toString());
RequestResponsePact pact = builder.uponReceiving("valid configuration").path(CONFIG_URL).method("GET").headers(headers).body("text").willRespondWith().status(200).body("{\"data\":\"\", \"status\":\"success\"}").toPact();
return pact;
}
use of au.com.dius.pact.core.model.RequestResponsePact 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.core.model.RequestResponsePact 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.core.model.RequestResponsePact in project pact-jvm by DiUS.
the class DefaultValuesTest method createPact.
@Pact(consumer = "DefaultValuesConsumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
RequestResponsePact pact = builder.given("status200").uponReceiving("Get object").path("/path").willRespondWith().status(200).uponReceiving("Download").path("/path2").matchQuery("source_filename", "[\\S\\s]+[\\S]+", "filename").willRespondWith().status(200).toPact();
assertThat(pact.getInteractions().get(0).asSynchronousRequestResponse().getRequest().getHeaders(), hasEntry("Content-Type", Collections.singletonList("application/json")));
assertThat(pact.getInteractions().get(1).asSynchronousRequestResponse().getRequest().getHeaders(), hasEntry("Content-Type", Collections.singletonList("application/json")));
return pact;
}
Aggregations