Skip to content

Commit 453b739

Browse files
committed
Add OkHttp example
1 parent 8692cad commit 453b739

File tree

10 files changed

+574
-6
lines changed

10 files changed

+574
-6
lines changed

spring-rest/pom.xml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependencies>
1616

1717
<!-- Spring Boot Dependencies -->
18-
18+
1919
<dependency>
2020
<groupId>org.springframework.boot</groupId>
2121
<artifactId>spring-boot-starter-thymeleaf</artifactId>
@@ -71,7 +71,7 @@
7171
<groupId>com.fasterxml.jackson.core</groupId>
7272
<artifactId>jackson-databind</artifactId>
7373
</dependency>
74-
74+
7575
<dependency>
7676
<groupId>com.fasterxml.jackson.dataformat</groupId>
7777
<artifactId>jackson-dataformat-xml</artifactId>
@@ -118,6 +118,14 @@
118118
<artifactId>log4j-over-slf4j</artifactId>
119119
</dependency>
120120

121+
<!-- okhttp -->
122+
123+
<dependency>
124+
<groupId>com.squareup.okhttp3</groupId>
125+
<artifactId>okhttp</artifactId>
126+
<version>${com.squareup.okhttp3.version}</version>
127+
</dependency>
128+
121129
<!-- test scoped -->
122130

123131
<dependency>
@@ -153,14 +161,14 @@
153161
<artifactId>rest-assured</artifactId>
154162
<version>${rest-assured.version}</version>
155163
</dependency>
156-
164+
157165
<!-- -->
158166
<dependency>
159167
<groupId>com.google.protobuf</groupId>
160168
<artifactId>protobuf-java</artifactId>
161169
<version>2.6.1</version>
162170
</dependency>
163-
171+
164172
<dependency>
165173
<groupId>com.esotericsoftware</groupId>
166174
<artifactId>kryo</artifactId>
@@ -198,7 +206,7 @@
198206
<artifactId>maven-surefire-plugin</artifactId>
199207
<configuration>
200208
<excludes>
201-
<exclude>**/*LiveTest.java</exclude>
209+
<exclude>**/*LiveTest.java</exclude>
202210
</excludes>
203211
<systemPropertyVariables>
204212
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
@@ -285,7 +293,7 @@
285293
</build>
286294
</profile>
287295
</profiles>
288-
296+
289297
<properties>
290298
<!-- Spring -->
291299

@@ -320,6 +328,9 @@
320328
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
321329
<cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version>
322330

331+
<!-- okhttp -->
332+
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
333+
323334
</properties>
324335

325336
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.okhttp;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.Request;
7+
import okhttp3.Response;
8+
9+
public class DefaultContentTypeInterceptor implements Interceptor {
10+
11+
private final String contentType;
12+
13+
public DefaultContentTypeInterceptor(String contentType) {
14+
this.contentType = contentType;
15+
}
16+
17+
public Response intercept(Interceptor.Chain chain) throws IOException {
18+
19+
Request originalRequest = chain.request();
20+
Request requestWithUserAgent = originalRequest.newBuilder()
21+
.header("Content-Type", contentType)
22+
.build();
23+
24+
return chain.proceed(requestWithUserAgent);
25+
}
26+
}
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 whenUploadFile_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 whenGetUploadFileProgress_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+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.baeldung.okhttp;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.io.IOException;
7+
8+
import org.junit.Test;
9+
10+
import okhttp3.Call;
11+
import okhttp3.Callback;
12+
import okhttp3.HttpUrl;
13+
import okhttp3.OkHttpClient;
14+
import okhttp3.Request;
15+
import okhttp3.Response;
16+
17+
public class OkHttpGetTest {
18+
19+
private static final String BASE_URL = "http://localhost:8080/spring-rest";
20+
21+
@Test
22+
public void whenGetRequest_thenCorrect() throws IOException {
23+
24+
OkHttpClient client = new OkHttpClient();
25+
26+
Request request = new Request.Builder()
27+
.url(BASE_URL + "/date")
28+
.build();
29+
30+
Call call = client.newCall(request);
31+
Response response = call.execute();
32+
33+
assertThat(response.code(), equalTo(200));
34+
}
35+
36+
@Test
37+
public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException {
38+
39+
OkHttpClient client = new OkHttpClient();
40+
41+
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
42+
urlBuilder.addQueryParameter("id", "1");
43+
44+
String url = urlBuilder.build().toString();
45+
46+
Request request = new Request.Builder()
47+
.url(url)
48+
.build();
49+
50+
Call call = client.newCall(request);
51+
Response response = call.execute();
52+
53+
assertThat(response.code(), equalTo(200));
54+
}
55+
56+
@Test
57+
public void whenAsynchronousGetRequest_thenCorrect() {
58+
59+
OkHttpClient client = new OkHttpClient();
60+
61+
Request request = new Request.Builder()
62+
.url(BASE_URL + "/date")
63+
.build();
64+
65+
Call call = client.newCall(request);
66+
67+
call.enqueue(new Callback() {
68+
69+
public void onResponse(Call call, Response response) throws IOException {
70+
assertThat(response.code(), equalTo(200));
71+
}
72+
73+
public void onFailure(Call call, IOException e) {
74+
75+
}
76+
});
77+
}
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.okhttp;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
import okhttp3.Call;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
12+
public class OkHttpHeaderTest {
13+
14+
private static final String SAMPLE_URL = "http://www.github.com";
15+
16+
@Test
17+
public void whenSetHeader_thenCorrect() throws IOException {
18+
19+
OkHttpClient client = new OkHttpClient();
20+
21+
Request request = new Request.Builder()
22+
.url(SAMPLE_URL)
23+
.addHeader("Content-Type", "application/json")
24+
.build();
25+
26+
Call call = client.newCall(request);
27+
Response response = call.execute();
28+
response.close();
29+
}
30+
31+
@Test
32+
public void whenSetDefaultHeader_thenCorrect() throws IOException {
33+
34+
OkHttpClient client = new OkHttpClient.Builder()
35+
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
36+
.build();
37+
38+
Request request = new Request.Builder()
39+
.url(SAMPLE_URL)
40+
.build();
41+
42+
Call call = client.newCall(request);
43+
Response response = call.execute();
44+
response.close();
45+
}
46+
47+
48+
}

0 commit comments

Comments
 (0)