From 1108ef39ec34674d0d9214076e72af25e2824ced Mon Sep 17 00:00:00 2001 From: Vladimir Babin Date: Sun, 20 Sep 2026 14:04:36 +0300 Subject: [PATCH 1/2] MockWebConnection: implement Serializable Closes #91 --- src/changes/changes.xml | 5 +- .../java/org/htmlunit/MockWebConnection.java | 22 ++++++- .../org/htmlunit/MockWebConnectionTest.java | 61 +++++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9e73edd61f1..2ec977cc343 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 6699e223800..bf841c55e8d 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 7936077ced6..2af81e86d25 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()); + } } From 2a5812ac1b679f1feb783c7317a8f3a31a946e4b Mon Sep 17 00:00:00 2001 From: Vladimir Babin Date: Thu, 24 Sep 2026 07:42:58 +0300 Subject: [PATCH 2/2] Add Vladimir Babin to contributors in pom.xml --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index 524002925d3..5dcaaf42128 100644 --- a/pom.xml +++ b/pom.xml @@ -1423,6 +1423,10 @@ MatrixNeoKozak matrixneo2026@tutamail.com + + Vladimir Babin + vovababin@gmail.com +