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