1 16 17 package org.apache.commons.latka; 18 19 import org.apache.commons.latka.http.*; 20 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.SocketException ; 24 import java.net.UnknownHostException ; 25 import java.net.URL ; 26 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 31 public class TestStatusCodes extends TestCase { 32 33 public TestStatusCodes(String testName) { 34 super(testName); 35 } 36 37 public static Test suite() { 38 return new TestSuite(TestStatusCodes.class); 39 } 40 41 54 55 public void testBadURL() { 56 SessionImpl session = new SessionImpl(); 57 58 URL url = null; 59 try { 60 url = new URL ("http://whatabadwolf.com"); 61 } catch (MalformedURLException e) { 62 fail(e.toString()); 63 } 64 65 Request request = 66 session.createRequest(url, Request.HTTP_METHOD_GET, "1.1"); 67 68 try { 69 request.execute(); 70 } catch (IOException e) { 71 return; 73 } 74 75 fail("Bad URL should have thrown IOException"); 76 } 77 78 public void testGoodURL() { 79 SessionImpl session = new SessionImpl(); 80 81 URL url = null; 82 try { 83 url = new URL ("http://www.britannica.com"); 84 } catch (MalformedURLException e) { 85 fail(e.toString()); 86 } 87 88 Request request = 89 session.createRequest(url, Request.HTTP_METHOD_GET, "1.1"); 90 91 try { 92 request.execute(); 93 } catch (SocketException se) { 94 if (se.getMessage().indexOf("unreachable") == -1) { 95 fail("Network error: " + se.toString()); 96 } 97 return; 98 } catch (UnknownHostException e) { 99 } catch (IOException e) { 101 fail(e.toString()); 102 } 103 104 105 } 106 107 108 109 } 110 | Popular Tags |