Code improvement
This commit is contained in:
parent
fe577c073d
commit
a8b3f44a2f
|
@ -1,44 +1,76 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
<modelVersion>4.0.0</modelVersion>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<groupId>org.baeldung.okhttp</groupId>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>okhttp</artifactId>
|
<groupId>org.baeldung.okhttp</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!-- okhttp -->
|
<!-- okhttp -->
|
||||||
<dependency>
|
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
|
||||||
<artifactId>okhttp</artifactId>
|
|
||||||
<version>${com.squareup.okhttp3.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
<dependency>
|
||||||
<dependency>
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
<groupId>junit</groupId>
|
<artifactId>okhttp</artifactId>
|
||||||
<artifactId>junit</artifactId>
|
<version>${com.squareup.okhttp3.version}</version>
|
||||||
<version>${junit.version}</version>
|
</dependency>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<!-- logging -->
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-library</artifactId>
|
|
||||||
<version>${org.hamcrest.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<properties>
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- okhttp -->
|
<dependency>
|
||||||
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- testing -->
|
<dependency>
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<groupId>org.slf4j</groupId>
|
||||||
<junit.version>4.12</junit.version>
|
<artifactId>log4j-over-slf4j</artifactId>
|
||||||
</properties>
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test scoped -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${org.hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
|
||||||
|
<!-- okhttp -->
|
||||||
|
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
<org.slf4j.version>1.7.13</org.slf4j.version>
|
||||||
|
<logback.version>1.1.3</logback.version>
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
package org.baeldung.okhttp;
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okio.*;
|
import okhttp3.MediaType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okio.Buffer;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.ForwardingSink;
|
||||||
|
import okio.Okio;
|
||||||
|
import okio.Sink;
|
||||||
|
|
||||||
public class ProgressRequestWrapper extends RequestBody {
|
public class ProgressRequestWrapper extends RequestBody {
|
||||||
|
|
||||||
protected RequestBody delegate;
|
protected RequestBody delegate;
|
||||||
|
@ -61,6 +66,7 @@ public class ProgressRequestWrapper extends RequestBody {
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ProgressListener {
|
public interface ProgressListener {
|
||||||
|
|
||||||
void onRequestProgress(long bytesWritten, long contentLength);
|
void onRequestProgress(long bytesWritten, long contentLength);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class OkHttpFileUploadingTest {
|
||||||
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException {
|
public void whenUploadFile_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class OkHttpFileUploadingTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException {
|
public void whenGetUploadFileProgress_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class OkHttpGetTest {
|
||||||
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetRequestUsingOkHttp_thenCorrect() throws IOException {
|
public void whenGetRequest_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public class OkHttpGetTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetRequestWithQueryParameterUsingOkHttp_thenCorrect() throws IOException {
|
public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ public class OkHttpGetTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAsynchronousGetRequestUsingOkHttp_thenCorrect() {
|
public void whenAsynchronousGetRequest_thenCorrect() {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class OkHttpHeaderTest {
|
||||||
private static final String SAMPLE_URL = "http://www.github.com";
|
private static final String SAMPLE_URL = "http://www.github.com";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSetHeaderUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSetHeader_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class OkHttpHeaderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSetDefaultHeaderUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSetDefaultHeader_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
|
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
|
||||||
|
|
|
@ -7,6 +7,8 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
|
@ -17,13 +19,12 @@ import okhttp3.Response;
|
||||||
public class OkHttpMiscTest {
|
public class OkHttpMiscTest {
|
||||||
|
|
||||||
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(OkHttpMiscTest.class);
|
||||||
|
|
||||||
//@Test
|
//@Test
|
||||||
public void whenSetRequestTimeoutUsingOkHttp_thenFail() throws IOException {
|
public void whenSetRequestTimeout_thenFail() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
//.connectTimeout(10, TimeUnit.SECONDS)
|
|
||||||
//.writeTimeout(10, TimeUnit.SECONDS)
|
|
||||||
.readTimeout(1, TimeUnit.SECONDS)
|
.readTimeout(1, TimeUnit.SECONDS)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -36,8 +37,8 @@ public class OkHttpMiscTest {
|
||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Test
|
@Test
|
||||||
public void whenCancelRequestUsingOkHttp_thenCorrect() throws IOException {
|
public void whenCancelRequest_thenCorrect() throws IOException {
|
||||||
|
|
||||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
|
@ -48,7 +49,6 @@ public class OkHttpMiscTest {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final int seconds = 1;
|
final int seconds = 1;
|
||||||
//final int seconds = 3;
|
|
||||||
|
|
||||||
final long startNanos = System.nanoTime();
|
final long startNanos = System.nanoTime();
|
||||||
final Call call = client.newCall(request);
|
final Call call = client.newCall(request);
|
||||||
|
@ -57,57 +57,55 @@ public class OkHttpMiscTest {
|
||||||
executor.schedule(new Runnable() {
|
executor.schedule(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
call.cancel();
|
call.cancel();
|
||||||
System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
}
|
}
|
||||||
}, seconds, TimeUnit.SECONDS);
|
}, seconds, TimeUnit.SECONDS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
|
||||||
Response response = call.execute();
|
Response response = call.execute();
|
||||||
System.out.printf("%.2f Call was expected to fail, but completed: %s%n", (System.nanoTime() - startNanos) / 1e9f, response);
|
logger.debug("Call was expected to fail, but completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
System.out.printf("%.2f Call failed as expected: %s%n", (System.nanoTime() - startNanos) / 1e9f, e);
|
logger.debug("Call failed as expected: " + (System.nanoTime() - startNanos) / 1e9f, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
//@Test
|
||||||
public void whenSetResponseCacheUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSetResponseCache_thenCorrect() throws IOException {
|
||||||
|
|
||||||
int cacheSize = 10 * 1024 * 1024; // 10 MiB
|
int cacheSize = 10 * 1024 * 1024; // 10 MiB
|
||||||
File cacheDirectory = new File("src/test/resources/cache");
|
File cacheDirectory = new File("src/test/resources/cache");
|
||||||
Cache cache = new Cache(cacheDirectory, cacheSize);
|
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.cache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url("http://publicobject.com/helloworld.txt")
|
.url("http://publicobject.com/helloworld.txt")
|
||||||
//.cacheControl(CacheControl.FORCE_NETWORK)
|
.build();
|
||||||
//.cacheControl(CacheControl.FORCE_CACHE)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Response response1 = client.newCall(request).execute();
|
Response response1 = client.newCall(request).execute();
|
||||||
|
|
||||||
String responseBody1 = response1.body().string();
|
String responseBody1 = response1.body().string();
|
||||||
|
|
||||||
System.out.println("Response 1 response: " + response1);
|
logger.debug("Response 1 response: " + response1);
|
||||||
System.out.println("Response 1 cache response: " + response1.cacheResponse());
|
logger.debug("Response 1 cache response: " + response1.cacheResponse());
|
||||||
System.out.println("Response 1 network response: " + response1.networkResponse());
|
logger.debug("Response 1 network response: " + response1.networkResponse());
|
||||||
System.out.println("Response 1 responseBody: " + responseBody1);
|
logger.debug("Response 1 responseBody: " + responseBody1);
|
||||||
|
|
||||||
Response response2 = client.newCall(request).execute();
|
Response response2 = client.newCall(request).execute();
|
||||||
|
|
||||||
String responseBody2 = response2.body().string();
|
String responseBody2 = response2.body().string();
|
||||||
|
|
||||||
System.out.println("Response 2 response: " + response2);
|
logger.debug("Response 2 response: " + response2);
|
||||||
System.out.println("Response 2 cache response: " + response2.cacheResponse());
|
logger.debug("Response 2 cache response: " + response2.cacheResponse());
|
||||||
System.out.println("Response 2 network response: " + response2.networkResponse());
|
logger.debug("Response 2 network response: " + response2.networkResponse());
|
||||||
System.out.println("Response 2 responseBody: " + responseBody2);
|
logger.debug("Response 2 responseBody: " + responseBody2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class OkHttpPostingTest {
|
||||||
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
|
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSendPostRequestUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSendPostRequest_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class OkHttpPostingTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSendPostRequestWithAuthorizationUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
|
||||||
|
|
||||||
String postBody = "test post";
|
String postBody = "test post";
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ public class OkHttpPostingTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPostJsonUsingOkHttp_thenCorrect() throws IOException {
|
public void whenPostJson_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ public class OkHttpPostingTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSendMultipartRequestUsingOkHttp_thenCorrect() throws IOException {
|
public void whenSendMultipartRequest_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ import okhttp3.Response;
|
||||||
public class OkHttpRedirectTest {
|
public class OkHttpRedirectTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSetFollowRedirectsUsingOkHttp_thenNotRedirected() throws IOException {
|
public void whenSetFollowRedirects_thenNotRedirected() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||||
.followRedirects(false)
|
.followRedirects(false)
|
||||||
|
|
Loading…
Reference in New Issue