fix unstable tests in libraries-http-2
This commit is contained in:
parent
bd5891852c
commit
d3ee95024f
@ -84,7 +84,6 @@
|
|||||||
<okhttp.version>3.14.2</okhttp.version>
|
<okhttp.version>3.14.2</okhttp.version>
|
||||||
<gson.version>2.8.5</gson.version>
|
<gson.version>2.8.5</gson.version>
|
||||||
<mockwebserver.version>3.14.2</mockwebserver.version>
|
<mockwebserver.version>3.14.2</mockwebserver.version>
|
||||||
<!-- <jackson.version>2.9.8</jackson.version>-->
|
|
||||||
<jetty.httpclient.version>1.0.3</jetty.httpclient.version>
|
<jetty.httpclient.version>1.0.3</jetty.httpclient.version>
|
||||||
<jetty.server.version>9.4.19.v20190610</jetty.server.version>
|
<jetty.server.version>9.4.19.v20190610</jetty.server.version>
|
||||||
<rxjava2.version>2.2.11</rxjava2.version>
|
<rxjava2.version>2.2.11</rxjava2.version>
|
||||||
|
13
libraries-http-2/src/main/resources/logback.xml
Normal file
13
libraries-http-2/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -3,23 +3,16 @@ package com.baeldung.jetty.httpclient;
|
|||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.eclipse.jetty.server.Handler;
|
import org.eclipse.jetty.server.Handler;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
|
||||||
public abstract class AbstractUnitTest {
|
public abstract class AbstractUnitTest {
|
||||||
|
|
||||||
protected HttpClient httpClient;
|
protected static HttpClient httpClient;
|
||||||
protected Server server;
|
protected static Server server;
|
||||||
protected static final String CONTENT = "Hello World!";
|
protected static final String CONTENT = "Hello World!";
|
||||||
protected final int port = 9080;
|
|
||||||
|
|
||||||
@Before
|
protected static void startClient() {
|
||||||
public void init() {
|
|
||||||
startServer(new RequestHandler());
|
|
||||||
startClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startClient() {
|
|
||||||
httpClient = new HttpClient();
|
httpClient = new HttpClient();
|
||||||
try {
|
try {
|
||||||
httpClient.start();
|
httpClient.start();
|
||||||
@ -28,7 +21,7 @@ public abstract class AbstractUnitTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startServer(Handler handler) {
|
protected static void startServer(Handler handler, int port) {
|
||||||
server = new Server(port);
|
server = new Server(port);
|
||||||
server.setHandler(handler);
|
server.setHandler(handler);
|
||||||
try {
|
try {
|
||||||
@ -38,17 +31,8 @@ public abstract class AbstractUnitTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
protected String uri(int port) {
|
||||||
public void dispose() throws Exception {
|
|
||||||
if (httpClient != null) {
|
|
||||||
httpClient.stop();
|
|
||||||
}
|
|
||||||
if (server != null) {
|
|
||||||
server.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String uri() {
|
|
||||||
return "http://localhost:" + port;
|
return "http://localhost:" + port;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,18 +4,29 @@ import org.eclipse.jetty.client.api.Request;
|
|||||||
import org.eclipse.jetty.http.HttpStatus;
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
||||||
import org.junit.Assert;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.Test;
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
|
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public class ProjectReactorUnitTest extends AbstractUnitTest {
|
public class ProjectReactorUnitTest extends AbstractUnitTest {
|
||||||
|
|
||||||
|
protected static int port = 9080;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
|
startServer(new RequestHandler(), port);
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception {
|
public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception {
|
||||||
|
|
||||||
Request request = httpClient.newRequest(uri());
|
Request request = httpClient.newRequest(uri(port));
|
||||||
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
||||||
.build();
|
.build();
|
||||||
Publisher<ReactiveResponse> publisher = reactiveRequest.response();
|
Publisher<ReactiveResponse> publisher = reactiveRequest.response();
|
||||||
@ -23,8 +34,19 @@ public class ProjectReactorUnitTest extends AbstractUnitTest {
|
|||||||
ReactiveResponse response = Mono.from(publisher)
|
ReactiveResponse response = Mono.from(publisher)
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
Assert.assertNotNull(response);
|
assertNotNull(response);
|
||||||
Assert.assertEquals(response.getStatus(), HttpStatus.OK_200);
|
assertEquals(response.getStatus(), HttpStatus.OK_200);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void dispose() throws Exception {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.stop();
|
||||||
|
}
|
||||||
|
if (server != null) {
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,27 @@ import org.eclipse.jetty.client.api.Request;
|
|||||||
import org.eclipse.jetty.http.HttpStatus;
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
||||||
import org.junit.Assert;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.Test;
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
|
|
||||||
public class ReactiveStreamsUnitTest extends AbstractUnitTest {
|
public class ReactiveStreamsUnitTest extends AbstractUnitTest {
|
||||||
|
|
||||||
|
protected static int port = 9081;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
|
startServer(new RequestHandler(), port);
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception {
|
public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception {
|
||||||
|
|
||||||
Request request = httpClient.newRequest(uri());
|
Request request = httpClient.newRequest(uri(port));
|
||||||
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
||||||
.build();
|
.build();
|
||||||
Publisher<ReactiveResponse> publisher = reactiveRequest.response();
|
Publisher<ReactiveResponse> publisher = reactiveRequest.response();
|
||||||
@ -21,8 +32,18 @@ public class ReactiveStreamsUnitTest extends AbstractUnitTest {
|
|||||||
BlockingSubscriber subscriber = new BlockingSubscriber();
|
BlockingSubscriber subscriber = new BlockingSubscriber();
|
||||||
publisher.subscribe(subscriber);
|
publisher.subscribe(subscriber);
|
||||||
ReactiveResponse response = subscriber.block();
|
ReactiveResponse response = subscriber.block();
|
||||||
Assert.assertNotNull(response);
|
assertNotNull(response);
|
||||||
Assert.assertEquals(response.getStatus(), HttpStatus.OK_200);
|
assertEquals(response.getStatus(), HttpStatus.OK_200);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void dispose() throws Exception {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.stop();
|
||||||
|
}
|
||||||
|
if (server != null) {
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,11 @@ import org.eclipse.jetty.http.HttpStatus;
|
|||||||
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveRequest.Event.Type;
|
import org.eclipse.jetty.reactive.client.ReactiveRequest.Event.Type;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
import org.eclipse.jetty.reactive.client.ReactiveResponse;
|
||||||
import org.junit.Assert;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.Test;
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
|
||||||
@ -20,10 +23,18 @@ import io.reactivex.Single;
|
|||||||
|
|
||||||
public class RxJava2UnitTest extends AbstractUnitTest {
|
public class RxJava2UnitTest extends AbstractUnitTest {
|
||||||
|
|
||||||
|
protected static int port = 9082;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
|
startServer(new RequestHandler(), port);
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenReactiveClient_whenRequestedWithBody_ShouldReturnBody() throws Exception {
|
public void givenReactiveClient_whenRequestedWithBody_ShouldReturnBody() throws Exception {
|
||||||
|
|
||||||
Request request = httpClient.newRequest(uri());
|
Request request = httpClient.newRequest(uri(port));
|
||||||
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request)
|
||||||
.content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8))
|
.content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8))
|
||||||
.build();
|
.build();
|
||||||
@ -32,12 +43,12 @@ public class RxJava2UnitTest extends AbstractUnitTest {
|
|||||||
String responseContent = Single.fromPublisher(publisher)
|
String responseContent = Single.fromPublisher(publisher)
|
||||||
.blockingGet();
|
.blockingGet();
|
||||||
|
|
||||||
Assert.assertEquals(CONTENT, responseContent);
|
assertEquals(CONTENT, responseContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenReactiveClient_whenRequested_ShouldPrintEvents() throws Exception {
|
public void givenReactiveClient_whenRequested_ShouldPrintEvents() throws Exception {
|
||||||
ReactiveRequest request = ReactiveRequest.newBuilder(httpClient, uri())
|
ReactiveRequest request = ReactiveRequest.newBuilder(httpClient, uri(port))
|
||||||
.content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8))
|
.content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8))
|
||||||
.build();
|
.build();
|
||||||
Publisher<ReactiveRequest.Event> requestEvents = request.requestEvents();
|
Publisher<ReactiveRequest.Event> requestEvents = request.requestEvents();
|
||||||
@ -58,10 +69,20 @@ public class RxJava2UnitTest extends AbstractUnitTest {
|
|||||||
int actualStatus = response.blockingGet()
|
int actualStatus = response.blockingGet()
|
||||||
.getStatus();
|
.getStatus();
|
||||||
|
|
||||||
Assert.assertEquals(6, requestEventTypes.size());
|
assertEquals(6, requestEventTypes.size());
|
||||||
Assert.assertEquals(5, responseEventTypes.size());
|
assertEquals(5, responseEventTypes.size());
|
||||||
|
|
||||||
Assert.assertEquals(actualStatus, HttpStatus.OK_200);
|
assertEquals(actualStatus, HttpStatus.OK_200);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void dispose() throws Exception {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.stop();
|
||||||
|
}
|
||||||
|
if (server != null) {
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,8 +1,11 @@
|
|||||||
package com.baeldung.jetty.httpclient;
|
package com.baeldung.jetty.httpclient;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.junit.Assert;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.Test;
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.client.reactive.ClientHttpConnector;
|
import org.springframework.http.client.reactive.ClientHttpConnector;
|
||||||
import org.springframework.http.client.reactive.JettyClientHttpConnector;
|
import org.springframework.http.client.reactive.JettyClientHttpConnector;
|
||||||
@ -13,24 +16,39 @@ import reactor.core.publisher.Mono;
|
|||||||
|
|
||||||
public class SpringWebFluxUnitTest extends AbstractUnitTest {
|
public class SpringWebFluxUnitTest extends AbstractUnitTest {
|
||||||
|
|
||||||
|
protected static int port = 9083;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void init() {
|
||||||
|
startServer(new RequestHandler(), port);
|
||||||
|
startClient();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenReactiveClient_whenRequested_shouldReturnResponse() throws Exception {
|
public void givenReactiveClient_whenRequested_shouldReturnResponse() throws Exception {
|
||||||
|
|
||||||
HttpClient httpClient = new HttpClient();
|
|
||||||
httpClient.start();
|
|
||||||
|
|
||||||
ClientHttpConnector clientConnector = new JettyClientHttpConnector(httpClient);
|
ClientHttpConnector clientConnector = new JettyClientHttpConnector(httpClient);
|
||||||
WebClient client = WebClient.builder()
|
WebClient client = WebClient.builder()
|
||||||
.clientConnector(clientConnector)
|
.clientConnector(clientConnector)
|
||||||
.build();
|
.build();
|
||||||
String responseContent = client.post()
|
String responseContent = client.post()
|
||||||
.uri(uri())
|
.uri(uri(port))
|
||||||
.contentType(MediaType.TEXT_PLAIN)
|
.contentType(MediaType.TEXT_PLAIN)
|
||||||
.body(BodyInserters.fromPublisher(Mono.just(CONTENT), String.class))
|
.body(BodyInserters.fromPublisher(Mono.just(CONTENT), String.class))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
.block();
|
.block();
|
||||||
Assert.assertNotNull(responseContent);
|
assertNotNull(responseContent);
|
||||||
Assert.assertEquals(CONTENT, responseContent);
|
assertEquals(CONTENT, responseContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void dispose() throws Exception {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.stop();
|
||||||
|
}
|
||||||
|
if (server != null) {
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user