|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 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.
|
|
25 | 25 | import java.util.Arrays;
|
26 | 26 | import java.util.Date;
|
27 | 27 | import java.util.List;
|
| 28 | +import java.util.Map; |
28 | 29 | import java.util.Optional;
|
29 | 30 |
|
30 | 31 | import jakarta.servlet.http.HttpServletRequest;
|
|
34 | 35 |
|
35 | 36 | import org.springframework.context.annotation.Bean;
|
36 | 37 | import org.springframework.core.annotation.AliasFor;
|
| 38 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 39 | +import org.springframework.core.env.MapPropertySource; |
| 40 | +import org.springframework.core.env.StandardEnvironment; |
37 | 41 | import org.springframework.format.annotation.DateTimeFormat;
|
38 | 42 | import org.springframework.format.annotation.DateTimeFormat.ISO;
|
39 | 43 | import org.springframework.http.HttpEntity;
|
40 | 44 | import org.springframework.http.MediaType;
|
| 45 | +import org.springframework.lang.Nullable; |
41 | 46 | import org.springframework.stereotype.Controller;
|
42 | 47 | import org.springframework.util.MultiValueMap;
|
43 | 48 | import org.springframework.web.bind.annotation.GetMapping;
|
@@ -146,6 +151,31 @@ public void fromControllerWithCustomBaseUrlViaInstance() {
|
146 | 151 | assertThat(builder.toUriString()).isEqualTo("https://example.org:9090/base");
|
147 | 152 | }
|
148 | 153 |
|
| 154 | + @Test |
| 155 | + public void fromControllerWithPlaceholder() { |
| 156 | + StandardEnvironment environment = new StandardEnvironment(); |
| 157 | + environment.getPropertySources().addFirst(new MapPropertySource("test", |
| 158 | + Map.of("context.test.mapping", "people"))); |
| 159 | + initWebApplicationContext(WebConfig.class, environment); |
| 160 | + UriComponents uriComponents = fromController(ConfigurablePersonController.class).build(); |
| 161 | + assertThat(uriComponents.toUriString()).endsWith("/people"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void fromControllerWithPlaceholderAndMissingValue() { |
| 166 | + StandardEnvironment environment = new StandardEnvironment(); |
| 167 | + assertThat(environment.containsProperty("context.test.mapping")).isFalse(); |
| 168 | + initWebApplicationContext(WebConfig.class, environment); |
| 169 | + UriComponents uriComponents = fromController(ConfigurablePersonController.class).build(); |
| 170 | + assertThat(uriComponents.toUriString()).endsWith("/${context.test.mapping}"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void fromControllerWithPlaceholderAndNoValueResolver() { |
| 175 | + UriComponents uriComponents = fromController(ConfigurablePersonController.class).build(); |
| 176 | + assertThat(uriComponents.toUriString()).endsWith("/${context.test.mapping}"); |
| 177 | + } |
| 178 | + |
149 | 179 | @Test
|
150 | 180 | public void usesForwardedHostAsHostIfHeaderIsSet() throws Exception {
|
151 | 181 | this.request.setScheme("https");
|
@@ -293,6 +323,17 @@ public void fromMethodNameWithMetaAnnotation() {
|
293 | 323 | assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/input");
|
294 | 324 | }
|
295 | 325 |
|
| 326 | + @Test |
| 327 | + public void fromMethodNameConfigurablePath() { |
| 328 | + StandardEnvironment environment = new StandardEnvironment(); |
| 329 | + environment.getPropertySources().addFirst(new MapPropertySource("test", |
| 330 | + Map.of("method.test.mapping", "custom"))); |
| 331 | + initWebApplicationContext(WebConfig.class, environment); |
| 332 | + UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, |
| 333 | + "methodWithConfigurableMapping", "1").build(); |
| 334 | + assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo"); |
| 335 | + } |
| 336 | + |
296 | 337 | @Test
|
297 | 338 | public void fromMethodCallOnSubclass() {
|
298 | 339 | UriComponents uriComponents = fromMethodCall(on(ExtendedController.class).myMethod(null)).build();
|
@@ -522,7 +563,14 @@ public void fromMethodWithPrefix() {
|
522 | 563 | }
|
523 | 564 |
|
524 | 565 | private void initWebApplicationContext(Class<?> configClass) {
|
| 566 | + initWebApplicationContext(configClass, null); |
| 567 | + } |
| 568 | + |
| 569 | + private void initWebApplicationContext(Class<?> configClass, @Nullable ConfigurableEnvironment environment) { |
525 | 570 | AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
| 571 | + if (environment != null) { |
| 572 | + context.setEnvironment(environment); |
| 573 | + } |
526 | 574 | context.setServletContext(new MockServletContext());
|
527 | 575 | context.register(configClass);
|
528 | 576 | context.refresh();
|
@@ -574,6 +622,11 @@ private class InvalidController {
|
574 | 622 | }
|
575 | 623 |
|
576 | 624 |
|
| 625 | + @RequestMapping("/${context.test.mapping}") |
| 626 | + interface ConfigurablePersonController { |
| 627 | + } |
| 628 | + |
| 629 | + |
577 | 630 | private class UnmappedController {
|
578 | 631 |
|
579 | 632 | @RequestMapping
|
@@ -636,6 +689,11 @@ HttpEntity<Void> methodWithOptionalParam(@RequestParam(defaultValue = "") String
|
636 | 689 | HttpEntity<Void> methodWithOptionalNamedParam(@RequestParam("search") Optional<String> q) {
|
637 | 690 | return null;
|
638 | 691 | }
|
| 692 | + |
| 693 | + @RequestMapping("/${method.test.mapping}/{id}/foo") |
| 694 | + HttpEntity<Void> methodWithConfigurableMapping(@PathVariable String id) { |
| 695 | + return null; |
| 696 | + } |
639 | 697 | }
|
640 | 698 |
|
641 | 699 |
|
|
0 commit comments