[BAEL-8499] - Moved server related codes / articles to libraries-server module
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.jetty;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
|
||||
public class JettyIntegrationTest {
|
||||
private static JettyServer jettyServer;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws Exception {
|
||||
jettyServer = new JettyServer();
|
||||
jettyServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() throws Exception {
|
||||
jettyServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenSendRequestToBlockingServlet_thenReturnStatusOK() throws Exception {
|
||||
// given
|
||||
String url = "http://localhost:8090/status";
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(url);
|
||||
HttpResponse response = client.execute(request);
|
||||
|
||||
// then
|
||||
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenSendRequestToNonBlockingServlet_thenReturnStatusOK() throws Exception {
|
||||
// when
|
||||
String url = "http://localhost:8090/heavy/async";
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(url);
|
||||
HttpResponse response = client.execute(request);
|
||||
|
||||
// then
|
||||
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
|
||||
String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
|
||||
assertThat(responseContent).isEqualTo("This is some heavy resource that will be served in an async way");
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package com.baeldung.jetty;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link JettyServerFactory}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class JettyServerFactoryUnitTest {
|
||||
|
||||
/**
|
||||
* Tests that when a base server is provided a request returns a status 404.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void givenBaseServer_whenHttpRequest_thenStatus404() throws Exception {
|
||||
Server server = JettyServerFactory.createBaseServer();
|
||||
server.start();
|
||||
|
||||
int statusCode = sendGetRequest();
|
||||
|
||||
Assert.assertEquals(404, statusCode);
|
||||
server.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that when a web app server is provided a request returns a status
|
||||
* 200.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void givenWebAppServer_whenHttpRequest_thenStatus200() throws Exception {
|
||||
Server server = JettyServerFactory.createWebAppServer();
|
||||
server.start();
|
||||
|
||||
int statusCode = sendGetRequest();
|
||||
|
||||
Assert.assertEquals(200, statusCode);
|
||||
server.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that when a multi handler server is provided a request returns a
|
||||
* status 200.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void givenMultiHandlerServerServer_whenHttpRequest_thenStatus200() throws Exception {
|
||||
Server server = JettyServerFactory.createMultiHandlerServer();
|
||||
server.start();
|
||||
|
||||
int statusCode = sendGetRequest();
|
||||
|
||||
Assert.assertEquals(200, statusCode);
|
||||
server.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a default HTTP GET request to the server and returns the response
|
||||
* status code.
|
||||
*
|
||||
* @return the status code of the response
|
||||
* @throws Exception
|
||||
*/
|
||||
private int sendGetRequest() throws Exception {
|
||||
HttpHost target = new HttpHost("localhost", JettyServerFactory.SERVER_PORT);
|
||||
HttpRequest request = new HttpGet(JettyServerFactory.APP_PATH);
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpResponse response = client.execute(target, request);
|
||||
return response.getStatusLine().getStatusCode();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package com.baeldung.netty;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.handler.codec.http.DefaultFullHttpRequest;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.netty.handler.codec.http.HttpVersion;
|
||||
|
||||
public class EmbeddedChannelUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testPipeline() {
|
||||
|
||||
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
|
||||
"/calculate?a=10&b=5");
|
||||
httpRequest.headers().add("Operator", "Add");
|
||||
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
channel.pipeline().addFirst(new HttpMessageHandler()).addLast(new CalculatorOperationHandler());
|
||||
|
||||
// send HTTP request to server and check that the message is on the inbound pipeline
|
||||
assertThat(channel.writeInbound(httpRequest)).isTrue();
|
||||
|
||||
long inboundChannelResponse = channel.readInbound();
|
||||
assertThat(inboundChannelResponse).isEqualTo(15);
|
||||
|
||||
// we should have an outbound message in the form of a HTTP response
|
||||
assertThat(channel.outboundMessages().size()).isEqualTo(1);
|
||||
// Object response = channel.readOutbound();
|
||||
|
||||
FullHttpResponse httpResponse = channel.readOutbound();
|
||||
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(httpResponseContent).isEqualTo("15");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() {
|
||||
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
|
||||
"/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Add");
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
// send invalid HTTP request to server and expect and error
|
||||
channel.pipeline().fireChannelRead(wrongHttpRequest);
|
||||
channel.checkException();
|
||||
}).isInstanceOf(UnsupportedOperationException.class)
|
||||
.hasMessage("HTTP method not supported");
|
||||
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
|
||||
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() {
|
||||
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
|
||||
|
||||
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
|
||||
"/calculate?a=10&b=5");
|
||||
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
|
||||
|
||||
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
|
||||
assertThatThrownBy(() -> {
|
||||
channel.writeInbound(wrongHttpRequest);
|
||||
}).isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("Operation not defined");
|
||||
|
||||
// the outbound message is a HTTP response with the status code 500
|
||||
FullHttpResponse errorHttpResponse = channel.readOutbound();
|
||||
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
|
||||
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
|
||||
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.tomcat;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Created by adi on 1/14/18.
|
||||
*/
|
||||
@RunWith(BlockJUnit4ClassRunner.class)
|
||||
public class ProgrammaticTomcatIntegrationTest {
|
||||
|
||||
private ProgrammaticTomcat tomcat = new ProgrammaticTomcat();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tomcat.startTomcat();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
tomcat.stopTomcat();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTomcatStarted_whenAccessServlet_responseIsTestAndResponseHeaderIsSet() throws Exception {
|
||||
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||
HttpGet getServlet = new HttpGet("http://localhost:8080/my-servlet");
|
||||
|
||||
HttpResponse response = httpClient.execute(getServlet);
|
||||
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
|
||||
|
||||
String myHeaderValue = response.getFirstHeader("myHeader").getValue();
|
||||
assertEquals("myHeaderValue", myHeaderValue);
|
||||
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
assertNotNull(responseEntity);
|
||||
|
||||
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
assertEquals("test", responseString);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user