|
| 1 | +package com.baeldung.serverconfig; |
| 2 | + |
| 3 | +import io.netty.handler.ssl.SslContext; |
| 4 | +import io.netty.handler.ssl.SslContextBuilder; |
| 5 | +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
| 6 | +import javax.net.ssl.SSLException; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 12 | +import org.springframework.http.client.reactive.ReactorClientHttpConnector; |
| 13 | +import org.springframework.test.context.ActiveProfiles; |
| 14 | +import org.springframework.test.context.junit4.SpringRunner; |
| 15 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 16 | +import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; |
| 17 | +import reactor.netty.http.client.HttpClient; |
| 18 | + |
| 19 | +@RunWith(SpringRunner.class) |
| 20 | +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) |
| 21 | +public class GreetingLiveTest { |
| 22 | + |
| 23 | + private static final String BASE_URL = "https://localhost:8443"; |
| 24 | + |
| 25 | + private WebTestClient webTestClient; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setup() throws SSLException { |
| 29 | + webTestClient = WebTestClient.bindToServer(getConnector()) |
| 30 | + .baseUrl(BASE_URL) |
| 31 | + .build(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void shouldGreet() { |
| 36 | + final String name = "Baeldung"; |
| 37 | + |
| 38 | + ResponseSpec response = webTestClient.get() |
| 39 | + .uri("/greet/{name}", name) |
| 40 | + .exchange(); |
| 41 | + |
| 42 | + response.expectStatus() |
| 43 | + .isOk() |
| 44 | + .expectBody(String.class) |
| 45 | + .isEqualTo("Greeting Baeldung"); |
| 46 | + } |
| 47 | + |
| 48 | + private ReactorClientHttpConnector getConnector() throws SSLException { |
| 49 | + SslContext sslContext = SslContextBuilder |
| 50 | + .forClient() |
| 51 | + .trustManager(InsecureTrustManagerFactory.INSTANCE) |
| 52 | + .build(); |
| 53 | + HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext)); |
| 54 | + return new ReactorClientHttpConnector(httpClient); |
| 55 | + } |
| 56 | +} |
0 commit comments