Skip to content

Commit

Permalink
WebRequest custom serialization misses the handling of defaultRespons…
Browse files Browse the repository at this point in the history
…eContentCharset_ (issue #898)
  • Loading branch information
rbri committed Nov 13, 2024
1 parent db89887 commit 82c1292
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="4.7.0" date="November xx, 2024" description="Bugfixes">
<action type="add" dev="rbri" issue="#898">
WebRequest custom serialization misses the handling of defaultResponseContentCharset_.
</action>
<action type="add" dev="Marek Andreansky">
Class SilentIncorrectnessListener added.
</action>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ public String toString() {
private void writeObject(final ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeObject(charset_ == null ? null : charset_.name());
oos.writeObject(defaultResponseContentCharset_ == null ? null : defaultResponseContentCharset_.name());
}

private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
Expand All @@ -698,5 +699,9 @@ private void readObject(final ObjectInputStream ois) throws ClassNotFoundExcepti
if (charsetName != null) {
charset_ = Charset.forName(charsetName);
}
final String defaultResponseContentCharset = (String) ois.readObject();
if (defaultResponseContentCharset != null) {
defaultResponseContentCharset_ = Charset.forName(charsetName);
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/htmlunit/WebRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import static org.junit.Assert.assertNotNull;

import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.http.auth.BasicUserPrincipal;
import org.apache.http.auth.Credentials;
import org.junit.Test;
Expand Down Expand Up @@ -327,4 +329,21 @@ public void getRequestParametersFromUrlEncodedBodyPost() throws Exception {

assertEquals(0, request.getRequestParameters().size());
}

/**
* @throws Exception if an error occurs
*/
@Test
public void serialization() throws Exception {
final WebRequest request = new WebRequest(URL_FIRST);
request.setCharset(StandardCharsets.UTF_8);
request.setDefaultResponseContentCharset(StandardCharsets.US_ASCII);

final byte[] bytes = SerializationUtils.serialize(request);
final WebRequest deserialized = (WebRequest) SerializationUtils.deserialize(bytes);

assertEquals(URL_FIRST, deserialized.getUrl());
assertEquals(StandardCharsets.UTF_8, deserialized.getCharset());
assertEquals(StandardCharsets.US_ASCII, deserialized.getDefaultResponseContentCharset());
}
}

0 comments on commit 82c1292

Please sign in to comment.