2
* $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsRedirectNoHost.java,v 1.6.2.1 2004/02/22 18:21:16 olegk Exp $
4
* $Date: 2004/02/22 18:21:16 $
5
* ====================================================================
7
* Copyright 2002-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
20
* ====================================================================
22
* This software consists of voluntary contributions made by many
23
* individuals on behalf of the Apache Software Foundation. For more
24
* information on the Apache Software Foundation, please see
25
* <http://www.apache.org/>.
27
* [Additional notices, if required by prior licensing conditions]
31
package org.apache.commons.httpclient;
33
import junit.framework.Test;
34
import junit.framework.TestCase;
35
import junit.framework.TestSuite;
37
import org.apache.commons.httpclient.methods.*;
40
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
41
* @version $Revision: 1.6.2.1 $
43
public class TestMethodsRedirectNoHost extends TestCase {
46
SimpleHttpConnection conn;
49
// ------------------------------------------------------------ Constructor
51
public TestMethodsRedirectNoHost(String testName) {
55
// ------------------------------------------------------- TestCase Methods
57
public static Test suite() {
58
return new TestSuite(TestMethodsRedirectNoHost.class);
61
public void setUp() throws Exception{
62
conn = new SimpleHttpConnection();
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, "");
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, "");
82
// ----------------------------------------------------------------- Tests
84
public void testRedirect() throws Exception {
85
addRedirectResponse("http://localhost/newfile");
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());
99
public void testRedirectIgnoreCase() throws Exception {
100
addRedirectResponse("HtTP://localhost/newfile");
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());
114
public void testPostRedirect() throws Exception {
115
addRedirectResponse("http://localhost/newfile");
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());
129
public void testNoRedirect() throws Exception {
131
addRedirectResponse("http://localhost/newfile");
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());
145
public void testRedirectBadLocation() throws Exception {
146
addRedirectResponse("newfile");
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());
160
public void testRedirectBadLocationStrict() throws Exception {
161
addRedirectResponse("newfile");
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());
174
public void testRedirectBogusLocationStrict() throws Exception {
175
addRedirectResponse("xxx://bogus");
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());
188
public void testRedirectDifferentHost() throws Exception {
189
conn = new SimpleHttpConnection("oldhost", 80);
190
addRedirectResponse("http://newhost/newfile");
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());
202
public void testRedirectDifferentPort() throws Exception {
203
conn = new SimpleHttpConnection("oldhost", 80);
204
addRedirectResponse("http://oldhost:8080/newfile");
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());
217
public void testRedirectDifferentProtocol() throws Exception {
218
conn = new SimpleHttpConnection("oldhost", 80);
219
addRedirectResponse("https://oldhost:80/newfile");
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());
232
public void testRedirectWithCookie() throws Exception {
233
addRedirectResponse("http://localhost/newfile");
237
HttpState state = new HttpState();
239
new Cookie("localhost", "name", "value", "/", -1, false));
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());
247
Header[] headers = method.getRequestHeaders();
249
for (int i = 0; i < headers.length; i++) {
250
if ("cookie".equalsIgnoreCase(headers[i].getName())) {
254
assertTrue("There can only be one (cookie)", cookiecount == 1);