use of org.jsoup.Connection in project jsoup by jhy.
the class UrlConnectTest method postHtmlFile.
/**
* Test fetching a form, and submitting it with a file attached.
*/
@Test
public void postHtmlFile() throws IOException {
Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get();
FormElement form = index.select("[name=tidy]").forms().get(0);
Connection post = form.submit();
File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html");
FileInputStream stream = new FileInputStream(uploadFile);
Connection.KeyVal fileData = post.data("_file");
fileData.value("check.html");
fileData.inputStream(stream);
Connection.Response res;
try {
res = post.execute();
} finally {
stream.close();
}
Document out = res.parse();
assertTrue(out.text().contains("HTML Tidy Complete"));
}
use of org.jsoup.Connection in project jsoup by jhy.
the class UrlConnectTest method ignores200WithNoContentExceptionIfSoConfigured.
@Test
public void ignores200WithNoContentExceptionIfSoConfigured() throws IOException {
Connection con = Jsoup.connect("http://direct.infohound.net/tools/200-no-content.pl").ignoreHttpErrors(true);
Connection.Response res = con.execute();
Document doc = res.parse();
assertEquals(200, res.statusCode());
assertEquals("All Good", res.statusMessage());
}
use of org.jsoup.Connection in project jsoup by jhy.
the class FormElementTest method createsSubmitableConnection.
@Test
public void createsSubmitableConnection() {
String html = "<form action='/search'><input name='q'></form>";
Document doc = Jsoup.parse(html, "http://example.com/");
doc.select("[name=q]").attr("value", "jsoup");
FormElement form = ((FormElement) doc.select("form").first());
Connection con = form.submit();
assertEquals(Connection.Method.GET, con.request().method());
assertEquals("http://example.com/search", con.request().url().toExternalForm());
List<Connection.KeyVal> dataList = (List<Connection.KeyVal>) con.request().data();
assertEquals("q=jsoup", dataList.get(0).toString());
doc.select("form").attr("method", "post");
Connection con2 = form.submit();
assertEquals(Connection.Method.POST, con2.request().method());
}
use of org.jsoup.Connection in project jsoup by jhy.
the class FormElementTest method actionWithNoBaseUri.
@Test
public void actionWithNoBaseUri() {
String html = "<form><input name='q'></form>";
Document doc = Jsoup.parse(html);
FormElement form = ((FormElement) doc.select("form").first());
boolean threw = false;
try {
Connection con = form.submit();
} catch (IllegalArgumentException e) {
threw = true;
assertEquals("Could not determine a form action URL for submit. Ensure you set a base URI when parsing.", e.getMessage());
}
assertTrue(threw);
}
use of org.jsoup.Connection in project jsoup by jhy.
the class UrlConnectTest method proxyGetAndSet.
@Test
public void proxyGetAndSet() throws IOException {
String url = "https://jsoup.org";
// invalid
Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 8889));
final Connection con = Jsoup.connect(url).proxy(proxy);
assert con.request().proxy() == proxy;
// disable
con.request().proxy(null);
Document doc = con.get();
// would fail if actually went via proxy
assertTrue(doc.title().contains("jsoup"));
}
Aggregations