Search in sources :

Example 1 with RequestResponsePact

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();
}
Also used : PactXmlBuilder(au.com.dius.pact.consumer.xml.PactXmlBuilder) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 2 with RequestResponsePact

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;
}
Also used : RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 3 with RequestResponsePact

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)));
}
Also used : ProviderClient(au.com.dius.pact.consumer.junit.exampleclients.ProviderClient) HashMap(java.util.HashMap) MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 4 with RequestResponsePact

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)));
}
Also used : MockProviderConfig(au.com.dius.pact.consumer.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) URL(java.net.URL)

Example 5 with RequestResponsePact

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;
}
Also used : RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Aggregations

RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)15 DslPart (au.com.dius.pact.consumer.dsl.DslPart)8 Pact (au.com.dius.pact.core.model.annotations.Pact)7 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)5 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)4 MockProviderConfig (au.com.dius.pact.consumer.model.MockProviderConfig)3 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)2 MatchingRules (au.com.dius.pact.core.model.matchingrules.MatchingRules)2 PactMismatchesException (au.com.dius.pact.consumer.PactMismatchesException)1 PactDslRootValue (au.com.dius.pact.consumer.dsl.PactDslRootValue)1 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)1 ProviderClient (au.com.dius.pact.consumer.junit.exampleclients.ProviderClient)1 PactXmlBuilder (au.com.dius.pact.consumer.xml.PactXmlBuilder)1 MatchingRule (au.com.dius.pact.core.model.matchingrules.MatchingRule)1 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)1