49
49
import static org .testng .Assert .assertNotNull ;
50
50
import static org .testng .Assert .fail ;
51
51
52
- public abstract class AuthTimeoutTest
53
- extends AbstractBasicTest {
52
+ public abstract class AuthTimeoutTest extends AbstractBasicTest {
54
53
55
54
private final static String user = "user" ;
56
55
57
56
private final static String admin = "admin" ;
58
57
59
58
protected AsyncHttpClient client ;
60
59
61
- public void setUpServer (String auth )
62
- throws Exception {
60
+ public void setUpServer (String auth ) throws Exception {
63
61
server = new Server ();
64
62
Logger root = Logger .getRootLogger ();
65
63
root .setLevel (Level .DEBUG );
@@ -78,7 +76,7 @@ public void setUpServer(String auth)
78
76
79
77
Constraint constraint = new Constraint ();
80
78
constraint .setName (auth );
81
- constraint .setRoles (new String []{ user , admin });
79
+ constraint .setRoles (new String [] { user , admin });
82
80
constraint .setAuthenticate (true );
83
81
84
82
ConstraintMapping mapping = new ConstraintMapping ();
@@ -105,10 +103,8 @@ public void setUpServer(String auth)
105
103
log .info ("Local HTTP server started successfully" );
106
104
}
107
105
108
- private class SimpleHandler
109
- extends AbstractHandler {
110
- public void handle (String s , Request r , HttpServletRequest request , HttpServletResponse response )
111
- throws IOException , ServletException {
106
+ private class SimpleHandler extends AbstractHandler {
107
+ public void handle (String s , Request r , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
112
108
113
109
// NOTE: handler sends less bytes than are given in Content-Length, which should lead to timeout
114
110
@@ -128,150 +124,135 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
128
124
}
129
125
}
130
126
131
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
132
- public void basicAuthTimeoutTest ()
133
- throws Exception {
127
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
128
+ public void basicAuthTimeoutTest () throws Exception {
134
129
setUpServer (Constraint .__BASIC_AUTH );
135
-
136
- Future <Response > f = execute (false );
137
130
try {
131
+ Future <Response > f = execute (false );
138
132
f .get ();
139
133
fail ("expected timeout" );
140
- }
141
- catch (Exception e ) {
134
+ } catch (Exception e ) {
142
135
inspectException (e );
136
+ } finally {
137
+ client .close ();
143
138
}
144
- client .close ();
145
139
}
146
140
147
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
148
- public void basicPreemptiveAuthTimeoutTest ()
149
- throws Exception {
141
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
142
+ public void basicPreemptiveAuthTimeoutTest () throws Exception {
150
143
setUpServer (Constraint .__BASIC_AUTH );
151
-
152
- Future <Response > f = execute (true );
153
144
try {
145
+ Future <Response > f = execute (true );
154
146
f .get ();
155
147
fail ("expected timeout" );
156
- }
157
- catch (Exception e ) {
148
+ } catch (Exception e ) {
158
149
inspectException (e );
150
+ } finally {
151
+ client .close ();
159
152
}
160
- client .close ();
161
153
}
162
154
163
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
164
- public void digestAuthTimeoutTest ()
165
- throws Exception {
155
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
156
+ public void digestAuthTimeoutTest () throws Exception {
166
157
setUpServer (Constraint .__DIGEST_AUTH );
167
158
168
- Future <Response > f = execute (false );
169
159
try {
160
+ Future <Response > f = execute (false );
170
161
f .get ();
171
162
fail ("expected timeout" );
172
- }
173
- catch (Exception e ) {
163
+ } catch (Exception e ) {
174
164
inspectException (e );
165
+ } finally {
166
+ client .close ();
175
167
}
176
- client .close ();
177
168
}
178
169
179
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
180
- public void digestPreemptiveAuthTimeoutTest ()
181
- throws Exception {
170
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
171
+ public void digestPreemptiveAuthTimeoutTest () throws Exception {
182
172
setUpServer (Constraint .__DIGEST_AUTH );
183
173
184
- Future <Response > f = execute (true );
185
174
try {
175
+ Future <Response > f = execute (true );
186
176
f .get ();
187
177
fail ("expected timeout" );
188
- }
189
- catch (Exception e ) {
178
+ } catch (Exception e ) {
190
179
inspectException (e );
180
+ } finally {
181
+ client .close ();
191
182
}
192
- client .close ();
193
183
}
194
184
195
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
196
- public void basicFutureAuthTimeoutTest ()
197
- throws Exception {
185
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
186
+ public void basicFutureAuthTimeoutTest () throws Exception {
198
187
setUpServer (Constraint .__BASIC_AUTH );
199
188
200
- Future <Response > f = execute (false );
201
189
try {
190
+ Future <Response > f = execute (false );
202
191
f .get (1 , TimeUnit .SECONDS );
203
192
fail ("expected timeout" );
204
- }
205
- catch (Exception e ) {
193
+ } catch (Exception e ) {
206
194
inspectException (e );
195
+ } finally {
196
+ client .close ();
207
197
}
208
- client .close ();
209
198
}
210
199
211
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
212
- public void basicFuturePreemptiveAuthTimeoutTest ()
213
- throws Exception {
200
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
201
+ public void basicFuturePreemptiveAuthTimeoutTest () throws Exception {
214
202
setUpServer (Constraint .__BASIC_AUTH );
215
203
216
- Future <Response > f = execute (true );
217
204
try {
205
+ Future <Response > f = execute (true );
218
206
f .get (1 , TimeUnit .SECONDS );
219
207
fail ("expected timeout" );
220
- }
221
- catch (Exception e ) {
208
+ } catch (Exception e ) {
222
209
inspectException (e );
210
+ } finally {
211
+ client .close ();
223
212
}
224
- client .close ();
225
213
}
226
214
227
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
228
- public void digestFutureAuthTimeoutTest ()
229
- throws Exception {
215
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
216
+ public void digestFutureAuthTimeoutTest () throws Exception {
230
217
setUpServer (Constraint .__DIGEST_AUTH );
231
218
232
- Future <Response > f = execute (false );
233
219
try {
220
+ Future <Response > f = execute (false );
234
221
f .get (1 , TimeUnit .SECONDS );
235
222
fail ("expected timeout" );
236
- }
237
- catch (Exception e ) {
223
+ } catch (Exception e ) {
238
224
inspectException (e );
225
+ } finally {
226
+ client .close ();
239
227
}
240
- client .close ();
241
228
}
242
229
243
- @ Test (groups = {"standalone" , "default_provider" }, enabled = false )
244
- public void digestFuturePreemptiveAuthTimeoutTest ()
245
- throws Exception {
230
+ @ Test (groups = { "standalone" , "default_provider" }, enabled = false )
231
+ public void digestFuturePreemptiveAuthTimeoutTest () throws Exception {
246
232
setUpServer (Constraint .__DIGEST_AUTH );
247
233
248
- Future <Response > f = execute (true );
249
234
try {
235
+ Future <Response > f = execute (true );
250
236
f .get (1 , TimeUnit .SECONDS );
251
237
fail ("expected timeout" );
252
- }
253
- catch (Exception e ) {
238
+ } catch (Exception e ) {
254
239
inspectException (e );
240
+ } finally {
241
+ client .close ();
255
242
}
256
- client .close ();
257
243
}
258
244
259
245
protected void inspectException (Throwable t ) {
260
246
assertNotNull (t .getCause ());
261
247
assertEquals (t .getCause ().getClass (), IOException .class );
262
- if (!t .getCause ().getMessage ().startsWith ("Remotely Closed" )){
248
+ if (!t .getCause ().getMessage ().startsWith ("Remotely Closed" )) {
263
249
fail ();
264
- };
250
+ }
265
251
}
266
252
267
- protected Future <Response > execute (boolean preemptive )
268
- throws IOException {
269
- client =
270
- getAsyncHttpClient (
271
- new AsyncHttpClientConfig .Builder ().setIdleConnectionInPoolTimeoutInMs (2000 ).setConnectionTimeoutInMs (20000 ).setRequestTimeoutInMs (2000 ).build ());
272
- AsyncHttpClient .BoundRequestBuilder r =
273
- client .prepareGet (getTargetUrl ()).setRealm (realm (preemptive )).setHeader ("X-Content" ,
274
- "Test" );
253
+ protected Future <Response > execute (boolean preemptive ) throws IOException {
254
+ client = getAsyncHttpClient (new AsyncHttpClientConfig .Builder ().setIdleConnectionInPoolTimeoutInMs (2000 ).setConnectionTimeoutInMs (20000 ).setRequestTimeoutInMs (2000 ).build ());
255
+ AsyncHttpClient .BoundRequestBuilder r = client .prepareGet (getTargetUrl ()).setRealm (realm (preemptive )).setHeader ("X-Content" , "Test" );
275
256
Future <Response > f = r .execute ();
276
257
return f ;
277
258
}
@@ -286,8 +267,7 @@ protected String getTargetUrl() {
286
267
}
287
268
288
269
@ Override
289
- public AbstractHandler configureHandler ()
290
- throws Exception {
270
+ public AbstractHandler configureHandler () throws Exception {
291
271
return new SimpleHandler ();
292
272
}
293
273
}
0 commit comments