Skip to content

Commit 5eee67b

Browse files
committed
Fixup tests that rely on TestRestTemplate redirects
See gh-43431
1 parent 47c230b commit 5eee67b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-oauth2-authorization-server/src/test/java/smoketest/oauth2/server/SampleOAuth2AuthorizationServerApplicationTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
2728
import org.springframework.boot.test.context.SpringBootTest;
2829
import org.springframework.boot.test.web.client.TestRestTemplate;
2930
import org.springframework.boot.test.web.server.LocalServerPort;
@@ -61,7 +62,6 @@ void openidConfigurationShouldAllowAccess() {
6162
ResponseEntity<Map<String, Object>> entity = this.restTemplate.exchange("/.well-known/openid-configuration",
6263
HttpMethod.GET, null, MAP_TYPE_REFERENCE);
6364
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
64-
6565
OidcProviderConfiguration config = OidcProviderConfiguration.withClaims(entity.getBody()).build();
6666
assertThat(config.getIssuer()).hasToString("https://provider.com");
6767
assertThat(config.getAuthorizationEndpoint()).hasToString("https://provider.com/authorize");
@@ -80,7 +80,6 @@ void authServerMetadataShouldAllowAccess() {
8080
ResponseEntity<Map<String, Object>> entity = this.restTemplate
8181
.exchange("/.well-known/oauth-authorization-server", HttpMethod.GET, null, MAP_TYPE_REFERENCE);
8282
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
83-
8483
OAuth2AuthorizationServerMetadata config = OAuth2AuthorizationServerMetadata.withClaims(entity.getBody())
8584
.build();
8685
assertThat(config.getIssuer()).hasToString("https://provider.com");
@@ -95,7 +94,8 @@ void authServerMetadataShouldAllowAccess() {
9594

9695
@Test
9796
void anonymousShouldRedirectToLogin() {
98-
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
97+
ResponseEntity<String> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
98+
.getForEntity("/", String.class);
9999
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
100100
assertThat(entity.getHeaders().getLocation()).isEqualTo(URI.create("http://localhost:" + this.port + "/login"));
101101
}
@@ -160,8 +160,8 @@ void anonymousTokenRequestWithAcceptHeaderTextHtmlShouldRedirectToLogin() {
160160
body.add(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue());
161161
body.add(OAuth2ParameterNames.SCOPE, "message.read message.write");
162162
HttpEntity<Object> request = new HttpEntity<>(body, headers);
163-
ResponseEntity<Map<String, Object>> entity = this.restTemplate.exchange("/token", HttpMethod.POST, request,
164-
MAP_TYPE_REFERENCE);
163+
ResponseEntity<Map<String, Object>> entity = this.restTemplate.withRedirects(Redirects.DONT_FOLLOW)
164+
.exchange("/token", HttpMethod.POST, request, MAP_TYPE_REFERENCE);
165165
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
166166
assertThat(entity.getHeaders().getLocation()).isEqualTo(URI.create("http://localhost:" + this.port + "/login"));
167167
}

0 commit comments

Comments
 (0)