Conversation
|
|
||
| @Test | ||
| public void bufferedParseWorksWhenCharsetDetectionFullyReadsResponse() throws IOException { | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp(); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, to fix a deprecated method invocation, you replace it with the recommended, non-deprecated alternative while preserving the observable behavior of the code. For jsoup Connection.Response, bufferUp() was used to fully read and buffer the response body so that multiple parses or charset detection logic would work consistently. Modern jsoup exposes higher-level parsing APIs (e.g., parse(), streamParser().complete()) that implicitly handle buffering without requiring an explicit bufferUp() call.
For this specific test in src/test/java/org/jsoup/integration/ConnectTest.java, the only direct usage of the deprecated API in the shown snippet is in bufferedParseWorksWhenCharsetDetectionFullyReadsResponse, at line 427:
Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp();Elsewhere in the same file, the test bufferedStreamParserWorksWhenCharsetDetectionFullyReadsResponse already uses res.streamParser().complete() to force a streaming parse and validate charset behavior without calling bufferUp(). We can mirror that pattern here: keep res as the result of execute() only (dropping .bufferUp()), and then parse via res.streamParser().complete() to force a full read and DOM creation. The assertions can remain exactly the same, because the Document produced by the streaming parser is equivalent for this test case. No new imports are needed; StreamParser is already imported.
Concretely:
- In
bufferedParseWorksWhenCharsetDetectionFullyReadsResponse, change the creation ofresto drop.bufferUp(). - Change
Document doc = res.parse();toDocument doc = res.streamParser().complete();. - Leave all other lines in the file as-is.
This removes the deprecated bufferUp() call while maintaining the intended behavior of validating charset detection and base URL handling.
| @@ -424,9 +424,9 @@ | ||
|
|
||
| @Test | ||
| public void bufferedParseWorksWhenCharsetDetectionFullyReadsResponse() throws IOException { | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp(); | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute(); | ||
|
|
||
| Document doc = res.parse(); | ||
| Document doc = res.streamParser().complete(); | ||
| assertEquals("UTF-8", res.charset()); | ||
| assertEquals("http://example.com/foo.jpg", doc.expectFirst("img").absUrl("src")); | ||
| } |
| @Test | ||
| public void bufferedStreamParserWorksWhenCharsetDetectionFullyReadsResponse() throws IOException { | ||
| // https://github.com/jhy/jsoup/issues/2483 | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp(); |
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, to fix deprecated Response.bufferUp() usage, you should replace it with the non-deprecated way of obtaining a buffered body or a parsed Document. For jsoup, calling Response.parse() already consumes and buffers the body internally, and then Response.parse() can be called multiple times safely without needing bufferUp().
In this specific test bufferedStreamParserWorksWhenCharsetDetectionFullyReadsResponse, the goal is to ensure that charset detection that fully reads the response still allows a streaming parse to complete correctly. The current code:
Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp();
Document doc = res.streamParser().complete();can be rewritten so that the buffering side-effect comes from res.parse(), which is not deprecated, and then the streaming parser is exercised on the same response. This keeps semantics as close as possible to the original intent: the response body is fully read once (via parse()), and then the stream parser is run:
- Execute the request to get
res. - Call
res.parse()to fully read and detect charset (this replacesbufferUp()). - Then call
res.streamParser().complete()and perform the same assertions.
No new imports are required; we only change the body of this one test method.
Concretely, in src/test/java/org/jsoup/integration/ConnectTest.java, around line 437, replace the execute().bufferUp() call with execute() followed by a res.parse() call on the next line, leaving the rest of the test unchanged.
| @@ -434,7 +434,9 @@ | ||
| @Test | ||
| public void bufferedStreamParserWorksWhenCharsetDetectionFullyReadsResponse() throws IOException { | ||
| // https://github.com/jhy/jsoup/issues/2483 | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute().bufferUp(); | ||
| Connection.Response res = Jsoup.connect(FileServlet.urlTo("/htmltests/charset-base.html")).execute(); | ||
| // fully read and detect charset, replacing deprecated bufferUp() | ||
| res.parse(); | ||
|
|
||
| Document doc = res.streamParser().complete(); | ||
| assertEquals("UTF-8", res.charset()); |
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )