3 import static de.juplo.demo.IntegrationTestBase.MOCK_SERVER;
4 import java.util.regex.Pattern;
5 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
6 import org.jsoup.Jsoup;
7 import org.jsoup.nodes.Document;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import static org.mockserver.matchers.Times.exactly;
11 import static org.mockserver.model.HttpForward.forward;
12 import static org.mockserver.model.HttpRequest.request;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.context.annotation.Configuration;
19 import org.springframework.http.MediaType;
20 import org.springframework.test.web.reactive.server.WebTestClient;
21 import org.springframework.web.reactive.function.client.WebClient;
23 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
24 @EnableAutoConfiguration
25 class DemoApplicationIT extends IntegrationTestBase
27 static Pattern MISSING_PATTERN =
28 Pattern.compile("^404\\ Not\\ Found\\ from\\ GET\\ http:\\/\\/localhost:[0-9]*/missing.txt$");
32 WebTestClient webClient;
36 @DisplayName("HtmlController - Remote-Response: 200")
37 void testHtmlControllerResponse200() throws Exception
40 .when(request().withPath("/test.txt"), exactly(1))
42 .withHost(NGINX.getContainerIpAddress())
43 .withPort(NGINX.getMappedPort(80)));
47 .uri("/?path=test.txt")
49 .expectStatus().isOk()
50 .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
51 .expectBody(String.class).value(rendered ->
53 Document doc = Jsoup.parse(rendered);
55 doc.select("html > body > main > div > div > div > pre").text())
56 .isEqualTo("Hello World!");
61 @DisplayName("HtmlController - Remote-Response: 404")
62 void testHtmlControllerResponse404() throws Exception
65 .when(request().withPath("/missing.txt"), exactly(1))
67 .withHost(NGINX.getContainerIpAddress())
68 .withPort(NGINX.getMappedPort(80)));
72 .uri("/?path=missing.txt")
74 .expectStatus().isOk()
75 .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
76 .expectBody(String.class).value(rendered ->
78 Document doc = Jsoup.parse(rendered);
80 doc.select("html > body > main > div > div > div > pre").text())
81 .matches(MISSING_PATTERN);
86 @DisplayName("RestController - Remote-Response: 200")
87 void testRestController200() throws Exception
90 .when(request().withPath("/test.txt"), exactly(1))
92 .withHost(NGINX.getContainerIpAddress())
93 .withPort(NGINX.getMappedPort(80)));
97 .uri("/?path=test.txt")
98 .header("Accept", MediaType.TEXT_PLAIN_VALUE)
100 .expectStatus().isOk()
101 .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)
102 .expectBody(String.class).isEqualTo("Hello World!\n");
106 @DisplayName("RestController - Remote-Response: 404")
107 void testRestController404() throws Exception
110 .when(request().withPath("/missing.txt"), exactly(1))
112 .withHost(NGINX.getContainerIpAddress())
113 .withPort(NGINX.getMappedPort(80)));
117 .uri("/?path=missing.txt")
118 .header("Accept", MediaType.TEXT_PLAIN_VALUE)
120 .expectStatus().isNotFound()
121 .expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
123 .jsonPath("status").isEqualTo(404)
124 .jsonPath("error").isEqualTo("Not Found")
125 .jsonPath("message").isEqualTo(
126 "Cause: 404 Not Found from GET http://localhost:" +
127 MOCK_SERVER.getLocalPort() +
129 .jsonPath("timestamp").exists();
134 static class Application
137 RemoteContentService service()
139 return new RemoteContentService(
142 .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort())
147 HtmlController htmlController(RemoteContentService service)
149 return new HtmlController(service);
153 RestController restController(RemoteContentService service)
155 return new RestController(service);