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