Skip to content

Commit b4b3fd1

Browse files
committed
Added test of partial fetch in Stream Parser
1 parent 9ba6dc7 commit b4b3fd1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/test/java/org/jsoup/parser/StreamParserTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.jsoup.parser;
22

3+
import org.jsoup.Connection;
4+
import org.jsoup.Jsoup;
35
import org.jsoup.helper.DataUtil;
46
import org.jsoup.integration.ParseTest;
7+
import org.jsoup.integration.servlets.FileServlet;
58
import org.jsoup.nodes.Document;
69
import org.jsoup.nodes.Element;
710
import org.jsoup.nodes.Node;
@@ -17,6 +20,7 @@
1720
import java.util.Iterator;
1821
import java.util.List;
1922
import java.util.NoSuchElementException;
23+
import java.util.concurrent.atomic.AtomicReference;
2024

2125
import static org.junit.jupiter.api.Assertions.*;
2226

@@ -340,6 +344,36 @@ private static CharacterReader getReader(StreamParser streamer) {
340344
assertTrue(isClosed(streamer));
341345
}
342346

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+
343377
// Fragments
344378

345379
@Test

0 commit comments

Comments
 (0)