Skip to content

Commit

Permalink
Fixes wiremock#1882 - Enable multi value pattern matching for DateTime
Browse files Browse the repository at this point in the history
- Adding failing test cases
- Apply a fix for the failing test cases
  • Loading branch information
klaasdellschaft committed Jul 12, 2022
1 parent dcd881c commit c90c99c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private static ZonedDateTime parseZonedOrNull(String dateTimeString, DateTimePar

private static ZonedDateTime parseZonedOrNull(
String dateTimeString, List<DateTimeParser> parsers) {
if (parsers.isEmpty()) {
if (parsers.isEmpty() || dateTimeString == null) {
return null;
}

Expand All @@ -256,6 +256,10 @@ private static LocalDateTime parseLocalOrNull(String dateTimeString) {
}

private static LocalDateTime parseLocalOrNull(String dateTimeString, DateTimeParser parser) {
if (dateTimeString == null) {
return null;
}

try {
return parser != null
? parser.parseLocalDateTime(dateTimeString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ public void returnsContentTypeHeaderEncodingInCorrectCase() {
}

@Test
public void matchesOnLiteralZonedDate() {
public void matchesInRequestBodyOnLiteralZonedDate() {
stubFor(
post("/date")
.withRequestBody(matchingJsonPath("$.date", before("2021-10-11T00:00:00Z")))
Expand All @@ -836,6 +836,26 @@ public void matchesOnLiteralZonedDate() {
is(404));
}

@Test
public void matchesQueryParameterOnLiteralZonedDate() {
stubFor(
get(urlPathEqualTo("/match-query-parameter"))
.withQueryParam("date", before("2021-10-11T00:00:00Z"))
.willReturn(ok()));

assertThat(
testClient.get("/match-query-parameter?date=2021-06-22T23%3A59%3A59Z").statusCode(),
is(200));

assertThat(
testClient.get("/match-query-parameter?date=2121-06-22T23%3A59%3A59Z").statusCode(),
is(404));

assertThat(
testClient.get("/match-query-parameter").statusCode(),
is(404));
}

@Test
public void matchesOnNowOffsetDate() {
stubFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ public void matchesNowWithExpectedAndActualTruncated() {
assertFalse(matcher.match(bad.toString()).isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.after("2021-06-14T15:15:15Z");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.after("2021-01-01T00:00:00Z");
assertThat(matcher.match("1971-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("1921-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(-1.0));
assertThat(matcher.match("2020-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public void doesNotMatchWhenActualValueUnparseable() {
assertFalse(matcher.match("2021-06-01T15:15:blahsdfj123").isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.before("2021-06-14T15:15:15");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void doesNotMatchWhenExpectedValueUnparseable() {
StringValuePattern matcher = WireMock.before("2021-06-wrongstuff:15:15");
Expand All @@ -84,6 +90,7 @@ public void returnsAReasonableDistanceWhenNoMatchForZonedExpectedZonedActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00Z");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(-1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -92,6 +99,7 @@ public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(-1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand All @@ -100,6 +108,7 @@ public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedLocalActual() {
StringValuePattern matcher = WireMock.before("2021-01-01T00:00:00");
assertThat(matcher.match("2071-01-01T00:00:00").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(-1.0));
assertThat(matcher.match("2022-01-01T00:00:00").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ public void matchesActualInEpochTimeFormat() {
assertFalse(matcher.match(bad).isExactMatch());
}

@Test
public void doesNotMatchWhenActualValueIsNull() {
StringValuePattern matcher = WireMock.equalToDateTime("2021-06-14T12:13:14Z");
assertFalse(matcher.match(null).isExactMatch());
}

@Test
public void returnsAReasonableDistanceWhenNoMatchForLocalExpectedZonedActual() {
StringValuePattern matcher = WireMock.equalToDateTime("2021-01-01T00:00:00Z");
assertThat(matcher.match("2071-01-01T00:00:00Z").getDistance(), is(0.5));
assertThat(matcher.match("2121-01-01T00:00:00Z").getDistance(), is(1.0));
assertThat(matcher.match(null).getDistance(), is(-1.0));
assertThat(matcher.match("2022-01-01T00:00:00Z").getDistance(), is(0.01));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.matching;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.http.HttpHeader.absent;
import static com.github.tomakehurst.wiremock.http.HttpHeader.httpHeader;
import static com.github.tomakehurst.wiremock.http.QueryParameter.queryParam;
Expand All @@ -42,6 +42,12 @@ public void returnsNonMatchForPresentHeaderWhenRequiredAbsent() {
MultiValuePattern.absent().match(httpHeader("the-key", "the value")).isExactMatch());
}

@Test
public void returnsNonMatchForAbsentHeaderWhenRequiredBeforeNow() {
assertFalse(
MultiValuePattern.of(beforeNow()).match(HttpHeader.absent("any-key")).isExactMatch());
}

@Test
public void returnsExactMatchForPresentHeaderWhenRequiredPresent() {
assertTrue(
Expand Down

0 comments on commit c90c99c

Please sign in to comment.