Commit cdebfcde authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.5.x'

parents 9464eea7 5be5b137
...@@ -6258,11 +6258,16 @@ public class MyTest { ...@@ -6258,11 +6258,16 @@ public class MyTest {
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful `TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
in integration tests. You can get a vanilla template or one that sends Basic HTTP in integration tests. You can get a vanilla template or one that sends Basic HTTP
authentication (with a username and password). In either case the template will behave authentication (with a username and password). In either case the template will behave
in a test-friendly way: not following redirects (so you can assert the response location), in a test-friendly way by not throwing exceptions on server-side errors. It is
ignoring cookies (so the template is stateless), and not throwing exceptions on recommended, but not mandatory, to use Apache HTTP Client (version 4.3.2 or better), and
server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client if you have that on your classpath the `TestRestTemplate` will respond by configuring
(version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate` the client appropriately. If you do use Apache's HTTP client some additional test-friendly
will respond by configuring the client appropriately. features will be enabled:
* Redirects will not be followed (so you can assert the response location)
* Cookies will be ignored (so the template is stateless)
`TestRestTemplate` can be instantiated directly in your integration tests:
[source,java,indent=0] [source,java,indent=0]
---- ----
...@@ -6272,17 +6277,18 @@ public class MyTest { ...@@ -6272,17 +6277,18 @@ public class MyTest {
@Test @Test
public void testRequest() throws Exception { public void testRequest() throws Exception {
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders(); HttpHeaders headers = template.getForEntity("http://myhost.com/example", String.class).getHeaders();
assertThat(headers.getLocation().toString(), containsString("myotherhost")); assertThat(headers.getLocation().toString(), containsString("myotherhost"));
} }
} }
---- ----
If you are using the `@SpringBootTest` annotation with `WebEnvironment.RANDOM_PORT` or Alternatively, if you are using the `@SpringBootTest` annotation with
`WebEnvironment.DEFINED_PORT`, you can just inject a fully configured `TestRestTemplate` `WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can just inject a
and start using it. If necessary, additional customizations can be applied via the fully configured `TestRestTemplate` and start using it. If necessary, additional
`RestTemplateBuilder` bean: customizations can be applied via the `RestTemplateBuilder` bean. Any URLs that do not
specify a host and port will automatically connect to the embedded server:
[source,java,indent=0] [source,java,indent=0]
---- ----
...@@ -6295,7 +6301,7 @@ public class MyTest { ...@@ -6295,7 +6301,7 @@ public class MyTest {
@Test @Test
public void testRequest() throws Exception { public void testRequest() throws Exception {
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders(); HttpHeaders headers = template.getForEntity("/example", String.class).getHeaders();
assertThat(headers.getLocation().toString(), containsString("myotherhost")); assertThat(headers.getLocation().toString(), containsString("myotherhost"));
} }
......
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