diff --git a/pom.xml b/pom.xml
index 524002925d..5dcaaf4212 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1423,6 +1423,10 @@
MatrixNeoKozak
matrixneo2026@tutamail.com
+
+ Vladimir Babin
+ vovababin@gmail.com
+
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e73edd61f..2ec977cc34 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,9 @@
+
+ MockWebConnection is now Serializable.
+
websocket-client: jetty updated to 12.1.13.
@@ -14230,7 +14233,7 @@
HtmlPage.clone() doesn't share idMap_ with original (#1708397).
-
+
ClickableElement: Add dblClick() (#1385295).
diff --git a/src/main/java/org/htmlunit/MockWebConnection.java b/src/main/java/org/htmlunit/MockWebConnection.java
index 6699e22380..bf841c55e8 100644
--- a/src/main/java/org/htmlunit/MockWebConnection.java
+++ b/src/main/java/org/htmlunit/MockWebConnection.java
@@ -17,6 +17,9 @@
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -42,7 +45,7 @@
* @author Ahmed Ashour
* @author Ronald Brill
*/
-public class MockWebConnection implements WebConnection {
+public class MockWebConnection implements WebConnection, Serializable {
private static final Log LOG = LogFactory.getLog(MockWebConnection.class);
@@ -56,13 +59,13 @@ public class MockWebConnection implements WebConnection {
/**
* Contains the raw data configured for a response.
*/
- public static class RawResponseData {
+ public static class RawResponseData implements Serializable {
private final List headers_;
private final byte[] byteContent_;
private final String stringContent_;
private final int statusCode_;
private final String statusMessage_;
- private final Charset charset_;
+ private transient Charset charset_;
RawResponseData(final byte[] byteContent, final int statusCode, final String statusMessage,
final String contentType, final List headers) {
@@ -162,6 +165,19 @@ public String getStatusMessage() {
public Charset getCharset() {
return charset_;
}
+
+ private void writeObject(final ObjectOutputStream oos) throws IOException {
+ oos.defaultWriteObject();
+ oos.writeObject(charset_ == null ? null : charset_.name());
+ }
+
+ private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
+ ois.defaultReadObject();
+ final String charsetName = (String) ois.readObject();
+ if (charsetName != null) {
+ charset_ = Charset.forName(charsetName);
+ }
+ }
}
/**
diff --git a/src/test/java/org/htmlunit/MockWebConnectionTest.java b/src/test/java/org/htmlunit/MockWebConnectionTest.java
index 7936077ced..2af81e86d2 100644
--- a/src/test/java/org/htmlunit/MockWebConnectionTest.java
+++ b/src/test/java/org/htmlunit/MockWebConnectionTest.java
@@ -15,7 +15,14 @@
package org.htmlunit;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collections;
+
+import org.apache.commons.io.IOUtils;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.util.MimeType;
import org.junit.jupiter.api.Test;
@@ -49,4 +56,58 @@ public void charset() throws Exception {
final HtmlPage page = client.getPage(URL_FIRST);
assertEquals("\u00A3", page.getBody().asNormalizedText());
}
+
+ /**
+ * @throws Exception if an error occurs
+ */
+ @Test
+ public void serialization() throws Exception {
+ final String html = DOCTYPE_HTML
+ + "\n"
+ + "\n"
+ + " \n"
+ + " Serialized\n"
+ + "\n"
+ + "\u00A3\n"
+ + "";
+
+ final MockWebConnection webConnection = new MockWebConnection();
+ webConnection.setResponse(URL_FIRST, html, MimeType.TEXT_HTML, UTF_8);
+ webConnection.setResponse(URL_SECOND, new byte[] {1, 2, 3}, 404, "Not Found",
+ MimeType.APPLICATION_OCTET_STREAM, null);
+ webConnection.setDefaultResponse("default");
+ webConnection.setThrowable(URL_THIRD, new IOException("boom"));
+
+ final WebClient client = getWebClient();
+ client.setWebConnection(webConnection);
+ client.getPage(URL_FIRST);
+
+ final MockWebConnection copy = clone(webConnection);
+
+ assertEquals(1, copy.getRequestCount());
+ assertEquals(Collections.singletonList(URL_FIRST), copy.getRequestedUrls());
+ assertEquals(URL_FIRST, copy.getLastWebRequest().getUrl());
+
+ client.setWebConnection(copy);
+ final HtmlPage page = client.getPage(URL_FIRST);
+ assertEquals("\u00A3", page.getBody().asNormalizedText());
+ assertEquals("Serialized", page.getTitleText());
+
+ final WebResponse second = copy.getResponse(new WebRequest(URL_SECOND));
+ assertEquals(404, second.getStatusCode());
+ assertEquals("Not Found", second.getStatusMessage());
+ assertArrayEquals(new byte[] {1, 2, 3}, IOUtils.toByteArray(second.getContentAsStream()));
+
+ final WebResponse other = copy.getResponse(new WebRequest(new URL("http://localhost/other")));
+ assertEquals("default", other.getContentAsString());
+
+ try {
+ copy.getResponse(new WebRequest(URL_THIRD));
+ fail("IOException expected");
+ }
+ catch (final IOException e) {
+ assertEquals("boom", e.getMessage());
+ }
+ assertEquals(5, copy.getRequestCount());
+ }
}