|
| 1 | +package com.baeldung.jetty; |
| 2 | + |
| 3 | + |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.apache.http.HttpResponse; |
| 6 | +import org.apache.http.client.HttpClient; |
| 7 | +import org.apache.http.client.methods.HttpGet; |
| 8 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | + |
| 15 | +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; |
| 16 | + |
| 17 | +public class JettyTest { |
| 18 | + private JettyServer jettyServer; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup() throws Exception { |
| 22 | + jettyServer = new JettyServer(); |
| 23 | + jettyServer.start(); |
| 24 | + } |
| 25 | + |
| 26 | + @After |
| 27 | + public void cleanup() throws Exception { |
| 28 | + Thread.sleep(2000); |
| 29 | + jettyServer.stop(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenServer_whenSendRequestToBlockingServlet_thenReturnStatusOK() throws Exception { |
| 34 | + //given |
| 35 | + String url = "http://localhost:8090/status"; |
| 36 | + HttpClient client = HttpClientBuilder.create().build(); |
| 37 | + HttpGet request = new HttpGet(url); |
| 38 | + HttpResponse response = client.execute(request); |
| 39 | + |
| 40 | + //then |
| 41 | + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenServer_whenSendRequestToNonBlockingServlet_thenReturnStatusOK() throws Exception { |
| 47 | + //when |
| 48 | + String url = "http://localhost:8090/heavy/async"; |
| 49 | + HttpClient client = HttpClientBuilder.create().build(); |
| 50 | + HttpGet request = new HttpGet(url); |
| 51 | + HttpResponse response = client.execute(request); |
| 52 | + |
| 53 | + //then |
| 54 | + assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); |
| 55 | + String responseContent = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); |
| 56 | + assertThat(responseContent).isEqualTo("This is some heavy resource that will be served in an async way"); |
| 57 | + } |
| 58 | +} |
0 commit comments