|
1 | 1 | /* |
2 | | - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
57 | 57 | import org.springframework.context.annotation.Bean; |
58 | 58 | import org.springframework.context.annotation.Configuration; |
59 | 59 | import org.springframework.stereotype.Component; |
| 60 | +import org.springframework.test.util.ReflectionTestUtils; |
60 | 61 | import org.springframework.web.filter.ForwardedHeaderFilter; |
61 | 62 | import org.springframework.web.servlet.DispatcherServlet; |
62 | 63 | import org.springframework.web.servlet.FrameworkServlet; |
@@ -369,6 +370,36 @@ void forwardedHeaderFilterWhenFilterAlreadyRegisteredShouldBackOff() { |
369 | 370 | .run((context) -> assertThat(context).hasSingleBean(FilterRegistrationBean.class)); |
370 | 371 | } |
371 | 372 |
|
| 373 | +@Test |
| 374 | +void relativeRedirectsShouldBeEnabledWhenUsingTomcatContainerAndUseRelativeRedirects() { |
| 375 | +WebApplicationContextRunner runner = new WebApplicationContextRunner( |
| 376 | +AnnotationConfigServletWebServerApplicationContext::new) |
| 377 | +.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)) |
| 378 | +.withPropertyValues("server.forward-headers-strategy=framework", |
| 379 | +"server.tomcat.use-relative-redirects=true"); |
| 380 | + |
| 381 | +runner.run((context) -> { |
| 382 | +Filter filter = context.getBean(FilterRegistrationBean.class).getFilter(); |
| 383 | +Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects"); |
| 384 | +assertThat(relativeRedirects).isTrue(); |
| 385 | +}); |
| 386 | +} |
| 387 | + |
| 388 | +@Test |
| 389 | +void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() { |
| 390 | +WebApplicationContextRunner runner = new WebApplicationContextRunner( |
| 391 | +AnnotationConfigServletWebServerApplicationContext::new) |
| 392 | +.withClassLoader(new FilteredClassLoader(Tomcat.class)) |
| 393 | +.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)) |
| 394 | +.withPropertyValues("server.forward-headers-strategy=framework"); |
| 395 | + |
| 396 | +runner.run((context) -> { |
| 397 | +Filter filter = context.getBean(FilterRegistrationBean.class).getFilter(); |
| 398 | +Boolean relativeRedirects = (Boolean) ReflectionTestUtils.getField(filter, "relativeRedirects"); |
| 399 | +assertThat(relativeRedirects).isFalse(); |
| 400 | +}); |
| 401 | +} |
| 402 | + |
372 | 403 | private ContextConsumer<AssertableWebApplicationContext> verifyContext() { |
373 | 404 | return this::verifyContext; |
374 | 405 | } |
|
0 commit comments