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.boot.test.context.SpringBootTest;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.http.MediaType;
13 import org.springframework.test.web.reactive.server.WebTestClient;
14 import org.springframework.web.context.WebApplicationContext;
15 import org.springframework.web.reactive.function.client.WebClient;
16
17 @SpringBootTest
18 class DemoApplicationIT extends IntegrationTestBase
19 {
20   WebTestClient webClient;
21
22
23   @BeforeEach
24   void setUp(WebApplicationContext context)
25   {
26     webClient = WebTestClient.bindToApplicationContext(context).build();
27   }
28
29
30   @Test
31   void test() throws Exception
32   {
33     MOCK_SERVER
34         .when(request().withPath("/test.txt"), exactly(1))
35         .forward(forward()
36             .withHost(NGINX.getContainerIpAddress())
37             .withPort(NGINX.getMappedPort(80)));
38     webClient
39         .get()
40         .uri(URI.create("http://S.U.T/?path=test.txt"))
41         .exchange()
42         .expectStatus().is2xxSuccessful()
43         .expectHeader().contentType(MediaType.TEXT_HTML)
44         .expectBody(String.class).isEqualTo("Hello World!\n");
45   }
46
47   @Configuration
48   static class Application
49   {
50     @Bean
51     RemoteContentService service()
52     {
53       return new RemoteContentService(
54           WebClient
55               .builder()
56               .baseUrl("http://localhost:" +
57                   DemoApplicationIT.MOCK_SERVER.getLocalPort())
58               .build());
59     }
60
61     @Bean
62     HtmlController controller(RemoteContentService service)
63     {
64       return new HtmlController(service);
65     }
66   }
67 }