Search in sources :

Example 26 with Connection

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"));
}
Also used : Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) File(java.io.File) FormElement(org.jsoup.nodes.FormElement) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 27 with Connection

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());
}
Also used : Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 28 with Connection

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());
}
Also used : Connection(org.jsoup.Connection) List(java.util.List) Test(org.junit.Test)

Example 29 with Connection

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);
}
Also used : Connection(org.jsoup.Connection) Test(org.junit.Test)

Example 30 with Connection

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"));
}
Also used : Proxy(java.net.Proxy) Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

Connection (org.jsoup.Connection)47 Test (org.junit.Test)42 Document (org.jsoup.nodes.Document)26 ParseTest (org.jsoup.integration.ParseTest)13 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 News (com.itculturalfestival.smartcampus.entity.News)1 DisposableHolder (com.vegen.smartcampus.baseframework.network.DisposableHolder)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Disposable (io.reactivex.disposables.Disposable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1