1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 import org.apache.commons.httpclient.params.HttpClientParams;
40 import org.apache.commons.httpclient.server.HttpService;
41 import org.apache.commons.httpclient.server.RequestLine;
42 import org.apache.commons.httpclient.server.SimpleRequest;
43 import org.apache.commons.httpclient.server.SimpleResponse;
44
45 /***
46 * Basic authentication test cases.
47 *
48 * @author Oleg Kalnichevski
49 *
50 * @version $Id: TestRedirects.java,v 1.2 2004/04/12 11:16:25 olegk Exp $
51 */
52 public class TestRedirects extends HttpClientTestBase {
53
54
55 public TestRedirects(String testName) {
56 super(testName);
57 }
58
59
60 public static void main(String args[]) {
61 String[] testCaseName = { TestRedirects.class.getName() };
62 junit.textui.TestRunner.main(testCaseName);
63 }
64
65
66
67 public static Test suite() {
68 return new TestSuite(TestRedirects.class);
69 }
70
71 private class RedirectService implements HttpService {
72
73 public RedirectService() {
74 super();
75 }
76
77 public boolean process(final SimpleRequest request, final SimpleResponse response)
78 throws IOException
79 {
80 RequestLine reqline = request.getRequestLine();
81 if (reqline.getUri().equals("/circular-location1/")) {
82 response.setStatusLine("HTTP/1.1 302 Object moved");
83 response.addHeader(new Header("Location", "/circular-location2/"));
84 } else if (reqline.getUri().equals("/circular-location2/")) {
85 response.setStatusLine("HTTP/1.1 302 Object moved");
86 response.addHeader(new Header("Location", "/circular-location1/"));
87 } else {
88 response.setStatusLine("HTTP/1.1 404 Not Found");
89 }
90 return true;
91 }
92 }
93
94 public void testMaxRedirectCheck() throws IOException {
95 this.server.setHttpService(new RedirectService());
96 GetMethod httpget = new GetMethod("/circular-location1/");
97 try {
98 this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
99 this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS, 5);
100 this.client.executeMethod(httpget);
101 fail("RedirectException exception should have been thrown");
102 }
103 catch (RedirectException e) {
104
105 } finally {
106 httpget.releaseConnection();
107 }
108 }
109
110 public void testCircularRedirect() throws IOException {
111 this.server.setHttpService(new RedirectService());
112 GetMethod httpget = new GetMethod("/circular-location1/");
113 try {
114 this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false);
115 this.client.executeMethod(httpget);
116 fail("RedirectException exception should have been thrown");
117 }
118 catch (RedirectException e) {
119
120 } finally {
121 httpget.releaseConnection();
122 }
123 }
124
125 private class RedirectService2 implements HttpService {
126
127 private String host = null;
128 private int port;
129
130 public RedirectService2(final String host, int port) {
131 super();
132 this.host = host;
133 this.port = port;
134 }
135
136 public boolean process(final SimpleRequest request, final SimpleResponse response)
137 throws IOException
138 {
139 RequestLine reqline = request.getRequestLine();
140 if (reqline.getUri().equals("/location1/")) {
141 response.setStatusLine("HTTP/1.1 302 Object moved");
142 response.addHeader(new Header("Location", "http://" + this.host + ":" + this.port + "/location2/"));
143 } else if (reqline.getUri().equals("/location2/")) {
144 response.setStatusLine("HTTP/1.1 200 OK");
145 response.setBodyString("Successful redirect");
146 } else {
147 response.setStatusLine("HTTP/1.1 404 Not Found");
148 }
149 return true;
150 }
151 }
152
153 public void testRedirectLocation() throws IOException {
154 String host = this.server.getLocalAddress();
155 int port = this.server.getLocalPort();
156 this.server.setHttpService(new RedirectService2(host, port));
157 GetMethod httpget = new GetMethod("/location1/");
158 try {
159 this.client.executeMethod(httpget);
160 assertEquals(host, httpget.getURI().getHost());
161 assertEquals(port, httpget.getURI().getPort());
162 assertEquals(new URI("http://" + host + ":" + port + "/location2/", false), httpget.getURI());
163 } finally {
164 httpget.releaseConnection();
165 }
166 }
167
168 }