Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,10 @@
<name>MatrixNeoKozak</name>
<email>matrixneo2026@tutamail.com</email>
</contributor>
<contributor>
<name>Vladimir Babin</name>
<email>vovababin@gmail.com</email>
</contributor>
</contributors>
<dependencies>
<!-- this includes httpclient as dependency -->
Expand Down
5 changes: 4 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="5.6.0" date="September xx, 2026" description="Chrome/Edge 153, Bugfixes">
<action type="add" dev="rbri" due-to="Vladimir Babin" issue="#91">
MockWebConnection is now Serializable.
</action>
<action type="update" dev="rbri">
websocket-client: jetty updated to 12.1.13.
</action>
Expand Down Expand Up @@ -14230,7 +14233,7 @@
<action type="fix" dev="asashour" issue="470" due-to="Bruce Chapman">
HtmlPage.clone() doesn't share idMap_ with original (#1708397).
</action>
<action type="add" dev="asashour" issue="91" system="features">
<action type="add" dev="asashour" issue="#91" system="features">
ClickableElement: Add dblClick() (#1385295).
</action>
<action type="add" dev="asashour" issue="364">
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/htmlunit/MockWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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<NameValuePair> 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<NameValuePair> headers) {
Expand Down Expand Up @@ -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);
}
}
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/org/htmlunit/MockWebConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
+ "<html>\n"
+ "<head>\n"
+ " <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>\n"
+ " <title>Serialized</title>\n"
+ "</head>\n"
+ "<body>\u00A3</body>\n"
+ "</html>";

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());
}
}