FileDocCategorySizeDatePackage
URLTest.javaAPI DocAndroid 1.5 API6858Wed May 06 22:42:02 BST 2009android.core

URLTest

public class URLTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
private static voidget(java.lang.String u)

        URL url = new URL(u);
        URLConnection cn = url.openConnection();
        cn.connect();
//        System.out.println("Content-Type: " + cn.getContentType());
//        System.out.println("Content-Length: " + cn.getContentLength());

        InputStream stream = cn.getInputStream();
        if (stream == null) {
            throw new RuntimeException("stream is null");
        }
        byte[] data = new byte[1024];
        stream.read(data);

//            if (true) {
//                System.out.print("data=");
//                System.out.write(data);
//                System.out.println();
//            }

//                System.out.println("Content-Type: " + cn.getContentType());
//                System.out.print("data:");
//                System.out.write(data);
//                System.out.println();

        assertTrue(new String(data).indexOf("<html>") >= 0);
    
private java.lang.Stringrequest(java.net.URL url)
Does a request to the given URL, reads and returns the result.

        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream input = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        try {
            return reader.readLine();
        } finally {
            reader.close();
        }
    
public voidtestGetHTTP()

        get("http://www.google.com");
    
public voidtestGetHTTPS()

        get("https://www.fortify.net/cgi/ssl_2.pl");
    
public voidtestGetKeepAlive()
Test case for HTTP keep-alive behavior.

        new Thread(new DummyServer(3)).start();
        Thread.sleep(100);

        // We expect the request to work three times, then it fails.
        URL url = new URL("http://localhost:8182");
        assertEquals("Hello, Android world #0!", request(url));
        assertEquals("Hello, Android world #1!", request(url));
        assertEquals("Hello, Android world #2!", request(url));

        try {
            request(url);
            fail("ConnectException expected.");
        } catch (Exception ex) {
            // Ok.
        }
    
public voidtestHttpConnectionTimeout()
Regression for issue 1001814.

        int timeout = 5000;
        HttpURLConnection cn = null;
        long start = 0;
        try {
            start = System.currentTimeMillis();
            URL url = new URL("http://123.123.123.123");
            cn = (HttpURLConnection) url.openConnection();
            cn.setConnectTimeout(5000);
            cn.connect();
            fail("should have thrown an exception");
        } catch (IOException ioe) {
            long delay = System.currentTimeMillis() - start;
            if (Math.abs(timeout - delay) > 1000) {
                fail("Timeout was not accurate. it needed " + delay +
                        " instead of " + timeout + "miliseconds");
            }
        } finally {
            if (cn != null) {
                cn.disconnect();
            }
        }
    
public voidtestMalformedUrl()
Regression test for issue 1158780 where using '{' and '}' in an URL threw an NPE. The RI accepts this URL and returns the status 404.

        URL url = new URL("http://www.google.com/cgi-bin/myscript?g={United+States}+Borders+Mexico+{Climate+change}+Marketing+{Automotive+industry}+News+Health+Internet");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        int status = conn.getResponseCode();
        android.util.Log.d("URLTest", "status: " + status);