BAEL-4205 - A Guide to Events in OkHTTP (#10734)

* BAEL-4706 - Spring Boot with Spring Batch

* BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite
changed

* BAEL-4736 - Convert JSONArray to List of Object using camel-jackson

* BAEL-4756 - Mockito MockSettings

* BAEL-4756 - Mockito MockSettings - fix spelling

* BAEL-2674 - Upgrade the Okhttp article

* BAEL-4204 - Adding Interceptors in OkHTTP

* BAEL-4836 - Mocking Static Methods with Mockito

* BAEL-4205 - A Guide to Events in OkHTTP

Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
Jonathan Cook
2021-05-07 17:38:12 +02:00
committed by GitHub
parent 2715a162a9
commit 087a668a45
4 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package com.baeldung.okhttp.events;
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class EventTimerLiveTest {
@Test
public void givenSimpleEventTimer_whenRequestSent_thenCallsLogged() throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(new EventTimer())
.build();
Request request = new Request.Builder()
.url("https://www.baeldung.com/")
.build();
try (Response response = client.newCall(request).execute()) {
assertEquals("Response code should be: ", 200, response.code());
}
}
}

View File

@@ -0,0 +1,53 @@
package com.baeldung.okhttp.events;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.SocketTimeoutException;
import org.junit.Rule;
import org.junit.Test;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class LogEventsListenerIntegrationTest {
@Rule
public MockWebServer server = new MockWebServer();
@Test
public void givenSimpleEventLogger_whenRequestSent_thenCallsLogged() throws IOException {
server.enqueue(new MockResponse().setBody("Hello Baeldung Readers!"));
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(new SimpleLogEventsListener())
.build();
Request request = new Request.Builder()
.url(server.url("/"))
.build();
try (Response response = client.newCall(request).execute()) {
assertEquals("Response code should be: ", 200, response.code());
assertEquals("Body should be: ", "Hello Baeldung Readers!", response.body().string());
}
}
@Test (expected = SocketTimeoutException.class)
public void givenConnectionError_whenRequestSent_thenFailedCallsLogged() throws IOException {
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(new EventTimer())
.build();
Request request = new Request.Builder()
.url(server.url("/"))
.build();
client.newCall(request).execute();
}
}