diff --git a/src/test/java/org/jsoup/parser/StreamParserTest.java b/src/test/java/org/jsoup/parser/StreamParserTest.java index f81bdb0ffc..4472683dd9 100644 --- a/src/test/java/org/jsoup/parser/StreamParserTest.java +++ b/src/test/java/org/jsoup/parser/StreamParserTest.java @@ -1,7 +1,10 @@ package org.jsoup.parser; +import org.jsoup.Connection; +import org.jsoup.Jsoup; import org.jsoup.helper.DataUtil; import org.jsoup.integration.ParseTest; +import org.jsoup.integration.servlets.FileServlet; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; @@ -17,6 +20,7 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.*; @@ -340,6 +344,36 @@ private static CharacterReader getReader(StreamParser streamer) { assertTrue(isClosed(streamer)); } + @Test void canCleanlyConsumePortionOfUrl() throws IOException { + // test that we can get just the head section of large.html, and only read the minimum required from the URL + String url = FileServlet.urlTo("/htmltests/large.html"); // 280 K + + AtomicReference seenPercent = new AtomicReference<>(0.0f); + StreamParser parserRef; + + Connection con = Jsoup.connect(url) + .onResponseProgress((processed, total, percent, response) -> { + System.out.println("Processed: " + processed + " Total: " + total + " Percent: " + percent); + seenPercent.set(percent); + }); + + Connection.Response response = con.execute(); + try (StreamParser parser = response.streamParser()) { + parserRef = parser; + // get the head section + Element head = parser.selectFirst("head"); + Element title = head.expectFirst("title"); + assertEquals("Large HTML", title.text()); + } + // now that we've left the try, the stream parser and the response bodystream should be closed + assertTrue(isClosed(parserRef)); + + // test that we didn't read all of the stream + assertTrue(seenPercent.get() > 0.0f); + assertTrue(seenPercent.get() < 100.0f); + // 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. + } + // Fragments @Test