Skip to content

Commit

Permalink
Added test of partial fetch in Stream Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Jul 8, 2024
1 parent 9ba6dc7 commit b4b3fd1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/test/java/org/jsoup/parser/StreamParserTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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<Float> 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
Expand Down

0 comments on commit b4b3fd1

Please sign in to comment.