diff --git a/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PactBuilderTest.java b/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PactBuilderTest.java index 5bfe4384a..f8df901ab 100644 --- a/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PactBuilderTest.java +++ b/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PactBuilderTest.java @@ -7,7 +7,6 @@ import au.com.dius.pact.core.model.V4Interaction; import au.com.dius.pact.core.model.V4Pact; import au.com.dius.pact.core.model.annotations.Pact; -import au.com.dius.pact.core.model.messaging.Message; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.ClassicHttpResponse; import org.hamcrest.core.Is; @@ -16,13 +15,11 @@ import java.io.IOException; import java.util.HashMap; -import java.util.List; import java.util.Map; +import static au.com.dius.pact.consumer.dsl.Matchers.notEmpty; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; @ExtendWith(PactConsumerTestExt.class) @PactTestFor(providerName = "v4_test_provider") @@ -41,7 +38,9 @@ public V4Pact httpInteraction(PactBuilder builder) { .method("GET")) .willRespondWith(responseBuilder -> responseBuilder .status(200) - .body("{\"responsetest\": true, \"version\": \"v3\"}")) + .body("{\"responsetest\": true, \"version\": \"v3\"}") + .header("test", notEmpty("Example")) + ) .comment("This is also a comment"); }) .comment("This is also a comment") @@ -55,6 +54,7 @@ void runHttpTest(MockServer mockServer) throws IOException { assertThat(httpResponse.getCode(), is(200)); assertThat(new String(httpResponse.getEntity().getContent().readAllBytes()), is(equalTo("{\"responsetest\": true, \"version\": \"v3\"}"))); + assertThat(httpResponse.containsHeader("test"), is(true)); } @Pact(consumer = "v4_test_consumer") diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/Matchers.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/Matchers.kt index 10d6d0bc7..47e98aacb 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/Matchers.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/Matchers.kt @@ -51,7 +51,8 @@ data class TimestampMatcher( val pattern: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern, val dateTimeValue: String? ) : Matcher( - dateTimeValue ?: DateTimeFormatter.ofPattern(pattern).format(Instant.ofEpochMilli(DATE_2000).atOffset(ZoneOffset.UTC)), + dateTimeValue ?: DateTimeFormatter.ofPattern(pattern) + .format(Instant.ofEpochMilli(DATE_2000).atOffset(ZoneOffset.UTC)), au.com.dius.pact.core.model.matchingrules.TimestampMatcher(pattern), if (dateTimeValue == null) DateTimeGenerator(pattern) else null ) @@ -89,6 +90,9 @@ object NullMatcher : Matcher(null, au.com.dius.pact.core.model.matchingrules.Nul data class ProviderStateMatcher(val expression: String, override val value: String) : Matcher(value, null, ProviderStateGenerator(expression)) +data class NotEmptyMatcher(override val value: Any?) : Matcher(value, + au.com.dius.pact.core.model.matchingrules.NotEmptyMatcher) + /** * Exception for handling invalid matchers */ @@ -317,4 +321,13 @@ object Matchers { fun fromProviderState(expression: String, value: String): Matcher { return ProviderStateMatcher(expression, value) } + + /** + * Matches if the value is not empty (empty string, null, or missing) + * @param value Example value + */ + @JvmStatic + fun notEmpty(example: String): Matcher { + return NotEmptyMatcher(example) + } }