|
| 1 | +package org.baeldung.web; |
| 2 | + |
| 3 | +import org.apache.http.client.HttpClient; |
| 4 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| 5 | +import org.apache.http.impl.client.HttpClients; |
| 6 | +import org.apache.http.ssl.SSLContextBuilder; |
| 7 | +import org.baeldung.ssl.HttpsEnabledApplication; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.core.io.Resource; |
| 13 | +import org.springframework.http.*; |
| 14 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 15 | +import org.springframework.test.context.ActiveProfiles; |
| 16 | +import org.springframework.test.context.junit4.SpringRunner; |
| 17 | +import org.springframework.web.client.RestTemplate; |
| 18 | + |
| 19 | +import javax.net.ssl.SSLContext; |
| 20 | +import java.util.Base64; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
| 24 | +@RunWith(SpringRunner.class) |
| 25 | +@SpringBootTest(classes = HttpsEnabledApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) |
| 26 | +@ActiveProfiles("ssl") |
| 27 | +public class HttpsApplicationIntegrationTest { |
| 28 | + |
| 29 | + private static final String WELCOME_URL = "https://localhost:8443/welcome"; |
| 30 | + |
| 31 | + @Value("${trust.store}") |
| 32 | + private Resource trustStore; |
| 33 | + |
| 34 | + @Value("${trust.store.password}") |
| 35 | + private String trustStorePassword; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenGETanHTTPSResource_thenCorrectResponse() throws Exception { |
| 39 | + ResponseEntity<String> response = restTemplate().exchange(WELCOME_URL, HttpMethod.GET, new HttpEntity<String>(withAuthorization("memuser", "pass")), String.class); |
| 40 | + |
| 41 | + assertEquals("<h1>Welcome to Secured Site</h1>", response.getBody()); |
| 42 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 43 | + } |
| 44 | + |
| 45 | + RestTemplate restTemplate() throws Exception { |
| 46 | + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()) |
| 47 | + .build(); |
| 48 | + SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext); |
| 49 | + HttpClient httpClient = HttpClients.custom() |
| 50 | + .setSSLSocketFactory(socketFactory) |
| 51 | + .build(); |
| 52 | + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); |
| 53 | + return new RestTemplate(factory); |
| 54 | + } |
| 55 | + |
| 56 | + HttpHeaders withAuthorization(String userName, String password) { |
| 57 | + return new HttpHeaders() { |
| 58 | + { |
| 59 | + String auth = userName + ":" + password; |
| 60 | + String authHeader = "Basic " + new String(Base64.getEncoder() |
| 61 | + .encode(auth.getBytes())); |
| 62 | + set("Authorization", authHeader); |
| 63 | + } |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments