1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestSuite; 36 37 import org.apache.commons.httpclient.methods.*; 38 39 43 public class TestMethodsRedirectNoHost extends TestCase { 44 45 46 SimpleHttpConnection conn; 47 48 49 51 public TestMethodsRedirectNoHost(String testName) { 52 super(testName); 53 } 54 55 57 public static Test suite() { 58 return new TestSuite(TestMethodsRedirectNoHost.class); 59 } 60 61 public void setUp() throws Exception { 62 conn = new SimpleHttpConnection(); 63 } 64 65 66 private void addRedirectResponse(String location) { 67 String headers = "HTTP/1.1 302 Redirect\r\n" 68 +"Date: Wed, 28 Mar 2002 05:05:04 GMT\r\n" 69 +"Location: " + location + "\r\n" 70 +"Connection: close\r\n"; 71 conn.addResponse(headers, ""); 72 } 73 74 private void addOkResponse() { 75 String headers = "HTTP/1.1 200 OK\r\n" 76 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 77 +"Connection: close\r\n"; 78 conn.addResponse(headers, ""); 79 } 80 81 82 84 public void testRedirect() throws Exception { 85 addRedirectResponse("http://localhost/newfile"); 86 addOkResponse(); 87 conn.open(); 88 89 HttpMethod method = new SimpleHttpMethod("/oldfile"); 90 method.setFollowRedirects(true); 91 method.execute(new HttpState(), conn); 92 Header locationHeader = method.getResponseHeader("Location"); 93 assertEquals(200, method.getStatusCode()); 94 assertEquals("/newfile", method.getPath()); 95 96 } 97 98 99 public void testRedirectIgnoreCase() throws Exception { 100 addRedirectResponse("HtTP://localhost/newfile"); 101 addOkResponse(); 102 conn.open(); 103 104 HttpMethod method = new SimpleHttpMethod("/oldfile"); 105 method.setFollowRedirects(true); 106 method.execute(new HttpState(), conn); 107 Header locationHeader = method.getResponseHeader("Location"); 108 assertEquals(200, method.getStatusCode()); 109 assertEquals("/newfile", method.getPath()); 110 111 } 112 113 114 public void testPostRedirect() throws Exception { 115 addRedirectResponse("http://localhost/newfile"); 116 addOkResponse(); 117 conn.open(); 118 119 PostMethod method = new PostMethod("/oldfile"); 120 method.setRequestBody(new NameValuePair[] { new NameValuePair("name", "value") } ); 121 method.execute(new HttpState(), conn); 122 Header locationHeader = method.getResponseHeader("Location"); 123 assertEquals(302, method.getStatusCode()); 124 assertEquals("/oldfile", method.getPath()); 125 126 } 127 128 129 public void testNoRedirect() throws Exception { 130 131 addRedirectResponse("http://localhost/newfile"); 132 addOkResponse(); 133 conn.open(); 134 135 HttpMethod method = new SimpleHttpMethod("/oldfile"); 136 method.setFollowRedirects(false); 137 method.execute(new HttpState(), conn); 138 Header locationHeader = method.getResponseHeader("Location"); 139 assertEquals(302, method.getStatusCode()); 140 assertEquals("/oldfile", method.getPath()); 141 142 } 143 144 145 public void testRedirectBadLocation() throws Exception { 146 addRedirectResponse("newfile"); 147 addOkResponse(); 148 conn.open(); 149 150 HttpMethod method = new SimpleHttpMethod("/oldfile"); 151 method.setFollowRedirects(true); 152 method.setStrictMode(false); 153 method.execute(new HttpState(), conn); 154 Header locationHeader = method.getResponseHeader("Location"); 155 assertEquals(200, method.getStatusCode()); 156 assertEquals("/newfile", method.getPath()); 157 } 158 159 160 public void testRedirectBadLocationStrict() throws Exception { 161 addRedirectResponse("newfile"); 162 addOkResponse(); 163 conn.open(); 164 165 HttpMethod method = new SimpleHttpMethod("/oldfile"); 166 method.setFollowRedirects(true); 167 method.setStrictMode(true); 168 method.execute(new HttpState(), conn); 169 Header locationHeader = method.getResponseHeader("Location"); 170 assertEquals(302, method.getStatusCode()); 171 assertEquals("/oldfile", method.getPath()); 172 } 173 174 public void testRedirectBogusLocationStrict() throws Exception { 175 addRedirectResponse("xxx://bogus"); 176 addOkResponse(); 177 conn.open(); 178 179 HttpMethod method = new SimpleHttpMethod("/oldfile"); 180 method.setFollowRedirects(true); 181 method.setStrictMode(true); 182 method.execute(new HttpState(), conn); 183 Header locationHeader = method.getResponseHeader("Location"); 184 assertEquals(302, method.getStatusCode()); 185 assertEquals("/oldfile", method.getPath()); 186 } 187 188 public void testRedirectDifferentHost() throws Exception { 189 conn = new SimpleHttpConnection("oldhost", 80); 190 addRedirectResponse("http://newhost/newfile"); 191 addOkResponse(); 192 conn.open(); 193 194 HttpMethod method = new SimpleHttpMethod("/oldfile"); 195 method.setFollowRedirects(true); 196 method.execute(new HttpState(), conn); 197 Header locationHeader = method.getResponseHeader("Location"); 198 assertEquals(302, method.getStatusCode()); 199 assertEquals("/oldfile", method.getPath()); 200 } 201 202 public void testRedirectDifferentPort() throws Exception { 203 conn = new SimpleHttpConnection("oldhost", 80); 204 addRedirectResponse("http://oldhost:8080/newfile"); 205 addOkResponse(); 206 conn.open(); 207 208 HttpMethod method = new SimpleHttpMethod("/oldfile"); 209 method.setFollowRedirects(true); 210 method.execute(new HttpState(), conn); 211 Header locationHeader = method.getResponseHeader("Location"); 212 assertEquals(302, method.getStatusCode()); 213 assertEquals("/oldfile", method.getPath()); 214 } 215 216 217 public void testRedirectDifferentProtocol() throws Exception { 218 conn = new SimpleHttpConnection("oldhost", 80); 219 addRedirectResponse("https://oldhost:80/newfile"); 220 addOkResponse(); 221 conn.open(); 222 223 HttpMethod method = new SimpleHttpMethod("/oldfile"); 224 method.setFollowRedirects(true); 225 method.execute(new HttpState(), conn); 226 Header locationHeader = method.getResponseHeader("Location"); 227 assertEquals(302, method.getStatusCode()); 228 assertEquals("/oldfile", method.getPath()); 229 } 230 231 232 public void testRedirectWithCookie() throws Exception { 233 addRedirectResponse("http://localhost/newfile"); 234 addOkResponse(); 235 conn.open(); 236 237 HttpState state = new HttpState(); 238 state.addCookie( 239 new Cookie("localhost", "name", "value", "/", -1, false)); 240 241 HttpMethod method = new SimpleHttpMethod("/oldfile"); 242 method.setFollowRedirects(true); 243 method.execute(state, conn); 244 Header locationHeader = method.getResponseHeader("Location"); 245 assertEquals(200, method.getStatusCode()); 246 247 Header[] headers = method.getRequestHeaders(); 248 int cookiecount = 0; 249 for (int i = 0; i < headers.length; i++) { 250 if ("cookie".equalsIgnoreCase(headers[i].getName())) { 251 ++cookiecount; 252 } 253 } 254 assertTrue("There can only be one (cookie)", cookiecount == 1); 255 } 256 257 } 258 | Popular Tags |