11package org .baeldung .okhttp ;
22
3- import okhttp3 .*;
4- import org .junit .Test ;
5- import org .slf4j .Logger ;
6- import org .slf4j .LoggerFactory ;
3+ import static org .baeldung .client .Consts .APPLICATION_PORT ;
4+
75import java .io .File ;
86import java .io .IOException ;
97import java .util .concurrent .Executors ;
108import java .util .concurrent .ScheduledExecutorService ;
119import java .util .concurrent .TimeUnit ;
12- import org .junit .Before ;
13- import org .junit .Test ;
14- import org .slf4j .Logger ;
15- import org .slf4j .LoggerFactory ;
10+
1611import okhttp3 .Cache ;
1712import okhttp3 .Call ;
1813import okhttp3 .OkHttpClient ;
1914import okhttp3 .Request ;
2015import okhttp3 .Response ;
2116
17+ import org .junit .Before ;
18+ import org .junit .Test ;
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
21+
2222public class OkHttpMiscLiveTest {
2323
24- private static final String BASE_URL = "http://localhost:8080 /spring-rest" ;
24+ private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + " /spring-rest" ;
2525 private static Logger logger = LoggerFactory .getLogger (OkHttpMiscLiveTest .class );
2626
2727 OkHttpClient client ;
2828
2929 @ Before
3030 public void init () {
3131
32- client = new OkHttpClient ();
32+ client = new OkHttpClient ();
3333 }
3434
3535 @ Test
3636 public void whenSetRequestTimeout_thenFail () throws IOException {
37- OkHttpClient clientWithTimeout = new OkHttpClient .Builder ()
38- .readTimeout (1 , TimeUnit .SECONDS )
39- .build ();
37+ final OkHttpClient clientWithTimeout = new OkHttpClient .Builder ().readTimeout (1 , TimeUnit .SECONDS ).build ();
4038
41- Request request = new Request .Builder ()
42- .url (BASE_URL + "/delay/2" ) // This URL is served with a 2 second delay.
43- .build ();
39+ final Request request = new Request .Builder ().url (BASE_URL + "/delay/2" ) // This URL is served with a 2 second delay.
40+ .build ();
4441
45- Call call = clientWithTimeout .newCall (request );
46- Response response = call .execute ();
42+ final Call call = clientWithTimeout .newCall (request );
43+ final Response response = call .execute ();
4744 response .close ();
4845 }
4946
5047 @ Test (expected = IOException .class )
5148 public void whenCancelRequest_thenCorrect () throws IOException {
52- ScheduledExecutorService executor = Executors .newScheduledThreadPool (1 );
49+ final ScheduledExecutorService executor = Executors .newScheduledThreadPool (1 );
5350
54- Request request = new Request .Builder ()
55- .url (BASE_URL + "/delay/2" ) // This URL is served with a 2 second delay.
56- .build ();
51+ final Request request = new Request .Builder ().url (BASE_URL + "/delay/2" ) // This URL is served with a 2 second delay.
52+ .build ();
5753
5854 final int seconds = 1 ;
5955 final long startNanos = System .nanoTime ();
@@ -63,42 +59,38 @@ public void whenCancelRequest_thenCorrect() throws IOException {
6359 // Schedule a job to cancel the call in 1 second.
6460 executor .schedule (() -> {
6561
66- logger .debug ("Canceling call: " + (System .nanoTime () - startNanos ) / 1e9f );
62+ logger .debug ("Canceling call: " + (( System .nanoTime () - startNanos ) / 1e9f ) );
6763 call .cancel ();
68- logger .debug ("Canceled call: " + (System .nanoTime () - startNanos ) / 1e9f );
64+ logger .debug ("Canceled call: " + (( System .nanoTime () - startNanos ) / 1e9f ) );
6965
7066 }, seconds , TimeUnit .SECONDS );
7167
72- logger .debug ("Executing call: " + (System .nanoTime () - startNanos ) / 1e9f );
73- Response response = call .execute ();
74- logger .debug ("Call completed: " + (System .nanoTime () - startNanos ) / 1e9f , response );
68+ logger .debug ("Executing call: " + (( System .nanoTime () - startNanos ) / 1e9f ) );
69+ final Response response = call .execute ();
70+ logger .debug ("Call completed: " + (( System .nanoTime () - startNanos ) / 1e9f ) , response );
7571 }
7672
7773 @ Test
78- public void whenSetResponseCache_thenCorrect () throws IOException {
74+ public void whenSetResponseCache_thenCorrect () throws IOException {
7975
80- int cacheSize = 10 * 1024 * 1024 ; // 10 MiB
81- File cacheDirectory = new File ("src/test/resources/cache" );
82- Cache cache = new Cache (cacheDirectory , cacheSize );
76+ final int cacheSize = 10 * 1024 * 1024 ; // 10 MiB
77+ final File cacheDirectory = new File ("src/test/resources/cache" );
78+ final Cache cache = new Cache (cacheDirectory , cacheSize );
8379
84- OkHttpClient clientCached = new OkHttpClient .Builder ()
85- .cache (cache )
86- .build ();
80+ final OkHttpClient clientCached = new OkHttpClient .Builder ().cache (cache ).build ();
8781
88- Request request = new Request .Builder ()
89- .url ("http://publicobject.com/helloworld.txt" )
90- .build ();
82+ final Request request = new Request .Builder ().url ("http://publicobject.com/helloworld.txt" ).build ();
9183
92- Response response1 = clientCached .newCall (request ).execute ();
84+ final Response response1 = clientCached .newCall (request ).execute ();
9385 logResponse (response1 );
9486
95- Response response2 = clientCached .newCall (request ).execute ();
87+ final Response response2 = clientCached .newCall (request ).execute ();
9688 logResponse (response2 );
9789 }
9890
9991 private void logResponse (Response response ) throws IOException {
10092
101- logger .debug ("Response response: " + response );
93+ logger .debug ("Response response: " + response );
10294 logger .debug ("Response cache response: " + response .cacheResponse ());
10395 logger .debug ("Response network response: " + response .networkResponse ());
10496 logger .debug ("Response responseBody: " + response .body ().string ());
0 commit comments