From 05dddadcdc30639a414f39e63a2cddd303453960 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Fri, 12 Jun 2020 12:49:06 +1000 Subject: [PATCH] fix: just use the DateTimeFormatter to parse any datetime value --- .../junit5/DateTimeWithTimezoneTest.groovy | 42 +++++++++++++++++++ .../pact/core/matchers/MatcherExecutor.kt | 4 +- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/DateTimeWithTimezoneTest.groovy diff --git a/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/DateTimeWithTimezoneTest.groovy b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/DateTimeWithTimezoneTest.groovy new file mode 100644 index 0000000000..c25139e133 --- /dev/null +++ b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/DateTimeWithTimezoneTest.groovy @@ -0,0 +1,42 @@ +package au.com.dius.pact.consumer.junit5 + +import au.com.dius.pact.consumer.MockServer +import au.com.dius.pact.consumer.dsl.PactDslJsonBody +import au.com.dius.pact.consumer.dsl.PactDslWithProvider +import au.com.dius.pact.core.model.RequestResponsePact +import au.com.dius.pact.core.model.annotations.Pact +import org.apache.http.HttpResponse +import org.apache.http.client.fluent.Request +import org.apache.http.entity.StringEntity +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +import java.time.ZonedDateTime +import java.time.format.DateTimeFormatter + +@ExtendWith(PactConsumerTestExt) +@PactTestFor(providerName = 'ProviderWithDateTime') +class DateTimeWithTimezoneTest { + @Pact(consumer = 'Consumer') + RequestResponsePact pactWithTimezone(PactDslWithProvider builder) { + builder + .uponReceiving('a request with some datetime info') + .method('POST') + .path('/values') + .body(new PactDslJsonBody().datetime('datetime', "YYYY-MM-dd'T'HH:mm:ss.SSSxxx")) + .willRespondWith() + .status(200) + .body(new PactDslJsonBody().datetime('datetime', "YYYY-MM-dd'T'HH:mm:ss.SSSxxx")) + .toPact() + } + + @Test + void testFiles(MockServer mockServer) { + HttpResponse httpResponse = Request.Post("${mockServer.url}/values") + .body(new StringEntity('{"datetime": "' + + DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSSxxx").format(ZonedDateTime.now()) + + '"}', 'application/json', 'UTF-8')) + .execute().returnResponse() + assert httpResponse.statusLine.statusCode == 200 + } +} diff --git a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt index b827fbaa40..a899f83038 100755 --- a/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt +++ b/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt @@ -22,8 +22,6 @@ import org.w3c.dom.Text import java.math.BigDecimal import java.math.BigInteger import java.text.ParseException -import java.time.ZoneId -import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.time.format.DateTimeParseException @@ -323,7 +321,7 @@ fun matchDateTime( emptyList() } else { try { - ZonedDateTime.parse(safeToString(actual), DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault())) + DateTimeFormatter.ofPattern(pattern).parse(safeToString(actual)) emptyList() } catch (e: DateTimeParseException) { listOf(mismatchFactory.create(expected, actual,