|
1 | 1 | package org.jsoup.parser;
|
2 | 2 |
|
| 3 | +import org.jsoup.Connection; |
| 4 | +import org.jsoup.Jsoup; |
3 | 5 | import org.jsoup.helper.DataUtil;
|
4 | 6 | import org.jsoup.integration.ParseTest;
|
| 7 | +import org.jsoup.integration.servlets.FileServlet; |
5 | 8 | import org.jsoup.nodes.Document;
|
6 | 9 | import org.jsoup.nodes.Element;
|
7 | 10 | import org.jsoup.nodes.Node;
|
|
17 | 20 | import java.util.Iterator;
|
18 | 21 | import java.util.List;
|
19 | 22 | import java.util.NoSuchElementException;
|
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
20 | 24 |
|
21 | 25 | import static org.junit.jupiter.api.Assertions.*;
|
22 | 26 |
|
@@ -340,6 +344,36 @@ private static CharacterReader getReader(StreamParser streamer) {
|
340 | 344 | assertTrue(isClosed(streamer));
|
341 | 345 | }
|
342 | 346 |
|
| 347 | + @Test void canCleanlyConsumePortionOfUrl() throws IOException { |
| 348 | + // test that we can get just the head section of large.html, and only read the minimum required from the URL |
| 349 | + String url = FileServlet.urlTo("/htmltests/large.html"); // 280 K |
| 350 | + |
| 351 | + AtomicReference<Float> seenPercent = new AtomicReference<>(0.0f); |
| 352 | + StreamParser parserRef; |
| 353 | + |
| 354 | + Connection con = Jsoup.connect(url) |
| 355 | + .onResponseProgress((processed, total, percent, response) -> { |
| 356 | + System.out.println("Processed: " + processed + " Total: " + total + " Percent: " + percent); |
| 357 | + seenPercent.set(percent); |
| 358 | + }); |
| 359 | + |
| 360 | + Connection.Response response = con.execute(); |
| 361 | + try (StreamParser parser = response.streamParser()) { |
| 362 | + parserRef = parser; |
| 363 | + // get the head section |
| 364 | + Element head = parser.selectFirst("head"); |
| 365 | + Element title = head.expectFirst("title"); |
| 366 | + assertEquals("Large HTML", title.text()); |
| 367 | + } |
| 368 | + // now that we've left the try, the stream parser and the response bodystream should be closed |
| 369 | + assertTrue(isClosed(parserRef)); |
| 370 | + |
| 371 | + // test that we didn't read all of the stream |
| 372 | + assertTrue(seenPercent.get() > 0.0f); |
| 373 | + assertTrue(seenPercent.get() < 100.0f); |
| 374 | + // not sure of a good way to assert the bufferedInputReader buf (as held by ConstrainableInputStream in Response.BodyStream) is null. But it is via StreamParser.close. |
| 375 | + } |
| 376 | + |
343 | 377 | // Fragments
|
344 | 378 |
|
345 | 379 | @Test
|
|
0 commit comments