Skip to content

Commit 79016a7

Browse files
committed
Re-enable most of AuthTimeoutTest tests
1 parent dd0775e commit 79016a7

File tree

1 file changed

+54
-64
lines changed

1 file changed

+54
-64
lines changed

client/src/test/java/org/asynchttpclient/AuthTimeoutTest.java

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
import static java.nio.charset.StandardCharsets.UTF_8;
1717
import static org.asynchttpclient.Dsl.*;
1818
import static org.asynchttpclient.test.TestUtils.*;
19-
import static org.testng.Assert.*;
2019

2120
import java.io.IOException;
2221
import java.io.OutputStream;
2322
import java.util.concurrent.Future;
2423
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.TimeoutException;
2525

2626
import javax.servlet.ServletException;
2727
import javax.servlet.http.HttpServletRequest;
2828
import javax.servlet.http.HttpServletResponse;
2929

30-
import org.asynchttpclient.exception.RemotelyClosedException;
3130
import org.eclipse.jetty.server.Request;
3231
import org.eclipse.jetty.server.Server;
3332
import org.eclipse.jetty.server.ServerConnector;
@@ -38,6 +37,10 @@
3837

3938
public class AuthTimeoutTest extends AbstractBasicTest {
4039

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+
4144
private Server server2;
4245

4346
@BeforeClass(alwaysRun = true)
@@ -83,115 +86,102 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
8386
}
8487
}
8588

86-
@Test(groups = "standalone", enabled = false)
87-
public void basicAuthTimeoutTest() throws Exception {
89+
@Test(expectedExceptions = TimeoutException.class)
90+
public void basicAuthTimeoutTest() throws Throwable {
8891
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);
9293
} catch (Exception e) {
93-
inspectException(e);
94+
throw e.getCause();
9495
}
9596
}
9697

97-
@Test(groups = "standalone", enabled = false)
98-
public void basicPreemptiveAuthTimeoutTest() throws Exception {
98+
@Test(expectedExceptions = TimeoutException.class)
99+
public void basicPreemptiveAuthTimeoutTest() throws Throwable {
99100
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);
103102
} catch (Exception e) {
104-
inspectException(e);
103+
throw e.getCause();
105104
}
106105
}
107106

108-
@Test(groups = "standalone", enabled = false)
109-
public void digestAuthTimeoutTest() throws Exception {
107+
@Test(expectedExceptions = TimeoutException.class)
108+
public void digestAuthTimeoutTest() throws Throwable {
110109
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);
114111
} catch (Exception e) {
115-
inspectException(e);
112+
throw e.getCause();
116113
}
117114
}
118115

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 {
121118
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);
125120
} catch (Exception e) {
126-
inspectException(e);
121+
throw e.getCause();
127122
}
128123
}
129124

130-
@Test(groups = "standalone", enabled = false)
131-
public void basicFutureAuthTimeoutTest() throws Exception {
125+
@Test(expectedExceptions = TimeoutException.class)
126+
public void basicAuthFutureTimeoutTest() throws Throwable {
132127
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);
138129
}
139130
}
140131

141-
@Test(groups = "standalone", enabled = false)
142-
public void basicFuturePreemptiveAuthTimeoutTest() throws Exception {
132+
@Test(expectedExceptions = TimeoutException.class)
133+
public void basicPreemptiveAuthFutureTimeoutTest() throws Throwable {
143134
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);
149136
}
150137
}
151138

152-
@Test(groups = "standalone", enabled = false)
153-
public void digestFutureAuthTimeoutTest() throws Exception {
139+
@Test(expectedExceptions = TimeoutException.class)
140+
public void digestAuthFutureTimeoutTest() throws Throwable {
154141
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);
160143
}
161144
}
162145

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 {
165148
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);
171150
}
172151
}
173152

174-
protected void inspectException(Throwable t) {
175-
assertEquals(t.getCause(), RemotelyClosedException.INSTANCE);
176-
}
177-
178153
private AsyncHttpClient newClient() {
179-
return asyncHttpClient(config().setPooledConnectionIdleTimeout(2000).setConnectTimeout(20000).setRequestTimeout(2000));
154+
return asyncHttpClient(config().setRequestTimeout(REQUEST_TIMEOUT));
180155
}
181156

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+
}
185171

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();
188173
}
189174

190175
@Override
191176
protected String getTargetUrl() {
192177
return "http://localhost:" + port1 + "/";
193178
}
194179

180+
@Override
181+
protected String getTargetUrl2() {
182+
return "http://localhost:" + port2 + "/";
183+
}
184+
195185
@Override
196186
public AbstractHandler configureHandler() throws Exception {
197187
return new IncompleteResponseHandler();

0 commit comments

Comments
 (0)