|
16 | 16 | import static java.nio.charset.StandardCharsets.UTF_8;
|
17 | 17 | import static org.asynchttpclient.Dsl.*;
|
18 | 18 | import static org.asynchttpclient.test.TestUtils.*;
|
19 |
| -import static org.testng.Assert.*; |
20 | 19 |
|
21 | 20 | import java.io.IOException;
|
22 | 21 | import java.io.OutputStream;
|
23 | 22 | import java.util.concurrent.Future;
|
24 | 23 | import java.util.concurrent.TimeUnit;
|
| 24 | +import java.util.concurrent.TimeoutException; |
25 | 25 |
|
26 | 26 | import javax.servlet.ServletException;
|
27 | 27 | import javax.servlet.http.HttpServletRequest;
|
28 | 28 | import javax.servlet.http.HttpServletResponse;
|
29 | 29 |
|
30 |
| -import org.asynchttpclient.exception.RemotelyClosedException; |
31 | 30 | import org.eclipse.jetty.server.Request;
|
32 | 31 | import org.eclipse.jetty.server.Server;
|
33 | 32 | import org.eclipse.jetty.server.ServerConnector;
|
|
38 | 37 |
|
39 | 38 | public class AuthTimeoutTest extends AbstractBasicTest {
|
40 | 39 |
|
| 40 | + private static final int REQUEST_TIMEOUT = 1000; |
| 41 | + private static final int SHORT_FUTURE_TIMEOUT = 500; // shorter than REQUEST_TIMEOUT |
| 42 | + private static final int LONG_FUTURE_TIMEOUT = 1500; // longer than REQUEST_TIMEOUT |
| 43 | + |
41 | 44 | private Server server2;
|
42 | 45 |
|
43 | 46 | @BeforeClass(alwaysRun = true)
|
@@ -83,115 +86,102 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
|
83 | 86 | }
|
84 | 87 | }
|
85 | 88 |
|
86 |
| - @Test(groups = "standalone", enabled = false) |
87 |
| - public void basicAuthTimeoutTest() throws Exception { |
| 89 | + @Test(expectedExceptions = TimeoutException.class) |
| 90 | + public void basicAuthTimeoutTest() throws Throwable { |
88 | 91 | try (AsyncHttpClient client = newClient()) {
|
89 |
| - Future<Response> f = execute(client, server, false); |
90 |
| - f.get(); |
91 |
| - fail("expected timeout"); |
| 92 | + execute(client, true, false).get(LONG_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
92 | 93 | } catch (Exception e) {
|
93 |
| - inspectException(e); |
| 94 | + throw e.getCause(); |
94 | 95 | }
|
95 | 96 | }
|
96 | 97 |
|
97 |
| - @Test(groups = "standalone", enabled = false) |
98 |
| - public void basicPreemptiveAuthTimeoutTest() throws Exception { |
| 98 | + @Test(expectedExceptions = TimeoutException.class) |
| 99 | + public void basicPreemptiveAuthTimeoutTest() throws Throwable { |
99 | 100 | try (AsyncHttpClient client = newClient()) {
|
100 |
| - Future<Response> f = execute(client, server, true); |
101 |
| - f.get(); |
102 |
| - fail("expected timeout"); |
| 101 | + execute(client, true, true).get(LONG_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
103 | 102 | } catch (Exception e) {
|
104 |
| - inspectException(e); |
| 103 | + throw e.getCause(); |
105 | 104 | }
|
106 | 105 | }
|
107 | 106 |
|
108 |
| - @Test(groups = "standalone", enabled = false) |
109 |
| - public void digestAuthTimeoutTest() throws Exception { |
| 107 | + @Test(expectedExceptions = TimeoutException.class) |
| 108 | + public void digestAuthTimeoutTest() throws Throwable { |
110 | 109 | try (AsyncHttpClient client = newClient()) {
|
111 |
| - Future<Response> f = execute(client, server2, false); |
112 |
| - f.get(); |
113 |
| - fail("expected timeout"); |
| 110 | + execute(client, false, false).get(LONG_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
114 | 111 | } catch (Exception e) {
|
115 |
| - inspectException(e); |
| 112 | + throw e.getCause(); |
116 | 113 | }
|
117 | 114 | }
|
118 | 115 |
|
119 |
| - @Test(groups = "standalone", enabled = false) |
120 |
| - public void digestPreemptiveAuthTimeoutTest() throws Exception { |
| 116 | + @Test(expectedExceptions = TimeoutException.class, enabled = false) |
| 117 | + public void digestPreemptiveAuthTimeoutTest() throws Throwable { |
121 | 118 | try (AsyncHttpClient client = newClient()) {
|
122 |
| - Future<Response> f = execute(client, server2, true); |
123 |
| - f.get(); |
124 |
| - fail("expected timeout"); |
| 119 | + execute(client, false, true).get(LONG_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
125 | 120 | } catch (Exception e) {
|
126 |
| - inspectException(e); |
| 121 | + throw e.getCause(); |
127 | 122 | }
|
128 | 123 | }
|
129 | 124 |
|
130 |
| - @Test(groups = "standalone", enabled = false) |
131 |
| - public void basicFutureAuthTimeoutTest() throws Exception { |
| 125 | + @Test(expectedExceptions = TimeoutException.class) |
| 126 | + public void basicAuthFutureTimeoutTest() throws Throwable { |
132 | 127 | try (AsyncHttpClient client = newClient()) {
|
133 |
| - Future<Response> f = execute(client, server, false); |
134 |
| - f.get(1, TimeUnit.SECONDS); |
135 |
| - fail("expected timeout"); |
136 |
| - } catch (Exception e) { |
137 |
| - inspectException(e); |
| 128 | + execute(client, true, false).get(SHORT_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
138 | 129 | }
|
139 | 130 | }
|
140 | 131 |
|
141 |
| - @Test(groups = "standalone", enabled = false) |
142 |
| - public void basicFuturePreemptiveAuthTimeoutTest() throws Exception { |
| 132 | + @Test(expectedExceptions = TimeoutException.class) |
| 133 | + public void basicPreemptiveAuthFutureTimeoutTest() throws Throwable { |
143 | 134 | try (AsyncHttpClient client = newClient()) {
|
144 |
| - Future<Response> f = execute(client, server, true); |
145 |
| - f.get(1, TimeUnit.SECONDS); |
146 |
| - fail("expected timeout"); |
147 |
| - } catch (Exception e) { |
148 |
| - inspectException(e); |
| 135 | + execute(client, true, true).get(SHORT_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
149 | 136 | }
|
150 | 137 | }
|
151 | 138 |
|
152 |
| - @Test(groups = "standalone", enabled = false) |
153 |
| - public void digestFutureAuthTimeoutTest() throws Exception { |
| 139 | + @Test(expectedExceptions = TimeoutException.class) |
| 140 | + public void digestAuthFutureTimeoutTest() throws Throwable { |
154 | 141 | try (AsyncHttpClient client = newClient()) {
|
155 |
| - Future<Response> f = execute(client, server2, false); |
156 |
| - f.get(1, TimeUnit.SECONDS); |
157 |
| - fail("expected timeout"); |
158 |
| - } catch (Exception e) { |
159 |
| - inspectException(e); |
| 142 | + execute(client, false, false).get(SHORT_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
160 | 143 | }
|
161 | 144 | }
|
162 | 145 |
|
163 |
| - @Test(groups = "standalone", enabled = false) |
164 |
| - public void digestFuturePreemptiveAuthTimeoutTest() throws Exception { |
| 146 | + @Test(expectedExceptions = TimeoutException.class, enabled = false) |
| 147 | + public void digestPreemptiveAuthFutureTimeoutTest() throws Throwable { |
165 | 148 | try (AsyncHttpClient client = newClient()) {
|
166 |
| - Future<Response> f = execute(client, server2, true); |
167 |
| - f.get(1, TimeUnit.SECONDS); |
168 |
| - fail("expected timeout"); |
169 |
| - } catch (Exception e) { |
170 |
| - inspectException(e); |
| 149 | + execute(client, false, true).get(SHORT_FUTURE_TIMEOUT, TimeUnit.MILLISECONDS); |
171 | 150 | }
|
172 | 151 | }
|
173 | 152 |
|
174 |
| - protected void inspectException(Throwable t) { |
175 |
| - assertEquals(t.getCause(), RemotelyClosedException.INSTANCE); |
176 |
| - } |
177 |
| - |
178 | 153 | private AsyncHttpClient newClient() {
|
179 |
| - return asyncHttpClient(config().setPooledConnectionIdleTimeout(2000).setConnectTimeout(20000).setRequestTimeout(2000)); |
| 154 | + return asyncHttpClient(config().setRequestTimeout(REQUEST_TIMEOUT)); |
180 | 155 | }
|
181 | 156 |
|
182 |
| - protected Future<Response> execute(AsyncHttpClient client, Server server, boolean preemptive) throws IOException { |
183 |
| - return client.prepareGet(getTargetUrl()).setRealm(realm(preemptive)).setHeader("X-Content", "Test").execute(); |
184 |
| - } |
| 157 | + protected Future<Response> execute(AsyncHttpClient client, boolean basic, boolean preemptive) throws IOException { |
| 158 | + Realm.Builder realm; |
| 159 | + String url; |
| 160 | + |
| 161 | + if (basic) { |
| 162 | + realm = basicAuthRealm(USER, ADMIN); |
| 163 | + url = getTargetUrl(); |
| 164 | + } else { |
| 165 | + realm = digestAuthRealm(USER, ADMIN); |
| 166 | + url = getTargetUrl2(); |
| 167 | + if (preemptive) { |
| 168 | + realm.setNonce("fFDVc60re9zt8fFDvht0tNrYuvqrcchN"); |
| 169 | + } |
| 170 | + } |
185 | 171 |
|
186 |
| - private Realm realm(boolean preemptive) { |
187 |
| - return basicAuthRealm(USER, ADMIN).setUsePreemptiveAuth(preemptive).build(); |
| 172 | + return client.prepareGet(url).setRealm(realm.setUsePreemptiveAuth(preemptive).build()).setHeader("X-Content", "Test").execute(); |
188 | 173 | }
|
189 | 174 |
|
190 | 175 | @Override
|
191 | 176 | protected String getTargetUrl() {
|
192 | 177 | return "http://localhost:" + port1 + "/";
|
193 | 178 | }
|
194 | 179 |
|
| 180 | + @Override |
| 181 | + protected String getTargetUrl2() { |
| 182 | + return "http://localhost:" + port2 + "/"; |
| 183 | + } |
| 184 | + |
195 | 185 | @Override
|
196 | 186 | public AbstractHandler configureHandler() throws Exception {
|
197 | 187 | return new IncompleteResponseHandler();
|
|
0 commit comments