1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.IOException ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.methods.GetMethod; 39 40 46 public class TestGetMethodLocal extends TestLocalHostBase { 47 48 50 public TestGetMethodLocal(String testName) { 51 super(testName); 52 } 53 54 55 57 public static Test suite() { 58 return new TestSuite(TestGetMethodLocal.class); 59 } 60 61 63 public void testGetSlash() { 64 HttpClient client = createHttpClient(); 65 66 GetMethod method = new GetMethod("/"); 67 68 try { 69 client.executeMethod(method); 70 } catch (Throwable t) { 71 t.printStackTrace(); 72 fail("Unable to execute method : " + t.toString()); 73 } 74 75 try { 76 String data = method.getResponseBodyAsString(); 77 assertTrue("No data returned.",(data.length() > 0)); 78 } catch (Throwable t) { 79 t.printStackTrace(); 80 fail("Unable to execute method : " + t.toString()); 81 } 82 assertEquals(200,method.getStatusCode()); 83 } 84 85 public void testExecuteMultipleMethods() throws Exception { 86 87 HttpClient client = createHttpClient(); 88 89 GetMethod getSlash = new GetMethod("/"); 90 for(int i=0;i<10;i++) { 91 assertEquals(200, client.executeMethod(getSlash)); 92 String data = getSlash.getResponseBodyAsString(); 93 assertTrue(null != data); 94 assertTrue(data.length() > 0); 95 getSlash.recycle(); 96 getSlash.setPath("/"); 97 } 98 } 99 100 public void testRecycle() { 101 HttpClient client = createHttpClient(null); 102 103 GetMethod method = new GetMethod("/"); 104 105 try { 106 client.executeMethod(method); 107 } catch (Throwable t) { 108 t.printStackTrace(); 109 fail("Unable to execute method : " + t.toString()); 110 } 111 112 try { 113 String data = method.getResponseBodyAsString(); 114 assertTrue("No data returned.",(data.length() > 0)); 115 } catch (Throwable t) { 116 t.printStackTrace(); 117 fail("Unable to execute method : " + t.toString()); 118 } 119 assertEquals(200,method.getStatusCode()); 120 121 method.recycle(); 122 method.setPath("/"); 123 124 try { 125 client.executeMethod(method); 126 } catch (Throwable t) { 127 t.printStackTrace(); 128 fail("Unable to execute method : " + t.toString()); 129 } 130 131 try { 132 String data = method.getResponseBodyAsString(); 133 assertTrue("No data returned.",(data.length() > 0)); 134 } catch (Throwable t) { 135 t.printStackTrace(); 136 fail("Unable to execute method : " + t.toString()); 137 } 138 assertEquals(200,method.getStatusCode()); 139 140 } 141 142 public void test404() { 143 HttpClient client = createHttpClient(null); 144 145 GetMethod method = new GetMethod("/i/am/assuming/this/path/and/file/doesnt/exist/on/the/web/server.xyzzy"); 146 147 try { 148 client.executeMethod(method); 149 } catch (Throwable t) { 150 t.printStackTrace(); 151 fail("Unable to execute method : " + t.toString()); 152 } 153 assertEquals(404,method.getStatusCode()); 154 155 } 156 157 165 public void testGetResponseNotReadAutoRecover() { 166 HttpClient client = createHttpClient(null); 167 168 try { 169 String path = "/"; 171 GetMethod method1 = new GetMethod(path); 172 method1.addRequestHeader("Connection", "close"); 173 client.executeMethod(method1); 174 assertEquals(0, method1.getRecoverableExceptionCount() ); 175 176 GetMethod method2 = new GetMethod(path); 178 client.executeMethod(method2); 179 assertEquals(0, method2.getRecoverableExceptionCount() ); 180 } 181 catch (IOException ioe) { 182 183 fail("Problem executing method : " + ioe.toString() ); 184 } 185 } 186 187 } 188 | Popular Tags |