Commit 07a50bb1 authored by Andy Wilkinson's avatar Andy Wilkinson

Update the samples to make use of auto-configured TestRestTemplate

Closes gh-6730
parent 3c5cf028
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -42,14 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -42,14 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleActuatorLog4J2ApplicationTests { public class SampleActuatorLog4J2ApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
.getForEntity("http://localhost:" + this.port, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -22,7 +22,7 @@ import java.util.Map; ...@@ -22,7 +22,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -47,15 +47,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -47,15 +47,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleActuatorUiApplicationTests { public class SampleActuatorUiApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
"http://localhost:" + this.port, HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Hello"); assertThat(entity.getBody()).contains("<title>Hello");
...@@ -63,8 +62,8 @@ public class SampleActuatorUiApplicationTests { ...@@ -63,8 +62,8 @@ public class SampleActuatorUiApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
...@@ -72,8 +71,8 @@ public class SampleActuatorUiApplicationTests { ...@@ -72,8 +71,8 @@ public class SampleActuatorUiApplicationTests {
@Test @Test
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics",
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
} }
...@@ -81,9 +80,8 @@ public class SampleActuatorUiApplicationTests { ...@@ -81,9 +80,8 @@ public class SampleActuatorUiApplicationTests {
public void testError() throws Exception { public void testError() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/error",
"http://localhost:" + this.port + "/error", HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody()).contains("<html>").contains("<body>") assertThat(entity.getBody()).contains("<html>").contains("<body>")
.contains("Please contact the operator with the above information"); .contains("Please contact the operator with the above information");
......
...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith; ...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,14 +48,14 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { ...@@ -49,14 +48,14 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testCustomErrorPath() throws Exception { public void testCustomErrorPath() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/oops", Map.class); .withBasicAuth("user", getPassword()).getForEntity("/oops", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -66,9 +65,9 @@ public class EndpointsPropertiesSampleActuatorApplicationTests { ...@@ -66,9 +65,9 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
@Test @Test
public void testCustomContextPath() throws Exception { public void testCustomContextPath() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/admin/health", .withBasicAuth("user", getPassword())
String.class); .getForEntity("/admin/health", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).contains("\"status\":\"UP\"");
assertThat(entity.getBody()).contains("\"hello\":\"world\""); assertThat(entity.getBody()).contains("\"hello\":\"world\"");
......
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -46,14 +46,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -46,14 +46,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@ActiveProfiles("unsecure-management") @ActiveProfiles("unsecure-management")
public class InsecureManagementSampleActuatorApplicationTests { public class InsecureManagementSampleActuatorApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
.getForEntity("http://localhost:" + this.port, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -70,8 +69,8 @@ public class InsecureManagementSampleActuatorApplicationTests { ...@@ -70,8 +69,8 @@ public class InsecureManagementSampleActuatorApplicationTests {
// ignore; // ignore;
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics",
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -44,14 +44,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -44,14 +44,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class InsecureSampleActuatorApplicationTests { public class InsecureSampleActuatorApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
.getForEntity("http://localhost:" + this.port, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -43,13 +43,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -43,13 +43,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class ManagementPathSampleActuatorApplicationTests { public class ManagementPathSampleActuatorApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate.getForEntity("/admin/health",
"http://localhost:" + this.port + "/admin/health", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).contains("\"status\":\"UP\"");
} }
...@@ -57,8 +57,7 @@ public class ManagementPathSampleActuatorApplicationTests { ...@@ -57,8 +57,7 @@ public class ManagementPathSampleActuatorApplicationTests {
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
.getForEntity("http://localhost:" + this.port, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith; ...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -48,14 +47,14 @@ public class NoManagementSampleActuatorApplicationTests { ...@@ -48,14 +47,14 @@ public class NoManagementSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@LocalServerPort @Autowired
private int port = 0; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port, Map.class); .withBasicAuth("user", getPassword()).getForEntity("/", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -66,8 +65,8 @@ public class NoManagementSampleActuatorApplicationTests { ...@@ -66,8 +65,8 @@ public class NoManagementSampleActuatorApplicationTests {
public void testMetricsNotAvailable() throws Exception { public void testMetricsNotAvailable() throws Exception {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class); .withBasicAuth("user", getPassword()).getForEntity("/metrics", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
} }
......
...@@ -19,7 +19,7 @@ package sample.actuator; ...@@ -19,7 +19,7 @@ package sample.actuator;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -41,13 +41,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -41,13 +41,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class NonSensitiveHealthTests { public class NonSensitiveHealthTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testSecureHealth() throws Exception { public void testSecureHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/health",
.getForEntity("http://localhost:" + this.port + "/health", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).doesNotContain("\"hello\":1"); assertThat(entity.getBody()).doesNotContain("\"hello\":1");
} }
......
...@@ -26,7 +26,6 @@ import org.junit.runner.RunWith; ...@@ -26,7 +26,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -55,14 +54,13 @@ public class SampleActuatorApplicationTests { ...@@ -55,14 +54,13 @@ public class SampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/", Map.class);
.getForEntity("http://localhost:" + this.port, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -73,25 +71,22 @@ public class SampleActuatorApplicationTests { ...@@ -73,25 +71,22 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testMetricsIsSecure() throws Exception { public void testMetricsIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/metrics",
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate() entity = this.restTemplate.getForEntity("/metrics/", Map.class);
.getForEntity("http://localhost:" + this.port + "/metrics/", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate().getForEntity( entity = this.restTemplate.getForEntity("/metrics/foo", Map.class);
"http://localhost:" + this.port + "/metrics/foo", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
entity = new TestRestTemplate().getForEntity( entity = this.restTemplate.getForEntity("/metrics.json", Map.class);
"http://localhost:" + this.port + "/metrics.json", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
} }
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port, Map.class); .withBasicAuth("user", getPassword()).getForEntity("/", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -102,8 +97,8 @@ public class SampleActuatorApplicationTests { ...@@ -102,8 +97,8 @@ public class SampleActuatorApplicationTests {
public void testMetrics() throws Exception { public void testMetrics() throws Exception {
testHome(); // makes sure some requests have been made testHome(); // makes sure some requests have been made
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class); .withBasicAuth("user", getPassword()).getForEntity("/metrics", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -113,8 +108,8 @@ public class SampleActuatorApplicationTests { ...@@ -113,8 +108,8 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testEnv() throws Exception { public void testEnv() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/env", Map.class); .withBasicAuth("user", getPassword()).getForEntity("/env", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -123,8 +118,8 @@ public class SampleActuatorApplicationTests { ...@@ -123,8 +118,8 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/health",
.getForEntity("http://localhost:" + this.port + "/health", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).contains("\"status\":\"UP\"");
assertThat(entity.getBody()).doesNotContain("\"hello\":\"1\""); assertThat(entity.getBody()).doesNotContain("\"hello\":\"1\"");
...@@ -132,16 +127,17 @@ public class SampleActuatorApplicationTests { ...@@ -132,16 +127,17 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testSecureHealth() throws Exception { public void testSecureHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/health", String.class); .withBasicAuth("user", getPassword())
.getForEntity("/health", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"hello\":1"); assertThat(entity.getBody()).contains("\"hello\":1");
} }
@Test @Test
public void testInfo() throws Exception { public void testInfo() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/info",
.getForEntity("http://localhost:" + this.port + "/info", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()) assertThat(entity.getBody())
.contains("\"artifact\":\"spring-boot-sample-actuator\""); .contains("\"artifact\":\"spring-boot-sample-actuator\"");
...@@ -154,8 +150,8 @@ public class SampleActuatorApplicationTests { ...@@ -154,8 +150,8 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testErrorPage() throws Exception { public void testErrorPage() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/foo", String.class); .withBasicAuth("user", getPassword()).getForEntity("/foo", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
String body = entity.getBody(); String body = entity.getBody();
assertThat(body).contains("\"error\":"); assertThat(body).contains("\"error\":");
...@@ -166,9 +162,9 @@ public class SampleActuatorApplicationTests { ...@@ -166,9 +162,9 @@ public class SampleActuatorApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<?> request = new HttpEntity<Void>(headers); HttpEntity<?> request = new HttpEntity<Void>(headers);
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = this.restTemplate
.exchange("http://localhost:" + this.port + "/foo", HttpMethod.GET, .withBasicAuth("user", getPassword())
request, String.class); .exchange("/foo", HttpMethod.GET, request, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
String body = entity.getBody(); String body = entity.getBody();
assertThat(body).as("Body was null").isNotNull(); assertThat(body).as("Body was null").isNotNull();
...@@ -177,11 +173,10 @@ public class SampleActuatorApplicationTests { ...@@ -177,11 +173,10 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testTrace() throws Exception { public void testTrace() throws Exception {
new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/health", this.restTemplate.getForEntity("/health", String.class);
String.class);
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<List> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/trace", List.class); .withBasicAuth("user", getPassword()).getForEntity("/trace", List.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Map<String, Object>> list = entity.getBody(); List<Map<String, Object>> list = entity.getBody();
...@@ -195,8 +190,7 @@ public class SampleActuatorApplicationTests { ...@@ -195,8 +190,7 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testErrorPageDirectAccess() throws Exception { public void testErrorPageDirectAccess() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/error", Map.class);
.getForEntity("http://localhost:" + this.port + "/error", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -208,8 +202,8 @@ public class SampleActuatorApplicationTests { ...@@ -208,8 +202,8 @@ public class SampleActuatorApplicationTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBeans() throws Exception { public void testBeans() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<List> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/beans", List.class); .withBasicAuth("user", getPassword()).getForEntity("/beans", List.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).hasSize(1); assertThat(entity.getBody()).hasSize(1);
Map<String, Object> body = (Map<String, Object>) entity.getBody().get(0); Map<String, Object> body = (Map<String, Object>) entity.getBody().get(0);
...@@ -219,9 +213,9 @@ public class SampleActuatorApplicationTests { ...@@ -219,9 +213,9 @@ public class SampleActuatorApplicationTests {
@Test @Test
public void testConfigProps() throws Exception { public void testConfigProps() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/configprops", .withBasicAuth("user", getPassword())
Map.class); .getForEntity("/configprops", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -44,14 +44,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -44,14 +44,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class ServletPathInsecureSampleActuatorApplicationTests { public class ServletPathInsecureSampleActuatorApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/",
.getForEntity("http://localhost:" + this.port + "/spring/", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -62,8 +62,8 @@ public class ServletPathInsecureSampleActuatorApplicationTests { ...@@ -62,8 +62,8 @@ public class ServletPathInsecureSampleActuatorApplicationTests {
@Test @Test
public void testMetricsIsSecure() throws Exception { public void testMetricsIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/metrics",
"http://localhost:" + this.port + "/spring/metrics", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
} }
......
...@@ -21,7 +21,7 @@ import java.util.Map; ...@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -43,15 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -43,15 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class ServletPathSampleActuatorApplicationTests { public class ServletPathSampleActuatorApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testErrorPath() throws Exception { public void testErrorPath() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", "password") ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/error",
.getForEntity("http://localhost:" + this.port + "/spring/error", Map.class);
Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -61,8 +60,8 @@ public class ServletPathSampleActuatorApplicationTests { ...@@ -61,8 +60,8 @@ public class ServletPathSampleActuatorApplicationTests {
@Test @Test
public void testHealth() throws Exception { public void testHealth() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate.getForEntity("/spring/health",
"http://localhost:" + this.port + "/spring/health", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("\"status\":\"UP\""); assertThat(entity.getBody()).contains("\"status\":\"UP\"");
} }
...@@ -70,8 +69,8 @@ public class ServletPathSampleActuatorApplicationTests { ...@@ -70,8 +69,8 @@ public class ServletPathSampleActuatorApplicationTests {
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate() ResponseEntity<Map> entity = this.restTemplate.getForEntity("/spring/",
.getForEntity("http://localhost:" + this.port + "/spring/", Map.class); Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith; ...@@ -23,7 +23,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -47,14 +46,14 @@ public class ShutdownSampleActuatorApplicationTests { ...@@ -47,14 +46,14 @@ public class ShutdownSampleActuatorApplicationTests {
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port, Map.class); .withBasicAuth("user", getPassword()).getForEntity("/", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
...@@ -64,9 +63,9 @@ public class ShutdownSampleActuatorApplicationTests { ...@@ -64,9 +63,9 @@ public class ShutdownSampleActuatorApplicationTests {
@Test @Test
public void testShutdown() throws Exception { public void testShutdown() throws Exception {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<Map> entity = this.restTemplate
.postForEntity("http://localhost:" + this.port + "/shutdown", null, .withBasicAuth("user", getPassword())
Map.class); .postForEntity("/shutdown", null, Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody(); Map<String, Object> body = entity.getBody();
......
...@@ -16,20 +16,18 @@ ...@@ -16,20 +16,18 @@
package sample.hateoas; package sample.hateoas;
import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
...@@ -39,13 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -39,13 +37,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleHateoasApplicationTests { public class SampleHateoasApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void hasHalLinks() throws Exception { public void hasHalLinks() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate.getForEntity("/customers/1",
"http://localhost:" + this.port + "/customers/1", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).startsWith( assertThat(entity.getBody()).startsWith(
"{\"id\":1,\"firstName\":\"Oliver\"" + ",\"lastName\":\"Gierke\""); "{\"id\":1,\"firstName\":\"Oliver\"" + ",\"lastName\":\"Gierke\"");
...@@ -56,10 +54,9 @@ public class SampleHateoasApplicationTests { ...@@ -56,10 +54,9 @@ public class SampleHateoasApplicationTests {
public void producesJsonWhenXmlIsPreferred() throws Exception { public void producesJsonWhenXmlIsPreferred() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8"); headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8");
RequestEntity<?> request = new RequestEntity<Void>(headers, HttpMethod.GET, HttpEntity<?> request = new HttpEntity<>(headers);
URI.create("http://localhost:" + this.port + "/customers/1")); ResponseEntity<String> response = this.restTemplate.exchange("/customers/1",
ResponseEntity<String> response = new TestRestTemplate().exchange(request, HttpMethod.GET, request, String.class);
String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().getContentType()) assertThat(response.getHeaders().getContentType())
.isEqualTo(MediaType.parseMediaType("application/json;charset=UTF-8")); .isEqualTo(MediaType.parseMediaType("application/json;charset=UTF-8"));
......
...@@ -16,20 +16,19 @@ ...@@ -16,20 +16,19 @@
package sample.hypermedia.ui; package sample.hypermedia.ui;
import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
...@@ -40,20 +39,18 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,20 +39,18 @@ import static org.assertj.core.api.Assertions.assertThat;
"management.context-path=" }) "management.context-path=" })
public class SampleHypermediaUiApplicationTests { public class SampleHypermediaUiApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void home() { public void home() {
String response = new TestRestTemplate() String response = this.restTemplate.getForObject("/", String.class);
.getForObject("http://localhost:" + this.port, String.class);
assertThat(response).contains("Hello World"); assertThat(response).contains("Hello World");
} }
@Test @Test
public void links() { public void links() {
String response = new TestRestTemplate().getForObject( String response = this.restTemplate.getForObject("/actuator", String.class);
"http://localhost:" + this.port + "/actuator", String.class);
assertThat(response).contains("\"_links\":"); assertThat(response).contains("\"_links\":");
} }
...@@ -61,10 +58,8 @@ public class SampleHypermediaUiApplicationTests { ...@@ -61,10 +58,8 @@ public class SampleHypermediaUiApplicationTests {
public void linksWithJson() throws Exception { public void linksWithJson() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
ResponseEntity<String> response = new TestRestTemplate().exchange( ResponseEntity<String> response = this.restTemplate.exchange("/actuator",
new RequestEntity<Void>(headers, HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new URI("http://localhost:" + this.port + "/actuator")),
String.class);
assertThat(response.getBody()).contains("\"_links\":"); assertThat(response.getBody()).contains("\"_links\":");
} }
...@@ -72,9 +67,8 @@ public class SampleHypermediaUiApplicationTests { ...@@ -72,9 +67,8 @@ public class SampleHypermediaUiApplicationTests {
public void homeWithHtml() throws Exception { public void homeWithHtml() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> response = new TestRestTemplate() ResponseEntity<String> response = this.restTemplate.exchange("/", HttpMethod.GET,
.exchange(new RequestEntity<Void>(headers, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new URI("http://localhost:" + this.port)), String.class);
assertThat(response.getBody()).contains("Hello World"); assertThat(response.getBody()).contains("Hello World");
} }
......
...@@ -16,20 +16,19 @@ ...@@ -16,20 +16,19 @@
package sample.hypermedia; package sample.hypermedia;
import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
...@@ -39,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -39,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleHypermediaApplicationHomePageTests { public class SampleHypermediaApplicationHomePageTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void home() { public void home() {
String response = new TestRestTemplate() String response = this.restTemplate.getForObject("/", String.class);
.getForObject("http://localhost:" + this.port, String.class);
assertThat(response).contains("404"); assertThat(response).contains("404");
} }
...@@ -53,10 +51,8 @@ public class SampleHypermediaApplicationHomePageTests { ...@@ -53,10 +51,8 @@ public class SampleHypermediaApplicationHomePageTests {
public void linksWithJson() throws Exception { public void linksWithJson() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
ResponseEntity<String> response = new TestRestTemplate().exchange( ResponseEntity<String> response = this.restTemplate.exchange("/actuator",
new RequestEntity<Void>(headers, HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new URI("http://localhost:" + this.port + "/actuator")),
String.class);
assertThat(response.getBody()).contains("\"_links\":"); assertThat(response.getBody()).contains("\"_links\":");
} }
...@@ -64,10 +60,8 @@ public class SampleHypermediaApplicationHomePageTests { ...@@ -64,10 +60,8 @@ public class SampleHypermediaApplicationHomePageTests {
public void halWithHtml() throws Exception { public void halWithHtml() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> response = new TestRestTemplate().exchange( ResponseEntity<String> response = this.restTemplate.exchange("/actuator/",
new RequestEntity<Void>(headers, HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new URI("http://localhost:" + this.port + "/actuator/")),
String.class);
assertThat(response.getBody()).contains("HAL Browser"); assertThat(response.getBody()).contains("HAL Browser");
} }
......
...@@ -19,7 +19,7 @@ package sample.jersey; ...@@ -19,7 +19,7 @@ package sample.jersey;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -33,30 +33,28 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -33,30 +33,28 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleJerseyApplicationTests { public class SampleJerseyApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
private TestRestTemplate restTemplate = new TestRestTemplate();
@Test @Test
public void contextLoads() { public void contextLoads() {
ResponseEntity<String> entity = this.restTemplate ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello",
.getForEntity("http://localhost:" + this.port + "/hello", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
} }
@Test @Test
public void reverse() { public void reverse() {
ResponseEntity<String> entity = this.restTemplate.getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/reverse?input=olleh", String.class); .getForEntity("/reverse?input=olleh", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("hello"); assertThat(entity.getBody()).isEqualTo("hello");
} }
@Test @Test
public void validation() { public void validation() {
ResponseEntity<String> entity = this.restTemplate ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse",
.getForEntity("http://localhost:" + this.port + "/reverse", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
} }
......
...@@ -19,7 +19,7 @@ package sample.jersey1; ...@@ -19,7 +19,7 @@ package sample.jersey1;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -31,14 +31,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -31,14 +31,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleJersey1ApplicationTests { public class SampleJersey1ApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void contextLoads() { public void rootReturnsHelloWorld() {
assertThat(new TestRestTemplate() assertThat(this.restTemplate.getForObject("/", String.class))
.getForObject("http://localhost:" + this.port + "/", String.class)) .isEqualTo("Hello World");
.isEqualTo("Hello World");
} }
} }
...@@ -19,7 +19,7 @@ package sample.jetty.jsp; ...@@ -19,7 +19,7 @@ package sample.jetty.jsp;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebJspApplicationTests { public class SampleWebJspApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testJspWithEl() throws Exception { public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("/resources/text.txt"); assertThat(entity.getBody()).contains("/resources/text.txt");
} }
......
...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; ...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleJettyApplicationTests { public class SampleJettyApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
...@@ -66,11 +65,8 @@ public class SampleJettyApplicationTests { ...@@ -66,11 +65,8 @@ public class SampleJettyApplicationTests {
requestHeaders.set("Accept-Encoding", "gzip"); requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
requestEntity, byte[].class);
ResponseEntity<byte[]> entity = restTemplate.exchange(
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
......
...@@ -19,11 +19,11 @@ package sample.jetty8.ssl; ...@@ -19,11 +19,11 @@ package sample.jetty8.ssl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -41,14 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -41,14 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleJetty8SslApplicationTests { public class SampleJetty8SslApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
......
...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; ...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleJetty8ApplicationTests { public class SampleJetty8ApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
...@@ -65,10 +64,8 @@ public class SampleJetty8ApplicationTests { ...@@ -65,10 +64,8 @@ public class SampleJetty8ApplicationTests {
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip"); requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
ResponseEntity<byte[]> entity = restTemplate.exchange( requestEntity, byte[].class);
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
GZIPInputStream inflater = new GZIPInputStream( GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody())); new ByteArrayInputStream(entity.getBody()));
......
...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; ...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleJetty92ApplicationTests { public class SampleJetty92ApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
...@@ -65,10 +64,8 @@ public class SampleJetty92ApplicationTests { ...@@ -65,10 +64,8 @@ public class SampleJetty92ApplicationTests {
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip"); requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
ResponseEntity<byte[]> entity = restTemplate.exchange( requestEntity, byte[].class);
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
GZIPInputStream inflater = new GZIPInputStream( GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody())); new ByteArrayInputStream(entity.getBody()));
......
...@@ -21,7 +21,6 @@ import org.junit.runner.RunWith; ...@@ -21,7 +21,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -42,23 +41,22 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -42,23 +41,22 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleServletApplicationTests { public class SampleServletApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Autowired @Autowired
private SecurityProperties security; private SecurityProperties security;
@Test @Test
public void testHomeIsSecure() throws Exception { public void testHomeIsSecure() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
} }
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", getPassword()) ResponseEntity<String> entity = this.restTemplate
.getForEntity("http://localhost:" + this.port, String.class); .withBasicAuth("user", getPassword()).getForEntity("/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
......
...@@ -18,7 +18,7 @@ package sample.testng; ...@@ -18,7 +18,7 @@ package sample.testng;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -38,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -38,13 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTestNGApplicationTests extends AbstractTestNGSpringContextTests { public class SampleTestNGApplicationTests extends AbstractTestNGSpringContextTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
......
...@@ -19,7 +19,7 @@ package sample.tomcat.jsp; ...@@ -19,7 +19,7 @@ package sample.tomcat.jsp;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebJspApplicationTests { public class SampleWebJspApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testJspWithEl() throws Exception { public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("/resources/text.txt"); assertThat(entity.getBody()).contains("/resources/text.txt");
} }
......
...@@ -19,11 +19,10 @@ package sample.tomcat.ssl; ...@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTomcatSslApplicationTests { public class SampleTomcatSslApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello, world"); assertThat(entity.getBody()).isEqualTo("Hello, world");
} }
......
...@@ -21,6 +21,7 @@ import org.junit.runner.RunWith; ...@@ -21,6 +21,7 @@ import org.junit.runner.RunWith;
import sample.tomcat.service.HelloWorldService; import sample.tomcat.service.HelloWorldService;
import sample.tomcat.web.SampleController; import sample.tomcat.web.SampleController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
...@@ -28,7 +29,6 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo ...@@ -28,7 +29,6 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -52,8 +52,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -52,8 +52,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class NonAutoConfigurationSampleTomcatApplicationTests { public class NonAutoConfigurationSampleTomcatApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Configuration @Configuration
@Import({ EmbeddedServletContainerAutoConfiguration.class, @Import({ EmbeddedServletContainerAutoConfiguration.class,
...@@ -73,8 +73,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests { ...@@ -73,8 +73,7 @@ public class NonAutoConfigurationSampleTomcatApplicationTests {
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
......
...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; ...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -49,13 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTomcatApplicationTests { public class SampleTomcatApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
...@@ -65,10 +64,8 @@ public class SampleTomcatApplicationTests { ...@@ -65,10 +64,8 @@ public class SampleTomcatApplicationTests {
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip"); requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
ResponseEntity<byte[]> entity = restTemplate.exchange( requestEntity, byte[].class);
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
GZIPInputStream inflater = new GZIPInputStream( GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody())); new ByteArrayInputStream(entity.getBody()));
......
...@@ -19,7 +19,7 @@ package sample.tomcat7.jsp; ...@@ -19,7 +19,7 @@ package sample.tomcat7.jsp;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebJspApplicationTests { public class SampleWebJspApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testJspWithEl() throws Exception { public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("/resources/text.txt"); assertThat(entity.getBody()).contains("/resources/text.txt");
} }
......
...@@ -19,11 +19,10 @@ package sample.tomcat.ssl; ...@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTomcatSslApplicationTests { public class SampleTomcatSslApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello, world"); assertThat(entity.getBody()).isEqualTo("Hello, world");
} }
......
...@@ -19,11 +19,10 @@ package sample.tomcat.ssl; ...@@ -19,11 +19,10 @@ package sample.tomcat.ssl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,14 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTomcatSslApplicationTests { public class SampleTomcatSslApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello, world"); assertThat(entity.getBody()).isEqualTo("Hello, world");
} }
......
...@@ -19,7 +19,7 @@ package sample.traditional; ...@@ -19,7 +19,7 @@ package sample.traditional;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleTraditionalApplicationTests { public class SampleTraditionalApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHomeJsp() throws Exception { public void testHomeJsp() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
String body = entity.getBody(); String body = entity.getBody();
assertThat(body).contains("<html>").contains("<h1>Home</h1>"); assertThat(body).contains("<html>").contains("<h1>Home</h1>");
...@@ -54,8 +53,8 @@ public class SampleTraditionalApplicationTests { ...@@ -54,8 +53,8 @@ public class SampleTraditionalApplicationTests {
@Test @Test
public void testStaticPage() throws Exception { public void testStaticPage() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate.getForEntity("/index.html",
"http://localhost:" + this.port + "/index.html", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
String body = entity.getBody(); String body = entity.getBody();
assertThat(body).contains("<html>").contains("<h1>Hello</h1>"); assertThat(body).contains("<html>").contains("<h1>Hello</h1>");
......
...@@ -19,11 +19,10 @@ package sample.undertow.ssl; ...@@ -19,11 +19,10 @@ package sample.undertow.ssl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
...@@ -41,14 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -41,14 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleUndertowSslApplicationTests { public class SampleUndertowSslApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
TestRestTemplate testRestTemplate = new TestRestTemplate(HttpClientOption.SSL); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello World"); assertThat(entity.getBody()).isEqualTo("Hello World");
} }
......
...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream; ...@@ -23,7 +23,7 @@ import java.util.zip.GZIPInputStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,8 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -49,8 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleUndertowApplicationTests { public class SampleUndertowApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
...@@ -67,10 +67,8 @@ public class SampleUndertowApplicationTests { ...@@ -67,10 +67,8 @@ public class SampleUndertowApplicationTests {
HttpHeaders requestHeaders = new HttpHeaders(); HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip"); requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders); HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET,
ResponseEntity<byte[]> entity = restTemplate.exchange( requestEntity, byte[].class);
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
GZIPInputStream inflater = new GZIPInputStream( GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody())); new ByteArrayInputStream(entity.getBody()));
...@@ -84,8 +82,8 @@ public class SampleUndertowApplicationTests { ...@@ -84,8 +82,8 @@ public class SampleUndertowApplicationTests {
} }
private void assertOkResponse(String path, String body) { private void assertOkResponse(String path, String body) {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity(path,
.getForEntity("http://localhost:" + this.port + path, String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo(body); assertThat(entity.getBody()).isEqualTo(body);
} }
......
...@@ -21,7 +21,7 @@ import java.util.Arrays; ...@@ -21,7 +21,7 @@ import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -47,13 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -47,13 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebFreeMarkerApplicationTests { public class SampleWebFreeMarkerApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate testRestTemplate;
@Test @Test
public void testFreeMarkerTemplate() throws Exception { public void testFreeMarkerTemplate() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.testRestTemplate.getForEntity("/",
.getForEntity("http://localhost:" + this.port, String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("Hello, Andy"); assertThat(entity.getBody()).contains("Hello, Andy");
} }
...@@ -64,9 +64,8 @@ public class SampleWebFreeMarkerApplicationTests { ...@@ -64,9 +64,8 @@ public class SampleWebFreeMarkerApplicationTests {
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( ResponseEntity<String> responseEntity = this.testRestTemplate
"http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class);
requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(responseEntity.getBody()) assertThat(responseEntity.getBody())
......
...@@ -21,6 +21,7 @@ import java.net.URI; ...@@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -47,10 +48,12 @@ public class SampleGroovyTemplateApplicationTests { ...@@ -47,10 +48,12 @@ public class SampleGroovyTemplateApplicationTests {
@LocalServerPort @LocalServerPort
private int port; private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).contains("<title>Messages");
assertThat(entity.getBody()).doesNotContain("layout:fragment"); assertThat(entity.getBody()).doesNotContain("layout:fragment");
...@@ -61,15 +64,14 @@ public class SampleGroovyTemplateApplicationTests { ...@@ -61,15 +64,14 @@ public class SampleGroovyTemplateApplicationTests {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("text", "FOO text"); map.set("text", "FOO text");
map.set("summary", "FOO"); map.set("summary", "FOO");
URI location = new TestRestTemplate() URI location = this.restTemplate.postForLocation("/", map);
.postForLocation("http://localhost:" + this.port, map);
assertThat(location.toString()).contains("localhost:" + this.port); assertThat(location.toString()).contains("localhost:" + this.port);
} }
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
......
...@@ -19,7 +19,7 @@ package sample.jsp; ...@@ -19,7 +19,7 @@ package sample.jsp;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,13 +40,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebJspApplicationTests { public class SampleWebJspApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testJspWithEl() throws Exception { public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("/resources/text.txt"); assertThat(entity.getBody()).contains("/resources/text.txt");
} }
......
...@@ -23,6 +23,7 @@ import java.util.regex.Pattern; ...@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -33,6 +34,7 @@ import org.springframework.http.HttpMethod; ...@@ -33,6 +34,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -53,12 +55,14 @@ public class SampleMethodSecurityApplicationTests { ...@@ -53,12 +55,14 @@ public class SampleMethodSecurityApplicationTests {
@LocalServerPort @LocalServerPort
private int port; private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
"http://localhost:" + this.port, HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Login"); assertThat(entity.getBody()).contains("<title>Login");
...@@ -72,8 +76,8 @@ public class SampleMethodSecurityApplicationTests { ...@@ -72,8 +76,8 @@ public class SampleMethodSecurityApplicationTests {
form.set("username", "admin"); form.set("username", "admin");
form.set("password", "admin"); form.set("password", "admin");
getCsrf(form, headers); getCsrf(form, headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.POST, HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
...@@ -89,14 +93,14 @@ public class SampleMethodSecurityApplicationTests { ...@@ -89,14 +93,14 @@ public class SampleMethodSecurityApplicationTests {
form.set("username", "user"); form.set("username", "user");
form.set("password", "user"); form.set("password", "user");
getCsrf(form, headers); getCsrf(form, headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.POST, HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
String cookie = entity.getHeaders().getFirst("Set-Cookie"); String cookie = entity.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
ResponseEntity<String> page = new TestRestTemplate().exchange( ResponseEntity<String> page = this.restTemplate.exchange(
entity.getHeaders().getLocation(), HttpMethod.GET, entity.getHeaders().getLocation(), HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(page.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
...@@ -105,28 +109,46 @@ public class SampleMethodSecurityApplicationTests { ...@@ -105,28 +109,46 @@ public class SampleMethodSecurityApplicationTests {
@Test @Test
public void testManagementProtected() throws Exception { public void testManagementProtected() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans",
.getForEntity("http://localhost:" + this.port + "/beans", String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
} }
@Test @Test
public void testManagementAuthorizedAccess() throws Exception { public void testManagementAuthorizedAccess() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("admin", "admin") BasicAuthorizationInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor(
.getForEntity("http://localhost:" + this.port + "/beans", String.class); "admin", "admin");
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); this.restTemplate.getRestTemplate().getInterceptors().add(basicAuthInterceptor);
try {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}
finally {
this.restTemplate.getRestTemplate().getInterceptors()
.remove(basicAuthInterceptor);
}
} }
@Test @Test
public void testManagementUnauthorizedAccess() throws Exception { public void testManagementUnauthorizedAccess() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate("user", "user") BasicAuthorizationInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor(
.getForEntity("http://localhost:" + this.port + "/beans", String.class); "user", "user");
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); this.restTemplate.getRestTemplate().getInterceptors().add(basicAuthInterceptor);
try {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/beans",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
}
finally {
this.restTemplate.getRestTemplate().getInterceptors()
.remove(basicAuthInterceptor);
}
} }
private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) { private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
ResponseEntity<String> page = new TestRestTemplate() ResponseEntity<String> page = this.restTemplate.getForEntity("/login",
.getForEntity("http://localhost:" + this.port + "/login", String.class); String.class);
String cookie = page.getHeaders().getFirst("Set-Cookie"); String cookie = page.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
String body = page.getBody(); String body = page.getBody();
......
...@@ -21,7 +21,7 @@ import java.util.Arrays; ...@@ -21,7 +21,7 @@ import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebMustacheApplicationTests { public class SampleWebMustacheApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testMustacheTemplate() throws Exception { public void testMustacheTemplate() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("Hello, Andy"); assertThat(entity.getBody()).contains("Hello, Andy");
} }
...@@ -63,9 +62,8 @@ public class SampleWebMustacheApplicationTests { ...@@ -63,9 +62,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( ResponseEntity<String> responseEntity = this.restTemplate
"http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class);
requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(responseEntity.getBody()) assertThat(responseEntity.getBody())
.contains("Something went wrong: 404 Not Found"); .contains("Something went wrong: 404 Not Found");
...@@ -76,9 +74,8 @@ public class SampleWebMustacheApplicationTests { ...@@ -76,9 +74,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/serviceUnavailable",
"http://localhost:" + this.port + "/serviceUnavailable", HttpMethod.GET, HttpMethod.GET, requestEntity, String.class);
requestEntity, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
assertThat(entity.getBody()).contains("I'm a 503"); assertThat(entity.getBody()).contains("I'm a 503");
} }
...@@ -88,9 +85,8 @@ public class SampleWebMustacheApplicationTests { ...@@ -88,9 +85,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/bang",
"http://localhost:" + this.port + "/bang", HttpMethod.GET, requestEntity, HttpMethod.GET, requestEntity, String.class);
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(entity.getBody()).contains("I'm a 5xx"); assertThat(entity.getBody()).contains("I'm a 5xx");
} }
...@@ -100,9 +96,8 @@ public class SampleWebMustacheApplicationTests { ...@@ -100,9 +96,8 @@ public class SampleWebMustacheApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/insufficientStorage",
"http://localhost:" + this.port + "/insufficientStorage", HttpMethod.GET, HttpMethod.GET, requestEntity, String.class);
requestEntity, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INSUFFICIENT_STORAGE); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INSUFFICIENT_STORAGE);
assertThat(entity.getBody()).contains("I'm a 507"); assertThat(entity.getBody()).contains("I'm a 507");
} }
......
...@@ -23,6 +23,7 @@ import java.util.regex.Pattern; ...@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebSecureCustomApplicationTests { public class SampleWebSecureCustomApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
...@@ -57,8 +61,7 @@ public class SampleWebSecureCustomApplicationTests { ...@@ -57,8 +61,7 @@ public class SampleWebSecureCustomApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
"http://localhost:" + this.port, HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation().toString()) assertThat(entity.getHeaders().getLocation().toString())
...@@ -69,9 +72,8 @@ public class SampleWebSecureCustomApplicationTests { ...@@ -69,9 +72,8 @@ public class SampleWebSecureCustomApplicationTests {
public void testLoginPage() throws Exception { public void testLoginPage() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("_csrf"); assertThat(entity.getBody()).contains("_csrf");
} }
...@@ -84,8 +86,8 @@ public class SampleWebSecureCustomApplicationTests { ...@@ -84,8 +86,8 @@ public class SampleWebSecureCustomApplicationTests {
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("username", "user"); form.set("username", "user");
form.set("password", "user"); form.set("password", "user");
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.POST, HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
...@@ -96,8 +98,8 @@ public class SampleWebSecureCustomApplicationTests { ...@@ -96,8 +98,8 @@ public class SampleWebSecureCustomApplicationTests {
private HttpHeaders getHeaders() { private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> page = new TestRestTemplate() ResponseEntity<String> page = this.restTemplate.getForEntity("/login",
.getForEntity("http://localhost:" + this.port + "/login", String.class); String.class);
assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK);
String cookie = page.getHeaders().getFirst("Set-Cookie"); String cookie = page.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
...@@ -110,8 +112,8 @@ public class SampleWebSecureCustomApplicationTests { ...@@ -110,8 +112,8 @@ public class SampleWebSecureCustomApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
......
...@@ -21,6 +21,7 @@ import java.net.URI; ...@@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -46,11 +47,12 @@ public class SampleGithubApplicationTests { ...@@ -46,11 +47,12 @@ public class SampleGithubApplicationTests {
@LocalServerPort @LocalServerPort
private int port; private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test @Test
public void everythingIsSecuredByDefault() throws Exception { public void everythingIsSecuredByDefault() throws Exception {
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<Void> entity = this.restTemplate.getForEntity("/", Void.class);
ResponseEntity<Void> entity = restTemplate
.getForEntity("http://localhost:" + this.port, Void.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation()) assertThat(entity.getHeaders().getLocation())
.isEqualTo(URI.create("http://localhost:" + this.port + "/login")); .isEqualTo(URI.create("http://localhost:" + this.port + "/login"));
...@@ -58,9 +60,8 @@ public class SampleGithubApplicationTests { ...@@ -58,9 +60,8 @@ public class SampleGithubApplicationTests {
@Test @Test
public void loginRedirectsToGithub() throws Exception { public void loginRedirectsToGithub() throws Exception {
TestRestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<Void> entity = this.restTemplate.getForEntity("/login",
ResponseEntity<Void> entity = restTemplate Void.class);
.getForEntity("http://localhost:" + this.port + "/login", Void.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation().toString()) assertThat(entity.getHeaders().getLocation().toString())
.startsWith("https://github.com/login/oauth"); .startsWith("https://github.com/login/oauth");
......
...@@ -23,6 +23,7 @@ import java.util.regex.Pattern; ...@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebSecureJdbcApplicationTests { public class SampleWebSecureJdbcApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
...@@ -57,8 +61,7 @@ public class SampleWebSecureJdbcApplicationTests { ...@@ -57,8 +61,7 @@ public class SampleWebSecureJdbcApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
"http://localhost:" + this.port, HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation().toString()) assertThat(entity.getHeaders().getLocation().toString())
...@@ -69,9 +72,8 @@ public class SampleWebSecureJdbcApplicationTests { ...@@ -69,9 +72,8 @@ public class SampleWebSecureJdbcApplicationTests {
public void testLoginPage() throws Exception { public void testLoginPage() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("_csrf"); assertThat(entity.getBody()).contains("_csrf");
} }
...@@ -84,8 +86,8 @@ public class SampleWebSecureJdbcApplicationTests { ...@@ -84,8 +86,8 @@ public class SampleWebSecureJdbcApplicationTests {
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("username", "user"); form.set("username", "user");
form.set("password", "user"); form.set("password", "user");
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.POST, HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
...@@ -96,8 +98,8 @@ public class SampleWebSecureJdbcApplicationTests { ...@@ -96,8 +98,8 @@ public class SampleWebSecureJdbcApplicationTests {
private HttpHeaders getHeaders() { private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> page = new TestRestTemplate() ResponseEntity<String> page = this.restTemplate.getForEntity("/login",
.getForEntity("http://localhost:" + this.port + "/login", String.class); String.class);
assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK);
String cookie = page.getHeaders().getFirst("Set-Cookie"); String cookie = page.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
...@@ -110,8 +112,8 @@ public class SampleWebSecureJdbcApplicationTests { ...@@ -110,8 +112,8 @@ public class SampleWebSecureJdbcApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
......
...@@ -23,6 +23,7 @@ import java.util.regex.Pattern; ...@@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -50,6 +51,9 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleSecureApplicationTests { public class SampleSecureApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
...@@ -57,8 +61,7 @@ public class SampleSecureApplicationTests { ...@@ -57,8 +61,7 @@ public class SampleSecureApplicationTests {
public void testHome() throws Exception { public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET,
"http://localhost:" + this.port, HttpMethod.GET,
new HttpEntity<Void>(headers), String.class); new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
assertThat(entity.getHeaders().getLocation().toString()) assertThat(entity.getHeaders().getLocation().toString())
...@@ -69,9 +72,8 @@ public class SampleSecureApplicationTests { ...@@ -69,9 +72,8 @@ public class SampleSecureApplicationTests {
public void testLoginPage() throws Exception { public void testLoginPage() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.GET, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("_csrf"); assertThat(entity.getBody()).contains("_csrf");
} }
...@@ -84,8 +86,8 @@ public class SampleSecureApplicationTests { ...@@ -84,8 +86,8 @@ public class SampleSecureApplicationTests {
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("username", "user"); form.set("username", "user");
form.set("password", "user"); form.set("password", "user");
ResponseEntity<String> entity = new TestRestTemplate().exchange( ResponseEntity<String> entity = this.restTemplate.exchange("/login",
"http://localhost:" + this.port + "/login", HttpMethod.POST, HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class); String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
...@@ -96,8 +98,8 @@ public class SampleSecureApplicationTests { ...@@ -96,8 +98,8 @@ public class SampleSecureApplicationTests {
private HttpHeaders getHeaders() { private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
ResponseEntity<String> page = new TestRestTemplate() ResponseEntity<String> page = this.restTemplate.getForEntity("/login",
.getForEntity("http://localhost:" + this.port + "/login", String.class); String.class);
assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK);
String cookie = page.getHeaders().getFirst("Set-Cookie"); String cookie = page.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie); headers.set("Cookie", cookie);
...@@ -110,8 +112,8 @@ public class SampleSecureApplicationTests { ...@@ -110,8 +112,8 @@ public class SampleSecureApplicationTests {
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
......
...@@ -19,7 +19,7 @@ package sample.web.staticcontent; ...@@ -19,7 +19,7 @@ package sample.web.staticcontent;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -41,24 +41,20 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -41,24 +41,20 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebStaticApplicationTests { public class SampleWebStaticApplicationTests {
@LocalServerPort @Autowired
private int port = 0; private TestRestTemplate restTemplate;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Static"); assertThat(entity.getBody()).contains("<title>Static");
} }
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity(
.getForEntity( "/webjars/bootstrap/3.0.3/css/bootstrap.min.css", String.class);
"http://localhost:" + this.port
+ "/webjars/bootstrap/3.0.3/css/bootstrap.min.css",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
assertThat(entity.getHeaders().getContentType()) assertThat(entity.getHeaders().getContentType())
......
...@@ -21,6 +21,7 @@ import java.net.URI; ...@@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebThymeleaf3ApplicationTests { public class SampleWebThymeleaf3ApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).contains("<title>Messages");
assertThat(entity.getBody()).doesNotContain("layout:fragment"); assertThat(entity.getBody()).doesNotContain("layout:fragment");
...@@ -61,15 +64,14 @@ public class SampleWebThymeleaf3ApplicationTests { ...@@ -61,15 +64,14 @@ public class SampleWebThymeleaf3ApplicationTests {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("text", "FOO text"); map.set("text", "FOO text");
map.set("summary", "FOO"); map.set("summary", "FOO");
URI location = new TestRestTemplate() URI location = this.restTemplate.postForLocation("/", map);
.postForLocation("http://localhost:" + this.port, map);
assertThat(location.toString()).contains("localhost:" + this.port); assertThat(location.toString()).contains("localhost:" + this.port);
} }
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); .getForEntity("/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
} }
......
...@@ -21,6 +21,7 @@ import java.net.URI; ...@@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
...@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -44,13 +45,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebUiApplicationTests { public class SampleWebUiApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort @LocalServerPort
private int port; private int port;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Messages"); assertThat(entity.getBody()).contains("<title>Messages");
assertThat(entity.getBody()).doesNotContain("layout:fragment"); assertThat(entity.getBody()).doesNotContain("layout:fragment");
...@@ -61,14 +64,13 @@ public class SampleWebUiApplicationTests { ...@@ -61,14 +64,13 @@ public class SampleWebUiApplicationTests {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.set("text", "FOO text"); map.set("text", "FOO text");
map.set("summary", "FOO"); map.set("summary", "FOO");
URI location = new TestRestTemplate() URI location = this.restTemplate.postForLocation("/", map);
.postForLocation("http://localhost:" + this.port, map);
assertThat(location.toString()).contains("localhost:" + this.port); assertThat(location.toString()).contains("localhost:" + this.port);
} }
@Test @Test
public void testCss() throws Exception { public void testCss() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = this.restTemplate.getForEntity(
"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class); "http://localhost:" + this.port + "/css/bootstrap.min.css", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("body"); assertThat(entity.getBody()).contains("body");
......
...@@ -21,7 +21,7 @@ import java.util.Arrays; ...@@ -21,7 +21,7 @@ import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -47,13 +47,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@DirtiesContext @DirtiesContext
public class SampleWebVelocityApplicationTests { public class SampleWebVelocityApplicationTests {
@LocalServerPort @Autowired
private int port; private TestRestTemplate restTemplate;
@Test @Test
public void testVelocityTemplate() throws Exception { public void testVelocityTemplate() throws Exception {
ResponseEntity<String> entity = new TestRestTemplate() ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
.getForEntity("http://localhost:" + this.port, String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("Hello, Andy"); assertThat(entity.getBody()).contains("Hello, Andy");
} }
...@@ -64,9 +63,8 @@ public class SampleWebVelocityApplicationTests { ...@@ -64,9 +63,8 @@ public class SampleWebVelocityApplicationTests {
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
HttpEntity<String> requestEntity = new HttpEntity<String>(headers); HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
ResponseEntity<String> responseEntity = new TestRestTemplate().exchange( ResponseEntity<String> responseEntity = this.restTemplate
"http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, .exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class);
requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(responseEntity.getBody()) assertThat(responseEntity.getBody())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment