Skip to content

Commit

Permalink
fix: fix parsing of date/time values with an offset ahead of UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
papiomytoglou committed May 2, 2024
1 parent ba9d517 commit d660fa9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/main/java/com/github/tomakehurst/wiremock/common/Urls.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.github.tomakehurst.wiremock.common.Strings.ordinalIndexOf;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;

import com.github.tomakehurst.wiremock.http.QueryParameter;
import com.google.common.collect.ImmutableListMultimap;
Expand All @@ -27,6 +28,7 @@
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -97,8 +99,19 @@ public static String urlToPathParts(URI uri) {
return nodeCount > 0 ? String.join("-", uriPathNodes) : "";
}

public static String decode(String encoded) {
return URLDecoder.decode(encoded, UTF_8);
private static String decode(String encoded) {
String decoded = URLDecoder.decode(encoded, UTF_8);
if (isISOOffsetDateTime(encoded)) return decoded.replace(' ', '+');
else return decoded;
}

private static boolean isISOOffsetDateTime(String encoded) {
try {
ISO_OFFSET_DATE_TIME.parse(encoded);
} catch (DateTimeParseException e) {
return false;
}
return true;
}

public static URL safelyCreateURL(String url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2023 Thomas Akehurst
* Copyright (C) 2014-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@

import com.github.tomakehurst.wiremock.http.QueryParameter;
import java.net.URI;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,6 +52,18 @@ public void supportsMultiValuedParameters() {
assertThat(params.get("param1").values(), hasItems("1", "2", "3"));
}

@Test
public void supportsZonedDateTimeParameters() {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2024-05-01T09:30:00.000Z");
params =
Urls.splitQuery(
URI.create(
"/thing?date=2024-05-01T10:30:00.000+01:00&date=2024-05-01T08:30:00.000-01:00&date=2024-05-01T09:30:00.000Z"));
for (QueryParameter queryParameter : params.values())
for (String parameterValue : queryParameter.values())
assert (zonedDateTime.isEqual(ZonedDateTime.parse(parameterValue)));
}

@Test
public void doesNotAttemptToDoubleDecodeSplitQueryString() {
URI url = URI.create("/thing?q=a%25b");
Expand Down

0 comments on commit d660fa9

Please sign in to comment.