|
| 1 | +package org.baeldung.okhttp; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +import org.baeldung.okhttp.ProgressRequestWrapper; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import okhttp3.Call; |
| 14 | +import okhttp3.MediaType; |
| 15 | +import okhttp3.MultipartBody; |
| 16 | +import okhttp3.OkHttpClient; |
| 17 | +import okhttp3.Request; |
| 18 | +import okhttp3.RequestBody; |
| 19 | +import okhttp3.Response; |
| 20 | + |
| 21 | +public class OkHttpFileUploadingTest { |
| 22 | + |
| 23 | + private static final String BASE_URL = "http://localhost:8080/spring-rest"; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException { |
| 27 | + |
| 28 | + OkHttpClient client = new OkHttpClient(); |
| 29 | + |
| 30 | + RequestBody requestBody = new MultipartBody.Builder() |
| 31 | + .setType(MultipartBody.FORM) |
| 32 | + .addFormDataPart("file", "file.txt", |
| 33 | + RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))) |
| 34 | + .build(); |
| 35 | + |
| 36 | + Request request = new Request.Builder() |
| 37 | + .url(BASE_URL + "/users/upload") |
| 38 | + .post(requestBody) |
| 39 | + .build(); |
| 40 | + |
| 41 | + Call call = client.newCall(request); |
| 42 | + Response response = call.execute(); |
| 43 | + |
| 44 | + assertThat(response.code(), equalTo(200)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException { |
| 49 | + |
| 50 | + OkHttpClient client = new OkHttpClient(); |
| 51 | + |
| 52 | + RequestBody requestBody = new MultipartBody.Builder() |
| 53 | + .setType(MultipartBody.FORM) |
| 54 | + .addFormDataPart("file", "file.txt", |
| 55 | + RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))) |
| 56 | + .build(); |
| 57 | + |
| 58 | + |
| 59 | + ProgressRequestWrapper.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() { |
| 60 | + |
| 61 | + public void onRequestProgress(long bytesWritten, long contentLength) { |
| 62 | + |
| 63 | + float percentage = 100f * bytesWritten / contentLength; |
| 64 | + assertFalse(Float.compare(percentage, 100) > 0); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener); |
| 69 | + |
| 70 | + Request request = new Request.Builder() |
| 71 | + .url(BASE_URL + "/users/upload") |
| 72 | + .post(countingBody) |
| 73 | + .build(); |
| 74 | + |
| 75 | + Call call = client.newCall(request); |
| 76 | + Response response = call.execute(); |
| 77 | + |
| 78 | + assertThat(response.code(), equalTo(200)); |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments