Skip to content

Commit 5be5b13

Browse files
committed
Clarify need for Apache HTTP Client to disable redirects in TestRestTemplate
Closes gh-9410
1 parent a666919 commit 5be5b13

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6047,11 +6047,16 @@ public class MyTest {
60476047
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
60486048
in integration tests. You can get a vanilla template or one that sends Basic HTTP
60496049
authentication (with a username and password). In either case the template will behave
6050-
in a test-friendly way: not following redirects (so you can assert the response location),
6051-
ignoring cookies (so the template is stateless), and not throwing exceptions on
6052-
server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client
6053-
(version 4.3.2 or better), and if you have that on your classpath the `TestRestTemplate`
6054-
will respond by configuring the client appropriately.
6050+
in a test-friendly way by not throwing exceptions on server-side errors. It is
6051+
recommended, but not mandatory, to use Apache HTTP Client (version 4.3.2 or better), and
6052+
if you have that on your classpath the `TestRestTemplate` will respond by configuring
6053+
the client appropriately. If you do use Apache's HTTP client some additional test-friendly
6054+
features will be enabled:
6055+
6056+
* Redirects will not be followed (so you can assert the response location)
6057+
* Cookies will be ignored (so the template is stateless)
6058+
6059+
`TestRestTemplate` can be instantiated directly in your integration tests:
60556060

60566061
[source,java,indent=0]
60576062
----
@@ -6061,17 +6066,18 @@ public class MyTest {
60616066
60626067
@Test
60636068
public void testRequest() throws Exception {
6064-
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders();
6069+
HttpHeaders headers = template.getForEntity("http://myhost.com/example", String.class).getHeaders();
60656070
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
60666071
}
60676072
60686073
}
60696074
----
60706075

6071-
If you are using the `@SpringBootTest` annotation with `WebEnvironment.RANDOM_PORT` or
6072-
`WebEnvironment.DEFINED_PORT`, you can just inject a fully configured `TestRestTemplate`
6073-
and start using it. If necessary, additional customizations can be applied via the
6074-
`RestTemplateBuilder` bean:
6076+
Alternatively, if you are using the `@SpringBootTest` annotation with
6077+
`WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can just inject a
6078+
fully configured `TestRestTemplate` and start using it. If necessary, additional
6079+
customizations can be applied via the `RestTemplateBuilder` bean. Any URLs that do not
6080+
specify a host and port will automatically connect to the embedded server:
60756081

60766082
[source,java,indent=0]
60776083
----
@@ -6084,7 +6090,7 @@ public class MyTest {
60846090
60856091
@Test
60866092
public void testRequest() throws Exception {
6087-
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders();
6093+
HttpHeaders headers = template.getForEntity("/example", String.class).getHeaders();
60886094
assertThat(headers.getLocation().toString(), containsString("myotherhost"));
60896095
}
60906096

0 commit comments

Comments
 (0)