WIP:itflux
[demos/testing] / src / test / java / de / juplo / demo / DemoApplicationIT.java
1 package de.juplo.demo;
2
3 import org.junit.jupiter.api.Test;
4 import static org.mockserver.matchers.Times.exactly;
5 import static org.mockserver.model.HttpForward.forward;
6 import static org.mockserver.model.HttpRequest.request;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
13 import org.springframework.http.MediaType;
14 import org.springframework.test.web.reactive.server.WebTestClient;
15 import org.springframework.web.reactive.function.client.WebClient;
16
17 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
18 @EnableAutoConfiguration
19 class DemoApplicationIT extends IntegrationTestBase
20 {
21   @Autowired
22   WebTestClient webClient;
23
24
25   @Test
26   void test() throws Exception
27   {
28     MOCK_SERVER
29         .when(request().withPath("/test.txt"), exactly(1))
30         .forward(forward()
31             .withHost(NGINX.getContainerIpAddress())
32             .withPort(NGINX.getMappedPort(80)));
33     webClient
34         .get()
35         .uri("/?path=test.txt")
36         .exchange()
37         .expectStatus().isOk()
38         .expectHeader().contentType(MediaType.TEXT_HTML)
39         .expectBody().xpath("/html/body/main/div/div").isEqualTo("Hello World!");
40   }
41
42   @Configuration
43   static class Application
44   {
45     @Bean
46     RemoteContentService service()
47     {
48       return new RemoteContentService(
49           WebClient
50               .builder()
51               .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort())
52               .build());
53     }
54
55     @Bean
56     HtmlController controller(RemoteContentService service)
57     {
58       return new HtmlController(service);
59     }
60   }
61 }