1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsRedirectNoHost.java,v 1.6 2003/04/20 23:26:23 olegk Exp $ 3 * $Revision: 1.6 $ 4 * $Date: 2003/04/20 23:26:23 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import junit.framework.Test; 66 import junit.framework.TestCase; 67 import junit.framework.TestSuite; 68 69 import org.apache.commons.httpclient.methods.*; 70 71 /*** 72 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 73 * @version $Revision: 1.6 $ 74 */ 75 public class TestMethodsRedirectNoHost extends TestCase { 76 77 78 SimpleHttpConnection conn; 79 80 81 // ------------------------------------------------------------ Constructor 82 83 public TestMethodsRedirectNoHost(String testName) { 84 super(testName); 85 } 86 87 // ------------------------------------------------------- TestCase Methods 88 89 public static Test suite() { 90 return new TestSuite(TestMethodsRedirectNoHost.class); 91 } 92 93 public void setUp() throws Exception{ 94 conn = new SimpleHttpConnection(); 95 } 96 97 98 private void addRedirectResponse(String location) { 99 String headers = "HTTP/1.1 302 Redirect\r\n" 100 +"Date: Wed, 28 Mar 2002 05:05:04 GMT\r\n" 101 +"Location: " + location + "\r\n" 102 +"Connection: close\r\n"; 103 conn.addResponse(headers, ""); 104 } 105 106 private void addOkResponse() { 107 String headers = "HTTP/1.1 200 OK\r\n" 108 +"Date: Wed, 28 Mar 2001 05:05:04 GMT\r\n" 109 +"Connection: close\r\n"; 110 conn.addResponse(headers, ""); 111 } 112 113 114 // ----------------------------------------------------------------- Tests 115 116 public void testRedirect() throws Exception { 117 addRedirectResponse("http://localhost/newfile"); 118 addOkResponse(); 119 conn.open(); 120 121 HttpMethod method = new SimpleHttpMethod("/oldfile"); 122 method.setFollowRedirects(true); 123 method.execute(new HttpState(), conn); 124 Header locationHeader = method.getResponseHeader("Location"); 125 assertEquals(200, method.getStatusCode()); 126 assertEquals("/newfile", method.getPath()); 127 128 } 129 130 131 public void testRedirectIgnoreCase() throws Exception { 132 addRedirectResponse("HtTP://localhost/newfile"); 133 addOkResponse(); 134 conn.open(); 135 136 HttpMethod method = new SimpleHttpMethod("/oldfile"); 137 method.setFollowRedirects(true); 138 method.execute(new HttpState(), conn); 139 Header locationHeader = method.getResponseHeader("Location"); 140 assertEquals(200, method.getStatusCode()); 141 assertEquals("/newfile", method.getPath()); 142 143 } 144 145 146 public void testPostRedirect() throws Exception { 147 addRedirectResponse("http://localhost/newfile"); 148 addOkResponse(); 149 conn.open(); 150 151 PostMethod method = new PostMethod("/oldfile"); 152 method.setRequestBody(new NameValuePair[] { new NameValuePair("name", "value") } ); 153 method.execute(new HttpState(), conn); 154 Header locationHeader = method.getResponseHeader("Location"); 155 assertEquals(302, method.getStatusCode()); 156 assertEquals("/oldfile", method.getPath()); 157 158 } 159 160 161 public void testNoRedirect() throws Exception { 162 163 addRedirectResponse("http://localhost/newfile"); 164 addOkResponse(); 165 conn.open(); 166 167 HttpMethod method = new SimpleHttpMethod("/oldfile"); 168 method.setFollowRedirects(false); 169 method.execute(new HttpState(), conn); 170 Header locationHeader = method.getResponseHeader("Location"); 171 assertEquals(302, method.getStatusCode()); 172 assertEquals("/oldfile", method.getPath()); 173 174 } 175 176 177 public void testRedirectBadLocation() throws Exception { 178 addRedirectResponse("newfile"); 179 addOkResponse(); 180 conn.open(); 181 182 HttpMethod method = new SimpleHttpMethod("/oldfile"); 183 method.setFollowRedirects(true); 184 method.setStrictMode(false); 185 method.execute(new HttpState(), conn); 186 Header locationHeader = method.getResponseHeader("Location"); 187 assertEquals(200, method.getStatusCode()); 188 assertEquals("/newfile", method.getPath()); 189 } 190 191 192 public void testRedirectBadLocationStrict() throws Exception { 193 addRedirectResponse("newfile"); 194 addOkResponse(); 195 conn.open(); 196 197 HttpMethod method = new SimpleHttpMethod("/oldfile"); 198 method.setFollowRedirects(true); 199 method.setStrictMode(true); 200 method.execute(new HttpState(), conn); 201 Header locationHeader = method.getResponseHeader("Location"); 202 assertEquals(302, method.getStatusCode()); 203 assertEquals("/oldfile", method.getPath()); 204 } 205 206 public void testRedirectBogusLocationStrict() throws Exception { 207 addRedirectResponse("xxx://bogus"); 208 addOkResponse(); 209 conn.open(); 210 211 HttpMethod method = new SimpleHttpMethod("/oldfile"); 212 method.setFollowRedirects(true); 213 method.setStrictMode(true); 214 method.execute(new HttpState(), conn); 215 Header locationHeader = method.getResponseHeader("Location"); 216 assertEquals(302, method.getStatusCode()); 217 assertEquals("/oldfile", method.getPath()); 218 } 219 220 public void testRedirectDifferentHost() throws Exception { 221 conn = new SimpleHttpConnection("oldhost", 80); 222 addRedirectResponse("http://newhost/newfile"); 223 addOkResponse(); 224 conn.open(); 225 226 HttpMethod method = new SimpleHttpMethod("/oldfile"); 227 method.setFollowRedirects(true); 228 method.execute(new HttpState(), conn); 229 Header locationHeader = method.getResponseHeader("Location"); 230 assertEquals(302, method.getStatusCode()); 231 assertEquals("/oldfile", method.getPath()); 232 } 233 234 public void testRedirectDifferentPort() throws Exception { 235 conn = new SimpleHttpConnection("oldhost", 80); 236 addRedirectResponse("http://oldhost:8080/newfile"); 237 addOkResponse(); 238 conn.open(); 239 240 HttpMethod method = new SimpleHttpMethod("/oldfile"); 241 method.setFollowRedirects(true); 242 method.execute(new HttpState(), conn); 243 Header locationHeader = method.getResponseHeader("Location"); 244 assertEquals(302, method.getStatusCode()); 245 assertEquals("/oldfile", method.getPath()); 246 } 247 248 249 public void testRedirectDifferentProtocol() throws Exception { 250 conn = new SimpleHttpConnection("oldhost", 80); 251 addRedirectResponse("https://oldhost:80/newfile"); 252 addOkResponse(); 253 conn.open(); 254 255 HttpMethod method = new SimpleHttpMethod("/oldfile"); 256 method.setFollowRedirects(true); 257 method.execute(new HttpState(), conn); 258 Header locationHeader = method.getResponseHeader("Location"); 259 assertEquals(302, method.getStatusCode()); 260 assertEquals("/oldfile", method.getPath()); 261 } 262 263 264 public void testRedirectWithCookie() throws Exception { 265 addRedirectResponse("http://localhost/newfile"); 266 addOkResponse(); 267 conn.open(); 268 269 HttpState state = new HttpState(); 270 state.addCookie( 271 new Cookie("localhost", "name", "value", "/", -1, false)); 272 273 HttpMethod method = new SimpleHttpMethod("/oldfile"); 274 method.setFollowRedirects(true); 275 method.execute(state, conn); 276 Header locationHeader = method.getResponseHeader("Location"); 277 assertEquals(200, method.getStatusCode()); 278 279 Header[] headers = method.getRequestHeaders(); 280 int cookiecount = 0; 281 for (int i = 0; i < headers.length; i++) { 282 if ("cookie".equalsIgnoreCase(headers[i].getName())) { 283 ++cookiecount; 284 } 285 } 286 assertTrue("There can only be one (cookie)", cookiecount == 1); 287 } 288 289 }

This page was automatically generated by Maven