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 package org.apache.commons.httpclient;
29
30 import java.io.IOException;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.apache.commons.httpclient.auth.AuthScope;
36 import org.apache.commons.httpclient.methods.GetMethod;
37 import org.apache.commons.httpclient.server.RequestLine;
38 import org.apache.commons.httpclient.server.SimpleRequest;
39 import org.apache.commons.httpclient.server.SimpleResponse;
40
41 /***
42 * Tests for proxied connections.
43 *
44 * @author Ortwin Glueck
45 * @author Oleg Kalnichevski
46 */
47 public class TestProxyWithRedirect extends HttpClientTestBase {
48
49 public TestProxyWithRedirect(String testName) throws IOException {
50 super(testName);
51 setUseProxy(true);
52 }
53
54 public static Test suite() {
55 return new TestSuite(TestProxyWithRedirect.class);
56 }
57
58 private class BasicRedirectService extends EchoService {
59
60 private String location = null;
61
62 public BasicRedirectService(final String location) {
63 super();
64 this.location = location;
65 }
66
67 public boolean process(final SimpleRequest request, final SimpleResponse response)
68 throws IOException
69 {
70 RequestLine reqline = request.getRequestLine();
71 HttpVersion ver = reqline.getHttpVersion();
72 if (reqline.getUri().equals("/redirect/")) {
73 response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
74 response.addHeader(new Header("Location", this.location));
75 response.addHeader(new Header("Connection", "Close"));
76 return true;
77 } else {
78 return super.process(request, response);
79 }
80 }
81 }
82
83 public void testAuthProxyWithRedirect() throws Exception {
84 UsernamePasswordCredentials creds =
85 new UsernamePasswordCredentials("testuser", "testpass");
86
87 this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
88 this.server.setHttpService(new BasicRedirectService("/"));
89 this.proxy.requireAuthentication(creds, "test", true);
90
91 GetMethod get = new GetMethod("/redirect/");
92 try {
93 this.client.executeMethod(get);
94 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
95 } finally {
96 get.releaseConnection();
97 }
98 }
99
100 public void testAuthProxyWithCrossSiteRedirect() throws Exception {
101 UsernamePasswordCredentials creds =
102 new UsernamePasswordCredentials("testuser", "testpass");
103
104 this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
105 this.server.setHttpService(new BasicRedirectService(
106 "http://127.0.0.1:" + this.server.getLocalPort()));
107
108 this.proxy.requireAuthentication(creds, "test", true);
109
110 GetMethod get = new GetMethod("/redirect/");
111 try {
112 this.client.executeMethod(get);
113 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
114 } finally {
115 get.releaseConnection();
116 }
117 }
118
119 public void testPreemptiveAuthProxyWithCrossSiteRedirect() throws Exception {
120 UsernamePasswordCredentials creds =
121 new UsernamePasswordCredentials("testuser", "testpass");
122
123 this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
124 this.client.getParams().setAuthenticationPreemptive(true);
125 this.server.setHttpService(new BasicRedirectService(
126 "http://127.0.0.1:" + this.server.getLocalPort()));
127
128 this.proxy.requireAuthentication(creds, "test", true);
129
130 GetMethod get = new GetMethod("/redirect/");
131 try {
132 this.client.executeMethod(get);
133 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
134 } finally {
135 get.releaseConnection();
136 }
137 }
138
139 }