Support @HttpExchange for server-side handling

See gh-30980
This commit is contained in:
Olga MaciaszekSharma
2023-08-02 16:39:53 +02:00
committed by rstoyanchev
parent 646fd3edcc
commit d1d5b54f12
8 changed files with 668 additions and 11 deletions

View File

@@ -47,11 +47,14 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.servlet.handler.PathPatternsParameterizedTest;
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
import org.springframework.web.servlet.mvc.condition.MediaTypeExpression;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
@@ -62,6 +65,7 @@ import static org.mockito.Mockito.mock;
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Olga Maciaszek-Sharma
*/
public class RequestMappingHandlerMappingTests {
@@ -278,6 +282,53 @@ public class RequestMappingHandlerMappingTests {
assertComposedAnnotationMapping(RequestMethod.PATCH);
}
@SuppressWarnings("DataFlowIssue")
@Test
void httpExchangeWithDefaultValues() throws NoSuchMethodException {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setApplicationContext(new StaticWebApplicationContext());
mapping.afterPropertiesSet();
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
HttpExchangeController.class.getMethod("defaultValuesExchange"),
HttpExchangeController.class);
assertThat(mappingInfo.getPathPatternsCondition().getPatterns())
.extracting(PathPattern::toString)
.containsOnly("/exchange");
assertThat(mappingInfo.getMethodsCondition().getMethods()).isEmpty();
assertThat(mappingInfo.getParamsCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getHeadersCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getConsumesCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getProducesCondition().getExpressions()).isEmpty();
}
@SuppressWarnings("DataFlowIssue")
@Test
void httpExchangeWithCustomValues() throws NoSuchMethodException {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setApplicationContext(new StaticWebApplicationContext());
mapping.afterPropertiesSet();
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
HttpExchangeController.class.getMethod("customValuesExchange"),
HttpExchangeController.class);
assertThat(mappingInfo.getPathPatternsCondition().getPatterns())
.extracting(PathPattern::toString)
.containsOnly("/exchange/custom");
assertThat(mappingInfo.getMethodsCondition().getMethods())
.containsOnly(RequestMethod.POST);
assertThat(mappingInfo.getParamsCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getHeadersCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getConsumesCondition().getExpressions())
.extracting(MediaTypeExpression::getMediaType)
.containsOnly(MediaType.APPLICATION_JSON);
assertThat(mappingInfo.getProducesCondition().getExpressions())
.extracting(MediaTypeExpression::getMediaType)
.containsOnly(MediaType.valueOf("text/plain;charset=UTF-8"));
}
private RequestMappingInfo assertComposedAnnotationMapping(RequestMethod requestMethod) throws Exception {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
@@ -367,6 +418,18 @@ public class RequestMappingHandlerMappingTests {
}
}
@RestController
@HttpExchange("/exchange")
static class HttpExchangeController {
@HttpExchange
public void defaultValuesExchange(){}
@HttpExchange(value = "/custom", accept = "text/plain;charset=UTF-8",
method = "POST", contentType = "application/json")
public void customValuesExchange(){}
}
private static class Foo {
}