Skip to content

Commit de699f1

Browse files
committed
OkHttp test refactoring
1 parent 4cb5b8a commit de699f1

3 files changed

Lines changed: 96 additions & 65 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.ext",
33+
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
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.ext",
55+
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
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+
}

‎okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,22 @@ public void whenSetResponseCacheUsingOkHttp_thenCorrect() throws IOException {
9292
//.cacheControl(CacheControl.FORCE_CACHE)
9393
.build();
9494

95-
Response response = client.newCall(request).execute();
95+
Response response1 = client.newCall(request).execute();
9696

97-
String responseBody = response.body().string();
97+
String responseBody1 = response1.body().string();
9898

99-
System.out.println("Response response: " + response);
100-
System.out.println("Response cache response: " + response.cacheResponse());
101-
System.out.println("Response network response: " + response.networkResponse());
102-
System.out.println("Response responseBody: " + responseBody);
99+
System.out.println("Response 1 response: " + response1);
100+
System.out.println("Response 1 cache response: " + response1.cacheResponse());
101+
System.out.println("Response 1 network response: " + response1.networkResponse());
102+
System.out.println("Response 1 responseBody: " + responseBody1);
103+
104+
Response response2 = client.newCall(request).execute();
105+
106+
String responseBody2 = response2.body().string();
107+
108+
System.out.println("Response 2 response: " + response2);
109+
System.out.println("Response 2 cache response: " + response2.cacheResponse());
110+
System.out.println("Response 2 network response: " + response2.networkResponse());
111+
System.out.println("Response 2 responseBody: " + responseBody2);
103112
}
104113
}

‎okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.baeldung.okhttp;
22

33
import static org.hamcrest.Matchers.equalTo;
4-
import static org.junit.Assert.assertFalse;
54
import static org.junit.Assert.assertThat;
65

76
import java.io.File;
87
import java.io.IOException;
98

10-
import org.baeldung.okhttp.ProgressRequestWrapper;
119
import org.junit.Test;
1210

1311
import okhttp3.Call;
@@ -108,61 +106,4 @@ public void whenSendMultipartRequestUsingOkHttp_thenCorrect() throws IOException
108106

109107
assertThat(response.code(), equalTo(200));
110108
}
111-
112-
@Test
113-
public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException {
114-
115-
OkHttpClient client = new OkHttpClient();
116-
117-
RequestBody requestBody = new MultipartBody.Builder()
118-
.setType(MultipartBody.FORM)
119-
.addFormDataPart("file", "file.ext",
120-
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
121-
.build();
122-
123-
Request request = new Request.Builder()
124-
.url(BASE_URL + "/users/upload")
125-
.post(requestBody)
126-
.build();
127-
128-
Call call = client.newCall(request);
129-
Response response = call.execute();
130-
131-
assertThat(response.code(), equalTo(200));
132-
}
133-
134-
@Test
135-
public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException {
136-
137-
OkHttpClient client = new OkHttpClient();
138-
139-
RequestBody requestBody = new MultipartBody.Builder()
140-
.setType(MultipartBody.FORM)
141-
.addFormDataPart("file", "file.ext",
142-
RequestBody.create(MediaType.parse("image/png"), new File("src/test/resources/test.in")))
143-
.build();
144-
145-
146-
ProgressRequestWrapper.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() {
147-
148-
public void onRequestProgress(long bytesWritten, long contentLength) {
149-
150-
float percentage = 100f * bytesWritten / contentLength;
151-
assertFalse(Float.compare(percentage, 100) > 0);
152-
}
153-
};
154-
155-
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener);
156-
157-
Request request = new Request.Builder()
158-
.url(BASE_URL + "/users/upload")
159-
.post(countingBody)
160-
.build();
161-
162-
Call call = client.newCall(request);
163-
Response response = call.execute();
164-
165-
assertThat(response.code(), equalTo(200));
166-
167-
}
168109
}

0 commit comments

Comments
 (0)