|
16 | 16 | importstaticjava.nio.charset.StandardCharsets.UTF_8; |
17 | 17 | importstaticorg.asynchttpclient.Dsl.*; |
18 | 18 | importstaticorg.asynchttpclient.test.TestUtils.*; |
19 | | -importstaticorg.testng.Assert.*; |
20 | 19 |
|
21 | 20 | importjava.io.IOException; |
22 | 21 | importjava.io.OutputStream; |
23 | 22 | importjava.util.concurrent.Future; |
24 | 23 | importjava.util.concurrent.TimeUnit; |
| 24 | +importjava.util.concurrent.TimeoutException; |
25 | 25 |
|
26 | 26 | importjavax.servlet.ServletException; |
27 | 27 | importjavax.servlet.http.HttpServletRequest; |
28 | 28 | importjavax.servlet.http.HttpServletResponse; |
29 | 29 |
|
30 | | -importorg.asynchttpclient.exception.RemotelyClosedException; |
31 | 30 | importorg.eclipse.jetty.server.Request; |
32 | 31 | importorg.eclipse.jetty.server.Server; |
33 | 32 | importorg.eclipse.jetty.server.ServerConnector; |
|
38 | 37 |
|
39 | 38 | publicclassAuthTimeoutTestextendsAbstractBasicTest { |
40 | 39 |
|
| 40 | +privatestaticfinalintREQUEST_TIMEOUT =1000; |
| 41 | +privatestaticfinalintSHORT_FUTURE_TIMEOUT =500;// shorter than REQUEST_TIMEOUT |
| 42 | +privatestaticfinalintLONG_FUTURE_TIMEOUT =1500;// longer than REQUEST_TIMEOUT |
| 43 | + |
41 | 44 | privateServerserver2; |
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 | | -publicvoidbasicAuthTimeoutTest()throwsException { |
| 89 | +@Test(expectedExceptions =TimeoutException.class) |
| 90 | +publicvoidbasicAuthTimeoutTest()throwsThrowable { |
88 | 91 | try (AsyncHttpClientclient =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 (Exceptione) { |
93 | | -inspectException(e); |
| 94 | +throwe.getCause(); |
94 | 95 | } |
95 | 96 | } |
96 | 97 |
|
97 | | -@Test(groups ="standalone",enabled =false) |
98 | | -publicvoidbasicPreemptiveAuthTimeoutTest()throwsException { |
| 98 | +@Test(expectedExceptions =TimeoutException.class) |
| 99 | +publicvoidbasicPreemptiveAuthTimeoutTest()throwsThrowable { |
99 | 100 | try (AsyncHttpClientclient =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 (Exceptione) { |
104 | | -inspectException(e); |
| 103 | +throwe.getCause(); |
105 | 104 | } |
106 | 105 | } |
107 | 106 |
|
108 | | -@Test(groups ="standalone",enabled =false) |
109 | | -publicvoiddigestAuthTimeoutTest()throwsException { |
| 107 | +@Test(expectedExceptions =TimeoutException.class) |
| 108 | +publicvoiddigestAuthTimeoutTest()throwsThrowable { |
110 | 109 | try (AsyncHttpClientclient =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 (Exceptione) { |
115 | | -inspectException(e); |
| 112 | +throwe.getCause(); |
116 | 113 | } |
117 | 114 | } |
118 | 115 |
|
119 | | -@Test(groups ="standalone",enabled =false) |
120 | | -publicvoiddigestPreemptiveAuthTimeoutTest()throwsException { |
| 116 | +@Test(expectedExceptions =TimeoutException.class,enabled =false) |
| 117 | +publicvoiddigestPreemptiveAuthTimeoutTest()throwsThrowable { |
121 | 118 | try (AsyncHttpClientclient =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 (Exceptione) { |
126 | | -inspectException(e); |
| 121 | +throwe.getCause(); |
127 | 122 | } |
128 | 123 | } |
129 | 124 |
|
130 | | -@Test(groups ="standalone",enabled =false) |
131 | | -publicvoidbasicFutureAuthTimeoutTest()throwsException { |
| 125 | +@Test(expectedExceptions =TimeoutException.class) |
| 126 | +publicvoidbasicAuthFutureTimeoutTest()throwsThrowable { |
132 | 127 | try (AsyncHttpClientclient =newClient()) { |
133 | | -Future<Response>f =execute(client,server,false); |
134 | | -f.get(1,TimeUnit.SECONDS); |
135 | | -fail("expected timeout"); |
136 | | - }catch (Exceptione) { |
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 | | -publicvoidbasicFuturePreemptiveAuthTimeoutTest()throwsException { |
| 132 | +@Test(expectedExceptions =TimeoutException.class) |
| 133 | +publicvoidbasicPreemptiveAuthFutureTimeoutTest()throwsThrowable { |
143 | 134 | try (AsyncHttpClientclient =newClient()) { |
144 | | -Future<Response>f =execute(client,server,true); |
145 | | -f.get(1,TimeUnit.SECONDS); |
146 | | -fail("expected timeout"); |
147 | | - }catch (Exceptione) { |
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 | | -publicvoiddigestFutureAuthTimeoutTest()throwsException { |
| 139 | +@Test(expectedExceptions =TimeoutException.class) |
| 140 | +publicvoiddigestAuthFutureTimeoutTest()throwsThrowable { |
154 | 141 | try (AsyncHttpClientclient =newClient()) { |
155 | | -Future<Response>f =execute(client,server2,false); |
156 | | -f.get(1,TimeUnit.SECONDS); |
157 | | -fail("expected timeout"); |
158 | | - }catch (Exceptione) { |
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 | | -publicvoiddigestFuturePreemptiveAuthTimeoutTest()throwsException { |
| 146 | +@Test(expectedExceptions =TimeoutException.class,enabled =false) |
| 147 | +publicvoiddigestPreemptiveAuthFutureTimeoutTest()throwsThrowable { |
165 | 148 | try (AsyncHttpClientclient =newClient()) { |
166 | | -Future<Response>f =execute(client,server2,true); |
167 | | -f.get(1,TimeUnit.SECONDS); |
168 | | -fail("expected timeout"); |
169 | | - }catch (Exceptione) { |
170 | | -inspectException(e); |
| 149 | +execute(client,false,true).get(SHORT_FUTURE_TIMEOUT,TimeUnit.MILLISECONDS); |
171 | 150 | } |
172 | 151 | } |
173 | 152 |
|
174 | | -protectedvoidinspectException(Throwablet) { |
175 | | -assertEquals(t.getCause(),RemotelyClosedException.INSTANCE); |
176 | | - } |
177 | | - |
178 | 153 | privateAsyncHttpClientnewClient() { |
179 | | -returnasyncHttpClient(config().setPooledConnectionIdleTimeout(2000).setConnectTimeout(20000).setRequestTimeout(2000)); |
| 154 | +returnasyncHttpClient(config().setRequestTimeout(REQUEST_TIMEOUT)); |
180 | 155 | } |
181 | 156 |
|
182 | | -protectedFuture<Response>execute(AsyncHttpClientclient,Serverserver,booleanpreemptive)throwsIOException { |
183 | | -returnclient.prepareGet(getTargetUrl()).setRealm(realm(preemptive)).setHeader("X-Content","Test").execute(); |
184 | | - } |
| 157 | +protectedFuture<Response>execute(AsyncHttpClientclient,booleanbasic,booleanpreemptive)throwsIOException { |
| 158 | +Realm.Builderrealm; |
| 159 | +Stringurl; |
| 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 | | -privateRealmrealm(booleanpreemptive) { |
187 | | -returnbasicAuthRealm(USER,ADMIN).setUsePreemptiveAuth(preemptive).build(); |
| 172 | +returnclient.prepareGet(url).setRealm(realm.setUsePreemptiveAuth(preemptive).build()).setHeader("X-Content","Test").execute(); |
188 | 173 | } |
189 | 174 |
|
190 | 175 | @Override |
191 | 176 | protectedStringgetTargetUrl() { |
192 | 177 | return"http://localhost:" +port1 +"/"; |
193 | 178 | } |
194 | 179 |
|
| 180 | +@Override |
| 181 | +protectedStringgetTargetUrl2() { |
| 182 | +return"http://localhost:" +port2 +"/"; |
| 183 | + } |
| 184 | + |
195 | 185 | @Override |
196 | 186 | publicAbstractHandlerconfigureHandler()throwsException { |
197 | 187 | returnnewIncompleteResponseHandler(); |
|