Skip to content

Commit 5a17f2c

Browse files
committed
Fixup tests that rely on TestRestTemplate redirects
See gh-43431
1 parent 284c78f commit 5a17f2c

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-saml2-service-provider/src/test/java/smoketest/saml2/serviceprovider/SampleSaml2RelyingPartyApplicationTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2425
import org.springframework.boot.test.context.SpringBootTest;
2526
import org.springframework.boot.test.web.client.TestRestTemplate;
2627
import org.springframework.boot.test.web.server.LocalServerPort;
@@ -40,7 +41,8 @@ class SampleSaml2RelyingPartyApplicationTests {
4041

4142
@Test
4243
void everythingShouldRedirectToLogin() {
43-
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
44+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
45+
.getForEntity("/", String.class);
4446
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
4547
assertThat(entity.getHeaders().getLocation()).isEqualTo(URI.create("http://localhost:" + this.port + "/login"));
4648
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-web-secure-custom/src/test/java/smoketest/web/secure/custom/SampleWebSecureCustomApplicationTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2425
import org.springframework.boot.test.context.SpringBootTest;
2526
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2627
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -55,8 +56,8 @@ class SampleWebSecureCustomApplicationTests {
5556
void testHome() {
5657
HttpHeaders headers = new HttpHeaders();
5758
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
58-
ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<>(headers),
59-
String.class);
59+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
60+
.exchange("/", HttpMethod.GET, new HttpEntity<>(headers), String.class);
6061
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
6162
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/login");
6263
}
@@ -79,8 +80,8 @@ void testLogin() {
7980
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
8081
form.set("username", "user");
8182
form.set("password", "password");
82-
ResponseEntity<String> entity = this.restTemplate.exchange("/login", HttpMethod.POST,
83-
new HttpEntity<>(form, headers), String.class);
83+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
84+
.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class);
8485
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
8586
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/");
8687
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-web-secure-jdbc/src/test/java/smoketest/web/secure/jdbc/SampleWebSecureJdbcApplicationTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2425
import org.springframework.boot.test.context.SpringBootTest;
2526
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2627
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -55,8 +56,8 @@ class SampleWebSecureJdbcApplicationTests {
5556
void testHome() {
5657
HttpHeaders headers = new HttpHeaders();
5758
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
58-
ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<>(headers),
59-
String.class);
59+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
60+
.exchange("/", HttpMethod.GET, new HttpEntity<>(headers), String.class);
6061
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
6162
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/login");
6263
}
@@ -79,8 +80,8 @@ void testLogin() {
7980
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
8081
form.set("username", "user");
8182
form.set("password", "user");
82-
ResponseEntity<String> entity = this.restTemplate.exchange("/login", HttpMethod.POST,
83-
new HttpEntity<>(form, headers), String.class);
83+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
84+
.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class);
8485
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
8586
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/");
8687
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-web-secure/src/test/java/smoketest/web/secure/SampleWebSecureApplicationTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2526
import org.springframework.boot.test.context.SpringBootTest;
2627
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2728
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -62,8 +63,8 @@ class SampleWebSecureApplicationTests {
6263
void testHome() {
6364
HttpHeaders headers = new HttpHeaders();
6465
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
65-
ResponseEntity<String> entity = this.restTemplate.exchange("/home", HttpMethod.GET, new HttpEntity<>(headers),
66-
String.class);
66+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
67+
.exchange("/home", HttpMethod.GET, new HttpEntity<>(headers), String.class);
6768
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
6869
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/login");
6970
}
@@ -86,8 +87,8 @@ void testLogin() {
8687
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
8788
form.set("username", "user");
8889
form.set("password", "password");
89-
ResponseEntity<String> entity = this.restTemplate.exchange("/login", HttpMethod.POST,
90-
new HttpEntity<>(form, headers), String.class);
90+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
91+
.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class);
9192
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
9293
assertThat(entity.getHeaders().getLocation().toString()).endsWith(this.port + "/");
9394
}

0 commit comments

Comments
 (0)