14
14
15
15
import io .netty .handler .codec .http .DefaultHttpHeaders ;
16
16
import io .netty .handler .codec .http .EmptyHttpHeaders ;
17
- import lombok .val ;
18
- import okhttp3 .MediaType ;
19
- import okhttp3 .Request ;
20
- import okhttp3 .RequestBody ;
17
+ import lombok .*;
18
+ import lombok .extern .slf4j .Slf4j ;
19
+ import okhttp3 .*;
21
20
import org .asynchttpclient .AsyncCompletionHandler ;
22
21
import org .asynchttpclient .AsyncHttpClient ;
22
+ import org .asynchttpclient .AsyncHttpClientConfig ;
23
23
import org .asynchttpclient .BoundRequestBuilder ;
24
+ import org .asynchttpclient .DefaultAsyncHttpClientConfig ;
24
25
import org .asynchttpclient .Response ;
25
26
import org .mockito .ArgumentCaptor ;
26
27
import org .testng .Assert ;
38
39
import java .util .function .Consumer ;
39
40
import java .util .function .Supplier ;
40
41
42
+ import static java .util .concurrent .TimeUnit .SECONDS ;
41
43
import static org .asynchttpclient .extras .retrofit .AsyncHttpClientCall .runConsumer ;
42
44
import static org .asynchttpclient .extras .retrofit .AsyncHttpClientCall .runConsumers ;
43
45
import static org .mockito .ArgumentMatchers .any ;
46
48
import static org .testng .Assert .assertNotEquals ;
47
49
import static org .testng .Assert .assertTrue ;
48
50
51
+ @ Slf4j
49
52
public class AsyncHttpClientCallTest {
50
53
static final Request REQUEST = new Request .Builder ().url ("http://www.google.com/" ).build ();
54
+ static final DefaultAsyncHttpClientConfig DEFAULT_AHC_CONFIG = new DefaultAsyncHttpClientConfig .Builder ()
55
+ .setRequestTimeout (1_000 )
56
+ .build ();
51
57
52
58
private AsyncHttpClient httpClient ;
53
59
private Supplier <AsyncHttpClient > httpClientSupplier = () -> httpClient ;
54
60
55
61
@ BeforeMethod
56
62
void setup () {
57
- this .httpClient = mock (AsyncHttpClient .class );
63
+ httpClient = mock (AsyncHttpClient .class );
64
+ when (httpClient .getConfig ()).thenReturn (DEFAULT_AHC_CONFIG );
58
65
}
59
66
60
67
@ Test (expectedExceptions = NullPointerException .class , dataProvider = "first" )
@@ -108,7 +115,6 @@ void shouldInvokeConsumersOnEachExecution(Consumer<AsyncCompletionHandler<?>> ha
108
115
.onRequestFailure (t -> numFailed .incrementAndGet ())
109
116
.onRequestSuccess (r -> numOk .incrementAndGet ())
110
117
.requestCustomizer (rb -> numRequestCustomizer .incrementAndGet ())
111
- .executeTimeoutMillis (1000 )
112
118
.build ();
113
119
114
120
// when
@@ -245,13 +251,12 @@ public void contentTypeHeaderIsPassedInRequest() throws Exception {
245
251
Request request = requestWithBody ();
246
252
247
253
ArgumentCaptor <org .asynchttpclient .Request > capture = ArgumentCaptor .forClass (org .asynchttpclient .Request .class );
248
- AsyncHttpClient client = mock (AsyncHttpClient .class );
249
254
250
- givenResponseIsProduced (client , aResponse ());
255
+ givenResponseIsProduced (httpClient , aResponse ());
251
256
252
- whenRequestIsMade (client , request );
257
+ whenRequestIsMade (httpClient , request );
253
258
254
- verify (client ).executeRequest (capture .capture (), any ());
259
+ verify (httpClient ).executeRequest (capture .capture (), any ());
255
260
256
261
org .asynchttpclient .Request ahcRequest = capture .getValue ();
257
262
@@ -263,11 +268,9 @@ public void contentTypeHeaderIsPassedInRequest() throws Exception {
263
268
264
269
@ Test
265
270
public void contenTypeIsOptionalInResponse () throws Exception {
266
- AsyncHttpClient client = mock ( AsyncHttpClient . class );
271
+ givenResponseIsProduced ( httpClient , responseWithBody ( null , "test" ) );
267
272
268
- givenResponseIsProduced (client , responseWithBody (null , "test" ));
269
-
270
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
273
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
271
274
272
275
assertEquals (response .code (), 200 );
273
276
assertEquals (response .header ("Server" ), "nginx" );
@@ -277,11 +280,9 @@ public void contenTypeIsOptionalInResponse() throws Exception {
277
280
278
281
@ Test
279
282
public void contentTypeIsProperlyParsedIfPresent () throws Exception {
280
- AsyncHttpClient client = mock (AsyncHttpClient .class );
281
-
282
- givenResponseIsProduced (client , responseWithBody ("text/plain" , "test" ));
283
+ givenResponseIsProduced (httpClient , responseWithBody ("text/plain" , "test" ));
283
284
284
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
285
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
285
286
286
287
assertEquals (response .code (), 200 );
287
288
assertEquals (response .header ("Server" ), "nginx" );
@@ -292,11 +293,9 @@ public void contentTypeIsProperlyParsedIfPresent() throws Exception {
292
293
293
294
@ Test
294
295
public void bodyIsNotNullInResponse () throws Exception {
295
- AsyncHttpClient client = mock (AsyncHttpClient .class );
296
-
297
- givenResponseIsProduced (client , responseWithNoBody ());
296
+ givenResponseIsProduced (httpClient , responseWithNoBody ());
298
297
299
- okhttp3 .Response response = whenRequestIsMade (client , REQUEST );
298
+ okhttp3 .Response response = whenRequestIsMade (httpClient , REQUEST );
300
299
301
300
assertEquals (response .code (), 200 );
302
301
assertEquals (response .header ("Server" ), "nginx" );
@@ -315,6 +314,50 @@ void getHttpClientShouldThrowISEIfSupplierReturnsNull() {
315
314
call .getHttpClient ();
316
315
}
317
316
317
+ @ Test
318
+ void shouldReturnTimeoutSpecifiedInAHCInstanceConfig () {
319
+ // given:
320
+ val cfgBuilder = new DefaultAsyncHttpClientConfig .Builder ();
321
+ AsyncHttpClientConfig config = null ;
322
+
323
+ // and: setup call
324
+ val call = AsyncHttpClientCall .builder ()
325
+ .httpClientSupplier (httpClientSupplier )
326
+ .request (requestWithBody ())
327
+ .build ();
328
+
329
+ // when: set read timeout to 5s, req timeout to 6s
330
+ config = cfgBuilder .setReadTimeout ((int ) SECONDS .toMillis (5 ))
331
+ .setRequestTimeout ((int ) SECONDS .toMillis (6 ))
332
+ .build ();
333
+ when (httpClient .getConfig ()).thenReturn (config );
334
+
335
+ // then: expect request timeout
336
+ assertEquals (call .getRequestTimeoutMillis (), SECONDS .toMillis (6 ));
337
+ assertEquals (call .timeout ().timeoutNanos (), SECONDS .toNanos (6 ));
338
+
339
+ // when: set read timeout to 10 seconds, req timeout to 7s
340
+ config = cfgBuilder .setReadTimeout ((int ) SECONDS .toMillis (10 ))
341
+ .setRequestTimeout ((int ) SECONDS .toMillis (7 ))
342
+ .build ();
343
+ when (httpClient .getConfig ()).thenReturn (config );
344
+
345
+ // then: expect request timeout
346
+ assertEquals (call .getRequestTimeoutMillis (), SECONDS .toMillis (7 ));
347
+ assertEquals (call .timeout ().timeoutNanos (), SECONDS .toNanos (7 ));
348
+
349
+ // when: set request timeout to a negative value, just for fun.
350
+ config = cfgBuilder .setRequestTimeout (-1000 )
351
+ .setReadTimeout (2000 )
352
+ .build ();
353
+
354
+ when (httpClient .getConfig ()).thenReturn (config );
355
+
356
+ // then: expect request timeout, but as positive value
357
+ assertEquals (call .getRequestTimeoutMillis (), SECONDS .toMillis (1 ));
358
+ assertEquals (call .timeout ().timeoutNanos (), SECONDS .toNanos (1 ));
359
+ }
360
+
318
361
private void givenResponseIsProduced (AsyncHttpClient client , Response response ) {
319
362
when (client .executeRequest (any (org .asynchttpclient .Request .class ), any ())).thenAnswer (invocation -> {
320
363
AsyncCompletionHandler <Response > handler = invocation .getArgument (1 );
0 commit comments