Search in sources :

Example 96 with URLConnection

use of java.net.URLConnection in project robovm by robovm.

the class ContentHandlerFactoryTest method test_createContentHandler.

@SideEffect("This test affects tests that are run after this one." + " The reason are side effects due to caching in URLConnection." + " Maybe this test needs to be run in isolation.")
public void test_createContentHandler() throws IOException {
    TestContentHandlerFactory factory = new TestContentHandlerFactory();
    if (isTestable) {
        assertFalse(isCreateContentHandlerCalled);
        URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
        URLConnection.setContentHandlerFactory(factory);
        URLConnection con = url.openConnection();
        try {
            con.getContent();
            assertTrue(isCreateContentHandlerCalled);
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        isGetContentCalled = false;
        try {
            con.getContent(new Class[] {});
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            con.setContentHandlerFactory(factory);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
        try {
            con.setContentHandlerFactory(null);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
    } else {
        ContentHandler ch = factory.createContentHandler("text/plain");
        URL url;
        try {
            url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
            assertNotNull(ch.getContent(url.openConnection()));
        } catch (MalformedURLException e) {
            fail("MalformedURLException was thrown: " + e.getMessage());
        } catch (IOException e) {
            fail("IOException was thrown.");
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ContentHandler(java.net.ContentHandler) SideEffect(dalvik.annotation.SideEffect)

Example 97 with URLConnection

use of java.net.URLConnection in project robovm by robovm.

the class URLConnectionTest method test_getRequestProperties_Exception.

/**
     * {@link java.net.URLConnection#getRequestProperties()}
     */
public void test_getRequestProperties_Exception() throws IOException {
    URL url = new URL("http", "test", 80, "index.html", new NewHandler());
    URLConnection urlCon = url.openConnection();
    urlCon.connect();
    try {
        urlCon.getRequestProperties();
        fail("should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 98 with URLConnection

use of java.net.URLConnection in project robovm by robovm.

the class URLConnectionTest method test_guessContentTypeFromName.

/**
     * {@link java.net.URLConnection#guessContentTypeFromName(String)}
     */
public void test_guessContentTypeFromName() throws IOException {
    URLConnection htmlFileCon = openHTMLFile();
    String[] expected = new String[] { "text/html", "text/plain" };
    String[] resources = new String[] { htmlFileCon.getURL().toString(), fileURL.toString() };
    for (int i = 0; i < resources.length; i++) {
        String mime = URLConnection.guessContentTypeFromName(resources[i]);
        assertEquals("checking " + resources[i] + " expected " + expected[i] + "got " + expected[i], expected[i], mime);
    }
    // Try simple case
    try {
        URLConnection.guessContentTypeFromStream(null);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 99 with URLConnection

use of java.net.URLConnection in project robovm by robovm.

the class URLConnectionTest method openHTMLFile.

private URLConnection openHTMLFile() throws IOException {
    String cts = System.getProperty("java.io.tmpdir");
    File tmpDir = new File(cts);
    Support_Resources.copyFile(tmpDir, null, "hyts_htmltest.html");
    URL fUrl1 = new URL("file:/" + tmpDir.getPath() + "/hyts_htmltest.html");
    URLConnection con1 = fUrl1.openConnection();
    return con1;
}
Also used : File(java.io.File) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 100 with URLConnection

use of java.net.URLConnection in project robovm by robovm.

the class URLConnectionTest method test_setIfModifiedSinceJ.

/**
     * {@link java.net.URLConnection#setIfModifiedSince(long)}
     */
public void test_setIfModifiedSinceJ() throws IOException {
    URL url = new URL("http://localhost:8080/");
    URLConnection connection = url.openConnection();
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    cal.clear();
    cal.set(2000, Calendar.MARCH, 5);
    long sinceTime = cal.getTime().getTime();
    connection.setIfModifiedSince(sinceTime);
    assertEquals("Wrong date set", sinceTime, connection.getIfModifiedSince());
    // content should be returned
    uc2.setIfModifiedSince(sinceTime);
    uc2.connect();
    assertEquals(200, ((HttpURLConnection) uc2).getResponseCode());
    try {
        uc2.setIfModifiedSince(2);
        fail("Exception expected");
    } catch (IllegalStateException e) {
    //ok
    }
    ((HttpURLConnection) uc2).disconnect();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Aggregations

URLConnection (java.net.URLConnection)786 URL (java.net.URL)515 IOException (java.io.IOException)328 HttpURLConnection (java.net.HttpURLConnection)284 InputStream (java.io.InputStream)244 InputStreamReader (java.io.InputStreamReader)148 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)139 Test (org.junit.Test)134 BufferedReader (java.io.BufferedReader)130 MalformedURLException (java.net.MalformedURLException)79 MockResponse (okhttp3.mockwebserver.MockResponse)76 File (java.io.File)73 BufferedInputStream (java.io.BufferedInputStream)55 JarURLConnection (java.net.JarURLConnection)53 OutputStream (java.io.OutputStream)50 FileOutputStream (java.io.FileOutputStream)49 FileInputStream (java.io.FileInputStream)42 GZIPInputStream (java.util.zip.GZIPInputStream)37 ArrayList (java.util.ArrayList)33 URI (java.net.URI)32