Search in sources :

Example 21 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class UrlConnectTest method combinesSameHeadersWithComma.

@Test
public void combinesSameHeadersWithComma() throws IOException {
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
    String url = "http://direct.infohound.net/tools/q.pl";
    Connection con = Jsoup.connect(url);
    con.get();
    assertEquals("text/html", con.response().header("Content-Type"));
    assertEquals("no-cache, no-store", con.response().header("Cache-Control"));
}
Also used : Connection(org.jsoup.Connection) Test(org.junit.Test)

Example 22 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class UrlConnectTest method followsRelativeDotRedirect.

@Test
public void followsRelativeDotRedirect() throws IOException {
    // redirects to "./ok.html", should resolve to http://direct.infohound.net/tools/ok.html
    // to ./ok.html
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/302-rel-dot.pl");
    Document doc = con.post();
    assertTrue(doc.title().contains("OK"));
    assertEquals(doc.location(), "http://direct.infohound.net/tools/ok.html");
}
Also used : Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 23 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class UrlConnectTest method multiCookieSet.

@Test
public void multiCookieSet() throws IOException {
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/302-cookie.pl");
    Connection.Response res = con.execute();
    // test cookies set by redirect:
    Map<String, String> cookies = res.cookies();
    assertEquals("asdfg123", cookies.get("token"));
    assertEquals("jhy", cookies.get("uid"));
    // send those cookies into the echo URL by map:
    Document doc = Jsoup.connect(echoURL).cookies(cookies).get();
    assertEquals("token=asdfg123; uid=jhy", ihVal("HTTP_COOKIE", doc));
}
Also used : Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 24 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class UrlConnectTest method throwsExceptionOnError.

@Test
public void throwsExceptionOnError() {
    String url = "http://direct.infohound.net/tools/404";
    Connection con = Jsoup.connect(url);
    boolean threw = false;
    try {
        Document doc = con.get();
    } catch (HttpStatusException e) {
        threw = true;
        assertEquals("org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=http://direct.infohound.net/tools/404", e.toString());
        assertEquals(url, e.getUrl());
        assertEquals(404, e.getStatusCode());
    } catch (IOException e) {
    }
    assertTrue(threw);
}
Also used : Connection(org.jsoup.Connection) HttpStatusException(org.jsoup.HttpStatusException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 25 with Connection

use of org.jsoup.Connection in project jsoup by jhy.

the class UrlConnectTest method followsNewTempRedirect.

@Test
public void followsNewTempRedirect() throws IOException {
    // http://jsoup.org
    Connection con = Jsoup.connect("http://direct.infohound.net/tools/307.pl");
    Document doc = con.get();
    assertTrue(doc.title().contains("jsoup"));
    assertEquals("https://jsoup.org/", con.response().url().toString());
}
Also used : 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