commit
3dae7f5352
44
okhttp/pom.xml
Normal file
44
okhttp/pom.xml
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.baeldung.okhttp</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- okhttp -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>${com.squareup.okhttp3.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>
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,26 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.Interceptor;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class DefaultContentTypeInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
private final String contentType;
|
||||||
|
|
||||||
|
public DefaultContentTypeInterceptor(String contentType) {
|
||||||
|
this.contentType = contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||||
|
|
||||||
|
Request originalRequest = chain.request();
|
||||||
|
Request requestWithUserAgent = originalRequest.newBuilder()
|
||||||
|
.header("Content-Type", contentType)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return chain.proceed(requestWithUserAgent);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okio.Buffer;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.ForwardingSink;
|
||||||
|
import okio.Okio;
|
||||||
|
import okio.Sink;
|
||||||
|
|
||||||
|
public class ProgressRequestWrapper extends RequestBody {
|
||||||
|
|
||||||
|
protected RequestBody delegate;
|
||||||
|
protected ProgressListener listener;
|
||||||
|
|
||||||
|
protected CountingSink countingSink;
|
||||||
|
|
||||||
|
public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MediaType contentType() {
|
||||||
|
return delegate.contentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long contentLength() throws IOException {
|
||||||
|
return delegate.contentLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeTo(BufferedSink sink) throws IOException {
|
||||||
|
|
||||||
|
BufferedSink bufferedSink;
|
||||||
|
|
||||||
|
countingSink = new CountingSink(sink);
|
||||||
|
bufferedSink = Okio.buffer(countingSink);
|
||||||
|
|
||||||
|
delegate.writeTo(bufferedSink);
|
||||||
|
|
||||||
|
bufferedSink.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final class CountingSink extends ForwardingSink {
|
||||||
|
|
||||||
|
private long bytesWritten = 0;
|
||||||
|
|
||||||
|
public CountingSink(Sink delegate) {
|
||||||
|
super(delegate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(Buffer source, long byteCount) throws IOException {
|
||||||
|
|
||||||
|
super.write(source, byteCount);
|
||||||
|
|
||||||
|
bytesWritten += byteCount;
|
||||||
|
listener.onRequestProgress(bytesWritten, contentLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static interface ProgressListener {
|
||||||
|
|
||||||
|
public void onRequestProgress(long bytesWritten, long contentLength);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,81 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.baeldung.okhttp.ProgressRequestWrapper;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpFileUploadingTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("file", "file.txt",
|
||||||
|
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/users/upload")
|
||||||
|
.post(requestBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("file", "file.txt",
|
||||||
|
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
ProgressRequestWrapper.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() {
|
||||||
|
|
||||||
|
public void onRequestProgress(long bytesWritten, long contentLength) {
|
||||||
|
|
||||||
|
float percentage = 100f * bytesWritten / contentLength;
|
||||||
|
assertFalse(Float.compare(percentage, 100) > 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener);
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/users/upload")
|
||||||
|
.post(countingBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
78
okhttp/src/test/java/org/baeldung/okhttp/OkHttpGetTest.java
Normal file
78
okhttp/src/test/java/org/baeldung/okhttp/OkHttpGetTest.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Callback;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpGetTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetRequestUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/date")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetRequestWithQueryParameterUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
|
||||||
|
urlBuilder.addQueryParameter("id", "1");
|
||||||
|
|
||||||
|
String url = urlBuilder.build().toString();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAsynchronousGetRequestUsingOkHttp_thenCorrect() {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/date")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
|
||||||
|
call.enqueue(new Callback() {
|
||||||
|
|
||||||
|
public void onResponse(Call call, Response response) throws IOException {
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onFailure(Call call, IOException e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpHeaderTest {
|
||||||
|
|
||||||
|
private static final String SAMPLE_URL = "http://www.github.com";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetHeaderUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(SAMPLE_URL)
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetDefaultHeaderUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(SAMPLE_URL)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
113
okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java
Normal file
113
okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Cache;
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpMiscTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
|
||||||
|
//@Test
|
||||||
|
public void whenSetRequestTimeoutUsingOkHttp_thenFail() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
//.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
|
//.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(1, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//@Test
|
||||||
|
public void whenCancelRequestUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final int seconds = 1;
|
||||||
|
//final int seconds = 3;
|
||||||
|
|
||||||
|
final long startNanos = System.nanoTime();
|
||||||
|
final Call call = client.newCall(request);
|
||||||
|
|
||||||
|
// Schedule a job to cancel the call in 1 second.
|
||||||
|
executor.schedule(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
call.cancel();
|
||||||
|
System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
}
|
||||||
|
}, seconds, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f);
|
||||||
|
Response response = call.execute();
|
||||||
|
System.out.printf("%.2f Call was expected to fail, but completed: %s%n", (System.nanoTime() - startNanos) / 1e9f, response);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
|
||||||
|
System.out.printf("%.2f Call failed as expected: %s%n", (System.nanoTime() - startNanos) / 1e9f, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetResponseCacheUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
int cacheSize = 10 * 1024 * 1024; // 10 MiB
|
||||||
|
File cacheDirectory = new File("src/test/resources/cache");
|
||||||
|
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.cache(cache)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url("http://publicobject.com/helloworld.txt")
|
||||||
|
//.cacheControl(CacheControl.FORCE_NETWORK)
|
||||||
|
//.cacheControl(CacheControl.FORCE_CACHE)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Response response1 = client.newCall(request).execute();
|
||||||
|
|
||||||
|
String responseBody1 = response1.body().string();
|
||||||
|
|
||||||
|
System.out.println("Response 1 response: " + response1);
|
||||||
|
System.out.println("Response 1 cache response: " + response1.cacheResponse());
|
||||||
|
System.out.println("Response 1 network response: " + response1.networkResponse());
|
||||||
|
System.out.println("Response 1 responseBody: " + responseBody1);
|
||||||
|
|
||||||
|
Response response2 = client.newCall(request).execute();
|
||||||
|
|
||||||
|
String responseBody2 = response2.body().string();
|
||||||
|
|
||||||
|
System.out.println("Response 2 response: " + response2);
|
||||||
|
System.out.println("Response 2 cache response: " + response2.cacheResponse());
|
||||||
|
System.out.println("Response 2 network response: " + response2.networkResponse());
|
||||||
|
System.out.println("Response 2 responseBody: " + responseBody2);
|
||||||
|
}
|
||||||
|
}
|
109
okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java
Normal file
109
okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Credentials;
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpPostingTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/spring-rest";
|
||||||
|
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSendPostRequestUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
RequestBody formBody = new FormBody.Builder()
|
||||||
|
.add("username", "test")
|
||||||
|
.add("password", "test")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/users")
|
||||||
|
.post(formBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSendPostRequestWithAuthorizationUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
String postBody = "test post";
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(URL_SECURED_BY_BASIC_AUTHENTICATION)
|
||||||
|
.addHeader("Authorization", Credentials.basic("test", "test"))
|
||||||
|
.post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), postBody))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPostJsonUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
String json = "{\"id\":1,\"name\":\"John\"}";
|
||||||
|
|
||||||
|
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/users/detail")
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSendMultipartRequestUsingOkHttp_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
RequestBody requestBody = new MultipartBody.Builder()
|
||||||
|
.setType(MultipartBody.FORM)
|
||||||
|
.addFormDataPart("username", "test")
|
||||||
|
.addFormDataPart("password", "test")
|
||||||
|
.addFormDataPart("file", "file.txt",
|
||||||
|
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(BASE_URL + "/users/multipart")
|
||||||
|
.post(requestBody)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(200));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.baeldung.okhttp;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class OkHttpRedirectTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSetFollowRedirectsUsingOkHttp_thenNotRedirected() throws IOException {
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
||||||
|
.followRedirects(false)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url("http://t.co/I5YYd9tddw")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
Response response = call.execute();
|
||||||
|
|
||||||
|
assertThat(response.code(), equalTo(301));
|
||||||
|
}
|
||||||
|
}
|
1
okhttp/src/test/resources/test.txt
Normal file
1
okhttp/src/test/resources/test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
hello world
|
@ -27,7 +27,13 @@ public class ItemController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/date")
|
@RequestMapping("/date")
|
||||||
public Date getCurrentDate() {
|
public Date getCurrentDate() throws Exception {
|
||||||
return new Date();
|
return new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/delay/{seconds}")
|
||||||
|
public void getCurrentTime(@PathVariable final int seconds) throws Exception {
|
||||||
|
|
||||||
|
Thread.sleep(seconds * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user