1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.IOException ; 34 import java.util.Enumeration ; 35 import junit.framework.*; 36 import org.apache.commons.httpclient.methods.*; 37 38 50 public class TestMethodsExternalHost extends TestCase { 51 52 private HttpClient client; 53 private HttpMethod method; 54 55 57 private static final String externalHost = "java.sun.com"; 58 private static final int externalPort = 80; 59 private static final String externalPath = "/index.html"; 60 private static final String externalUri = "http://java.sun.com/index.html"; 61 private final String PROXY_HOST = System.getProperty("httpclient.test.proxyHost"); 62 private final String PROXY_PORT = System.getProperty("httpclient.test.proxyPort"); 63 private final String PROXY_USER = System.getProperty("httpclient.test.proxyUser"); 64 private final String PROXY_PASS = System.getProperty("httpclient.test.proxyPass"); 65 66 68 69 public TestMethodsExternalHost(String testName) { 70 super(testName); 71 } 72 73 74 76 77 public static Test suite() { 78 return new TestSuite(TestMethodsExternalHost.class); 79 } 80 81 83 public void setUp() { 84 client = new HttpClient(); 85 86 client.getHostConfiguration().setHost(externalHost, externalPort, "http"); 87 88 if (PROXY_HOST != null) { 89 if (PROXY_USER != null) { 90 HttpState state = client.getState(); 91 state.setProxyCredentials(null, new UsernamePasswordCredentials( 92 PROXY_USER, PROXY_PASS)); 93 } 94 client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT)); 95 } 96 } 97 98 public void tearDown() { 99 method.releaseConnection(); 100 method = null; 101 client = null; 102 } 103 104 public void executeMethod() { 105 try { 106 client.executeMethod(method); 107 } catch (Throwable t) { 108 t.printStackTrace(); 109 fail("Unable to execute method : " + t.toString()); 110 } 111 } 112 113 115 116 public void testMethodsOptionsExternal() { 117 118 method = new OptionsMethod(externalPath); 119 executeMethod(); 120 121 Enumeration methodsAllowed = ((OptionsMethod)method).getAllowedMethods(); 122 assertTrue("No HTTP method allowed : result of OPTIONS is incorrect.", 124 methodsAllowed.hasMoreElements()); 125 126 } 127 129 130 public void testMethodsGetExternal() { 131 132 method = new GetMethod(externalUri); 133 executeMethod(); 134 135 try { 136 String data = method.getResponseBodyAsString(); 137 assertTrue("No data returned.", 139 (data.length() > 0)); 140 } catch (Throwable t) { 141 t.printStackTrace(); 142 fail("Unable to execute method : " + t.toString()); 143 } 144 145 method.recycle(); 146 method.setPath(externalPath); 147 executeMethod(); 148 149 try { 150 String data = method.getResponseBodyAsString(); 151 assertTrue("No data returned.", 153 (data.length() > 0)); 154 } catch (Throwable t) { 155 t.printStackTrace(); 156 fail("Unable to execute method : " + t.toString()); 157 } 158 159 } 160 161 162 164 public void testMethodsHeadExternal() { 165 166 method = new HeadMethod(externalPath); 167 executeMethod(); 168 169 assertTrue("Method failed : " + method.getStatusCode(), 170 (method.getStatusCode() == 200)); 171 172 } 173 174 180 public void testIOException() { 181 182 method = new GetMethod("http://www.bogusurl.xyz"); 183 184 try { 185 client.executeMethod(method); 186 if ((PROXY_HOST != null) && (method.getStatusCode() >= 400)) return; 187 } catch (IOException e) { 188 return; } 190 191 fail("Should have thrown an exception"); 192 193 } 194 195 196 199 public void testDomino_Go_Webserver404() throws Exception { 200 201 method = new GetMethod("http://www.pc.ibm.com/us/accessories/monitors/p_allmodelos.html"); 203 int statusCode = client.executeMethod(method); 204 205 assertEquals(404, method.getStatusCode()); 206 207 } 208 209 210 } 211 | Popular Tags |