34
34
import org .asynchttpclient .HttpResponseHeaders ;
35
35
import org .asynchttpclient .HttpResponseStatus ;
36
36
import org .asynchttpclient .Realm ;
37
+ import org .asynchttpclient .Realm .AuthScheme ;
37
38
import org .asynchttpclient .Response ;
38
39
import org .asynchttpclient .SimpleAsyncHttpClient ;
39
40
import org .asynchttpclient .consumers .AppendableBodyConsumer ;
@@ -64,6 +65,8 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
64
65
protected static final String MY_MESSAGE = "my message" ;
65
66
66
67
private Server server2 ;
68
+ private Server serverNoAuth ;
69
+ private int portNoAuth ;
67
70
68
71
public abstract String getProviderClass ();
69
72
@@ -72,6 +75,7 @@ public abstract class BasicAuthTest extends AbstractBasicTest {
72
75
public void setUpGlobal () throws Exception {
73
76
port1 = findFreePort ();
74
77
port2 = findFreePort ();
78
+ portNoAuth = findFreePort ();
75
79
76
80
server = newJettyHttpServer (port1 );
77
81
addBasicAuthHandler (server , false , configureHandler ());
@@ -81,13 +85,19 @@ public void setUpGlobal() throws Exception {
81
85
addDigestAuthHandler (server2 , true , new RedirectHandler ());
82
86
server2 .start ();
83
87
88
+ // need noAuth server to verify the preemptive auth mode (see basicAuthTetPreemtiveTest)
89
+ serverNoAuth = newJettyHttpServer (portNoAuth );
90
+ serverNoAuth .setHandler (new SimpleHandler ());
91
+ serverNoAuth .start ();
92
+
84
93
logger .info ("Local HTTP server started successfully" );
85
94
}
86
95
87
96
@ AfterClass (alwaysRun = true )
88
97
public void tearDownGlobal () throws Exception {
89
98
super .tearDownGlobal ();
90
99
server2 .stop ();
100
+ serverNoAuth .stop ();
91
101
}
92
102
93
103
@ Override
@@ -100,6 +110,10 @@ protected String getTargetUrl2() {
100
110
return "http://127.0.0.1:" + port2 + "/uff" ;
101
111
}
102
112
113
+ protected String getTargetUrlNoAuth () {
114
+ return "http://127.0.0.1:" + portNoAuth + "/" ;
115
+ }
116
+
103
117
@ Override
104
118
public AbstractHandler configureHandler () throws Exception {
105
119
return new SimpleHandler ();
@@ -247,8 +261,10 @@ public Integer onCompleted() throws Exception {
247
261
public void basicAuthTestPreemtiveTest () throws IOException , ExecutionException , TimeoutException , InterruptedException {
248
262
AsyncHttpClient client = getAsyncHttpClient (null );
249
263
try {
250
- Future <Response > f = client .prepareGet (getTargetUrl ())//
251
- .setRealm ((new Realm .RealmBuilder ()).setPrincipal (USER ).setPassword (ADMIN ).setUsePreemptiveAuth (true ).build ())//
264
+ // send the request to the no-auth endpoint to be able to verify the auth header is
265
+ // really sent preemptively for the initial call.
266
+ Future <Response > f = client .prepareGet (getTargetUrlNoAuth ())//
267
+ .setRealm ((new Realm .RealmBuilder ()).setScheme (AuthScheme .BASIC ).setPrincipal (USER ).setPassword (ADMIN ).setUsePreemptiveAuth (true ).build ())//
252
268
.execute ();
253
269
254
270
Response resp = f .get (3 , TimeUnit .SECONDS );
0 commit comments