22
22
import com .ning .http .client .HttpResponseHeaders ;
23
23
import com .ning .http .client .HttpResponseStatus ;
24
24
import com .ning .http .client .Realm ;
25
+ import com .ning .http .client .Realm .AuthScheme ;
25
26
import com .ning .http .client .Response ;
26
27
import com .ning .http .client .generators .InputStreamBodyGenerator ;
27
28
@@ -72,6 +73,8 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
72
73
protected final static String admin = "admin" ;
73
74
74
75
private Server server2 ;
76
+ private Server serverNoAuth ;
77
+ private int portNoAuth ;
75
78
76
79
@ BeforeClass (alwaysRun = true )
77
80
@ Override
@@ -201,6 +204,24 @@ private void stopSecondServer() throws Exception {
201
204
server2 .stop ();
202
205
}
203
206
207
+ private void setUpServerNoAuth () throws Exception {
208
+ serverNoAuth = new Server ();
209
+ portNoAuth = findFreePort ();
210
+
211
+ Connector listener = new SelectChannelConnector ();
212
+ listener .setHost ("127.0.0.1" );
213
+ listener .setPort (portNoAuth );
214
+
215
+ serverNoAuth .addConnector (listener );
216
+
217
+ serverNoAuth .setHandler (new SimpleHandler ());
218
+ serverNoAuth .start ();
219
+ }
220
+
221
+ private void stopServerNoAuth () throws Exception {
222
+ serverNoAuth .stop ();
223
+ }
224
+
204
225
private class RedirectHandler extends AbstractHandler {
205
226
206
227
public void handle (String s , Request r , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
@@ -231,7 +252,7 @@ public void handle(String s, Request r, HttpServletRequest request, HttpServletR
231
252
232
253
private class SimpleHandler extends AbstractHandler {
233
254
public void handle (String s , Request r , HttpServletRequest request , HttpServletResponse response ) throws IOException , ServletException {
234
-
255
+
235
256
if (request .getHeader ("X-401" ) != null ) {
236
257
response .setStatus (401 );
237
258
response .getOutputStream ().flush ();
@@ -307,6 +328,10 @@ protected String getTargetUrl2() {
307
328
return "http://127.0.0.1:" + port2 + "/uff" ;
308
329
}
309
330
331
+ protected String getTargetUrlNoAuth () {
332
+ return "http://127.0.0.1:" + portNoAuth + "/" ;
333
+ }
334
+
310
335
@ Test (groups = { "standalone" , "default_provider" })
311
336
public void basic401Test () throws IOException , ExecutionException , TimeoutException , InterruptedException {
312
337
AsyncHttpClient client = getAsyncHttpClient (null );
@@ -351,10 +376,13 @@ public Integer onCompleted() throws Exception {
351
376
}
352
377
353
378
@ Test (groups = { "standalone" , "default_provider" })
354
- public void basicAuthTestPreemtiveTest () throws IOException , ExecutionException , TimeoutException , InterruptedException {
379
+ public void basicAuthTestPreemtiveTest () throws Exception , ExecutionException , TimeoutException , InterruptedException {
355
380
AsyncHttpClient client = getAsyncHttpClient (null );
356
381
try {
357
- AsyncHttpClient .BoundRequestBuilder r = client .prepareGet (getTargetUrl ()).setRealm ((new Realm .RealmBuilder ()).setPrincipal (user ).setPassword (admin ).setUsePreemptiveAuth (true ).build ());
382
+ setUpServerNoAuth ();
383
+
384
+ AsyncHttpClient .BoundRequestBuilder r = client .prepareGet (getTargetUrlNoAuth ())
385
+ .setRealm ((new Realm .RealmBuilder ()).setScheme (AuthScheme .BASIC ).setPrincipal (user ).setPassword (admin ).setUsePreemptiveAuth (true ).build ());
358
386
359
387
Future <Response > f = r .execute ();
360
388
Response resp = f .get (3 , TimeUnit .SECONDS );
@@ -363,6 +391,7 @@ public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException,
363
391
assertEquals (resp .getStatusCode (), HttpServletResponse .SC_OK );
364
392
} finally {
365
393
client .close ();
394
+ stopServerNoAuth ();
366
395
}
367
396
}
368
397
0 commit comments