contributeApplicationDefaults
+ */
+ @Contribute(SymbolProvider.class)
+ @ApplicationDefaults
+ public static void setupEnvironment(MappedConfigurationdevelopment.
+ */
+public class DevelopmentModule {
+ public static void contributeApplicationDefaults(
+ MappedConfigurationqa ("quality assurance").
+ */
+public class QaModule
+{
+ public static void bind(ServiceBinder binder)
+ {
+ // Bind any services needed by the QA team to produce their reports
+ // binder.bind(MyServiceMonitorInterface.class, MyServiceMonitorImpl.class);
+ }
+
+
+ public static void contributeApplicationDefaults(
+ MappedConfigurationThe current time is: ${currentTime}
+
+
+
The current time is: ${currentTime}
++ This is a template for a simple marketing or informational website. It includes a large callout called + the hero unit and three supporting pieces of content. Use it as a starting point to create something + more unique. +
+Clink the bottom link and the page refresh with event complete
Click the bottom link to update just the middle column with Ajax call with event ajax
I'v been updated through AJAX call
+The current time is: ${currentTime}
+spring-rest module before running this live test
- */
-public class OkHttpPostingLiveTest {
-
- private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
- private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
-
- OkHttpClient client;
-
- @Before
- public void init() {
-
- client = new OkHttpClient();
- }
-
- @Test
- public void whenSendPostRequest_thenCorrect() throws IOException {
- final RequestBody formBody = new FormBody.Builder().add("username", "test").add("password", "test").build();
-
- final Request request = new Request.Builder().url(BASE_URL + "/users").post(formBody).build();
-
- final Call call = client.newCall(request);
- final Response response = call.execute();
-
- assertThat(response.code(), equalTo(200));
- }
-
- @Test
- public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
- final String postBody = "test post";
-
- final 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"), "test post")).build();
-
- final Call call = client.newCall(request);
- final Response response = call.execute();
-
- assertThat(response.code(), equalTo(200));
- }
-
- @Test
- public void whenPostJson_thenCorrect() throws IOException {
- final String json = "{\"id\":1,\"name\":\"John\"}";
-
- final RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"id\":1,\"name\":\"John\"}");
- final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
-
- final Call call = client.newCall(request);
- final Response response = call.execute();
-
- assertThat(response.code(), equalTo(200));
- }
-
- @Test
- public void whenSendMultipartRequest_thenCorrect() throws IOException {
- final 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();
-
- final Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build();
-
- final Call call = client.newCall(request);
- final Response response = call.execute();
-
- assertThat(response.code(), equalTo(200));
- }
-}
+package com.baeldung.okhttp.post;
+
+import static com.baeldung.client.Consts.APPLICATION_PORT;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+
+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;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Execute spring-rest module before running this live test
+ */
+public class OkHttpPostingLiveTest {
+
+ private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
+ private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
+
+ OkHttpClient client;
+
+ @Before
+ public void init() {
+
+ client = new OkHttpClient();
+ }
+
+ @Test
+ public void whenSendPostRequest_thenCorrect() throws IOException {
+ final RequestBody formBody = new FormBody.Builder()
+ .add("username", "test")
+ .add("password", "test")
+ .build();
+
+ final Request request = new Request.Builder()
+ .url(BASE_URL + "/users")
+ .post(formBody)
+ .build();
+
+ final Call call = client.newCall(request);
+ final Response response = call.execute();
+
+ assertThat(response.code(), equalTo(200));
+ }
+
+ @Test
+ public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
+ final String postBody = "test post";
+
+ final 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"), "test post"))
+ .build();
+
+ final Call call = client.newCall(request);
+ final Response response = call.execute();
+
+ assertThat(response.code(), equalTo(200));
+ }
+
+ @Test
+ public void whenPostJson_thenCorrect() throws IOException {
+ final String json = "{\"id\":1,\"name\":\"John\"}";
+
+ final RequestBody body = RequestBody.create(MediaType.parse("application/json"), "{\"id\":1,\"name\":\"John\"}");
+ final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
+
+ final Call call = client.newCall(request);
+ final Response response = call.execute();
+
+ assertThat(response.code(), equalTo(200));
+ }
+
+ @Test
+ public void whenPostJsonWithoutCharset_thenCharsetIsUtf8() throws IOException {
+ final String json = "{\"id\":1,\"name\":\"John\"}";
+
+ final RequestBody body = RequestBody.create(
+ MediaType.parse("application/json"), json);
+
+ String charset = body.contentType().charset().displayName();
+
+ assertThat(charset, equalTo("UTF-8"));
+ }
+
+ @Test
+ public void whenPostJsonWithUtf16Charset_thenCharsetIsUtf16() throws IOException {
+ final String json = "{\"id\":1,\"name\":\"John\"}";
+
+ final RequestBody body = RequestBody.create(
+ MediaType.parse("application/json; charset=utf-16"), json);
+
+ String charset = body.contentType().charset().displayName();
+
+ assertThat(charset, equalTo("UTF-16"));
+ }
+
+ @Test
+ public void whenSendMultipartRequest_thenCorrect() throws IOException {
+ final 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();
+
+ final Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build();
+
+ final Call call = client.newCall(request);
+ final Response response = call.execute();
+
+ assertThat(response.code(), equalTo(200));
+ }
+}
diff --git a/libraries-http/src/test/java/com/baeldung/okhttp/timeout/OkHttpTimeoutLiveTest.java b/libraries-http/src/test/java/com/baeldung/okhttp/timeout/OkHttpTimeoutLiveTest.java
new file mode 100644
index 0000000000..42032aef93
--- /dev/null
+++ b/libraries-http/src/test/java/com/baeldung/okhttp/timeout/OkHttpTimeoutLiveTest.java
@@ -0,0 +1,136 @@
+package com.baeldung.okhttp.timeout;
+
+import okhttp3.*;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.SocketTimeoutException;
+import java.util.concurrent.TimeUnit;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.catchThrowable;
+
+public class OkHttpTimeoutLiveTest {
+
+ private static Logger logger = LoggerFactory.getLogger(OkHttpTimeoutLiveTest.class);
+
+ private static final String HTTP_NON_ROUTABLE_ADDRESS = "http://203.0.113.1";
+ private static final String HTTPS_ADDRESS_DELAY_2 = "https://httpbin.org/delay/2";
+
+ @Test
+ public void whenConnectTimeoutExceeded_thenSocketTimeoutException() {
+ // Given
+ OkHttpClient client = new OkHttpClient.Builder()
+ .connectTimeout(10, TimeUnit.MILLISECONDS)
+ .build();
+
+ Request request = new Request.Builder()
+ .url(HTTP_NON_ROUTABLE_ADDRESS)
+ .build();
+
+ // When
+ Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
+
+ // Then
+ assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
+
+ logThrown(thrown);
+ }
+
+ @Test
+ public void whenReadTimeoutExceeded_thenSocketTimeoutException() {
+ // Given
+ OkHttpClient client = new OkHttpClient.Builder()
+ .readTimeout(10, TimeUnit.MILLISECONDS)
+ .build();
+
+ Request request = new Request.Builder()
+ .url(HTTPS_ADDRESS_DELAY_2)
+ .build();
+
+ // When
+ Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
+
+ // Then
+ assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
+
+ logThrown(thrown);
+ }
+
+ @Test
+ public void whenWriteTimeoutExceeded_thenSocketTimeoutException() {
+ // Given
+ OkHttpClient client = new OkHttpClient.Builder()
+ .writeTimeout(10, TimeUnit.MILLISECONDS)
+ .build();
+
+ Request request = new Request.Builder()
+ .url(HTTPS_ADDRESS_DELAY_2)
+ .post(RequestBody.create(MediaType.parse("text/plain"), create1MBString()))
+ .build();
+
+ // When
+ Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
+
+ // Then
+ assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
+
+ logThrown(thrown);
+ }
+
+ @Test
+ public void whenCallTimeoutExceeded_thenInterruptedIOException() {
+ // Given
+ OkHttpClient client = new OkHttpClient.Builder()
+ .callTimeout(1, TimeUnit.SECONDS)
+ .build();
+
+ Request request = new Request.Builder()
+ .url(HTTPS_ADDRESS_DELAY_2)
+ .build();
+
+ // When
+ Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
+
+ // Then
+ assertThat(thrown).isInstanceOf(InterruptedIOException.class);
+
+ logThrown(thrown);
+ }
+
+ @Test
+ public void whenPerRequestTimeoutExtended_thenResponseSuccess() throws IOException {
+ // Given
+ OkHttpClient defaultClient = new OkHttpClient.Builder()
+ .readTimeout(1, TimeUnit.SECONDS)
+ .build();
+
+ Request request = new Request.Builder()
+ .url(HTTPS_ADDRESS_DELAY_2)
+ .build();
+
+ Throwable thrown = catchThrowable(() -> defaultClient.newCall(request).execute());
+
+ assertThat(thrown).isInstanceOf(InterruptedIOException.class);
+
+ // When
+ OkHttpClient extendedTimeoutClient = defaultClient.newBuilder()
+ .readTimeout(5, TimeUnit.SECONDS)
+ .build();
+
+ // Then
+ Response response = extendedTimeoutClient.newCall(request).execute();
+ assertThat(response.code()).isEqualTo(200);
+ }
+
+ private void logThrown(Throwable thrown) {
+ logger.info("Thrown: ", thrown);
+ }
+
+ private String create1MBString() {
+ return new String(new char[512 * 1024]);
+ }
+}
diff --git a/libraries-io/pom.xml b/libraries-io/pom.xml
index 5b78ae9442..8c2e841630 100644
--- a/libraries-io/pom.xml
+++ b/libraries-io/pom.xml
@@ -1,7 +1,7 @@
Spring Security Thymeleaf tutorial
-Spring Security Thymeleaf tutorial
+