clean exception testing
@@ -10,8 +10,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.ResourceAccessException;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.*;
// Tests worked with Spring Boot 2.7.5 with random port configuration
// Not working after migration to Spring Boot 3.0
@@ -33,15 +32,14 @@ public class HerokuWebSecurityConfigTest {
.defaultHeader("x-forwarded-proto", "value")
.build();
var rootAddress = "http://localhost:" + port + "/heroku";//note we loosely ask for "heroku" to be part of the url
- try {
- //old, but will fail in srping security 2022
- var result = restTemplate.getForEntity(rootAddress, String.class);
- assertEquals(HttpStatus.FOUND, result.getStatusCode());
- assertEquals("https", result.getHeaders().getLocation().getScheme());
- } catch (ResourceAccessException e) {
- assertEquals(e.getCause().getClass(), HttpHostConnectException.class);
- assertEquals(e.getCause().getMessage(), "Connect to https://localhost:8443 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused");
- }
+
+ //in Spring security 2022 we no longer can bind to the new port of the redirect as that is not preset. hence this exception proves the redirect
+ Exception exception = assertThrows(ResourceAccessException.class, () -> {
+ restTemplate.getForEntity(rootAddress, String.class);
+ });
+
+ assertEquals(exception.getCause().getClass(), HttpHostConnectException.class);
+ assertEquals(exception.getCause().getMessage(), "Connect to https://localhost:8443 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused");
}
@Test
