OkHttp test refactoring

This commit is contained in:
Ivan Paolillo 2016-09-29 14:22:10 +02:00
parent 4cb5b8ad4a
commit de699f1832
3 changed files with 96 additions and 65 deletions

View File

@ -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.ext",
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
.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.ext",
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
.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));
}
}

View File

@ -92,13 +92,22 @@ public class OkHttpMiscTest {
//.cacheControl(CacheControl.FORCE_CACHE)
.build();
Response response = client.newCall(request).execute();
Response response1 = client.newCall(request).execute();
String responseBody = response.body().string();
String responseBody1 = response1.body().string();
System.out.println("Response response: " + response);
System.out.println("Response cache response: " + response.cacheResponse());
System.out.println("Response network response: " + response.networkResponse());
System.out.println("Response responseBody: " + responseBody);
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);
}
}

View File

@ -1,13 +1,11 @@
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;
@ -108,61 +106,4 @@ public class OkHttpPostingTest {
assertThat(response.code(), equalTo(200));
}
@Test
public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "file.ext",
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
.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.ext",
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
.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));
}
}