feat: support java.time.OffsetDateTime in post data (#1624)
Fixes https://github.com/microsoft/playwright-java/issues/1623
This commit is contained in:
Родитель
7fa8081032
Коммит
7a12897be4
|
@ -35,10 +35,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.nio.file.Path;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -79,6 +76,7 @@ class Serialization {
|
|||
static final Gson jsonDataSerializer = new GsonBuilder().disableHtmlEscaping()
|
||||
.registerTypeAdapter(Date.class, new DateSerializer())
|
||||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer())
|
||||
.serializeNulls().create();
|
||||
|
||||
static SerializedError serializeError(Throwable e) {
|
||||
|
@ -519,6 +517,13 @@ class Serialization {
|
|||
}
|
||||
}
|
||||
|
||||
private static class OffsetDateTimeSerializer implements JsonSerializer<OffsetDateTime> {
|
||||
@Override
|
||||
public JsonElement serialize(OffsetDateTime src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(dateFormat.format(Date.from(src.toInstant())));
|
||||
}
|
||||
}
|
||||
|
||||
private static class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
|
||||
@Override
|
||||
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
|
|
@ -28,6 +28,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.nio.file.Path;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -521,6 +522,16 @@ public class TestBrowserContextFetch extends TestBase {
|
|||
new String(req.get().postBody));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportOffsetDateTimeInData() throws ExecutionException, InterruptedException {
|
||||
APIRequestContext request = playwright.request().newContext();
|
||||
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2024-07-10T10:15:30-08:00");
|
||||
Future<Server.Request> serverRequest = server.futureRequest("/empty.html");
|
||||
request.get(server.EMPTY_PAGE, RequestOptions.create().setData(mapOf("date", offsetDateTime)));
|
||||
byte[] body = serverRequest.get().postBody;
|
||||
assertEquals("{\"date\":\"2024-07-10T18:15:30.000Z\"}", new String(body));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {
|
||||
Future<Server.Request> req = server.futureRequest("/empty.html");
|
||||
|
|
Загрузка…
Ссылка в новой задаче