1 30 31 package org.apache.commons.httpclient; 32 33 import java.util.Enumeration ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.apache.commons.httpclient.methods.GetMethod; 39 import org.apache.commons.httpclient.methods.HeadMethod; 40 import org.apache.commons.httpclient.methods.OptionsMethod; 41 42 57 public class TestMethodsLocalHost extends TestLocalHostBase { 58 59 61 public TestMethodsLocalHost(String testName) { 62 super(testName); 63 } 64 65 66 68 69 public static Test suite() { 70 return new TestSuite(TestMethodsLocalHost.class); 71 } 72 73 74 76 82 public void testMethodsOptions() { 83 84 HttpClient client = createHttpClient(); 85 OptionsMethod method = new OptionsMethod("/"); 86 87 try { 88 client.executeMethod(method); 89 } catch (Throwable t) { 90 t.printStackTrace(); 91 fail("Unable to execute method : " + t.toString()); 92 } 93 94 Enumeration methodsAllowed = method.getAllowedMethods(); 95 assertTrue("No HTTP method allowed : result of OPTIONS is incorrect " 97 + "(make sure the webserver running on port " + getPort() 98 + " supports OPTIONS properly)", 99 methodsAllowed.hasMoreElements()); 100 101 } 102 103 104 106 107 public void testMethodsGet() { 108 109 HttpClient client = createHttpClient(); 110 111 GetMethod method = new GetMethod("/"); 112 113 114 try { 115 client.executeMethod(method); 116 } catch (Throwable t) { 117 t.printStackTrace(); 118 fail("Unable to execute method : " + t.toString()); 119 } 120 121 try { 122 String data = method.getResponseBodyAsString(); 123 assertTrue("No data returned.", 125 (data.length() > 0)); 126 } catch (Throwable t) { 127 t.printStackTrace(); 128 fail("Unable to execute method : " + t.toString()); 129 } 130 131 method.recycle(); 132 method.setPath("/index.html"); 133 134 try { 135 client.executeMethod(method); 136 } catch (Throwable t) { 137 t.printStackTrace(); 138 fail("Unable to execute method : " + t.toString()); 139 } 140 141 try { 142 String data = method.getResponseBodyAsString(); 143 assertTrue("No data returned.", 145 (data.length() > 0)); 146 } catch (Throwable t) { 147 t.printStackTrace(); 148 fail("Unable to execute method : " + t.toString()); 149 } 150 151 } 152 153 154 156 157 public void testMethodsHead() { 158 159 HttpClient client = createHttpClient(); 160 161 OptionsMethod opmethod = new OptionsMethod("/"); 162 163 try { 164 client.executeMethod(opmethod); 165 } catch (Throwable t) { 166 t.printStackTrace(); 167 fail("Unable to execute method : " + t.toString()); 168 } 169 170 String path = "/"; 171 HeadMethod method = new HeadMethod(path); 172 173 try { 174 client.executeMethod(method); 175 } catch (Throwable t) { 176 t.printStackTrace(); 177 fail("Unable to execute method : " + t.toString()); 178 } 179 180 assertEquals(200, method.getStatusCode()); 181 182 method.recycle(); 183 method.setPath(path); 184 185 try { 186 client.executeMethod(method); 187 } catch (Throwable t) { 188 t.printStackTrace(); 189 fail("Unable to execute method : " + t.toString()); 190 } 191 192 assertEquals(200, method.getStatusCode()); 193 194 } 195 196 } 197 | Popular Tags |