Add support for headers in @HttpExchange

On the client side, supports `name=value` pairs. Placeholders in values
are resolved by the `embeddedValueResolver`.
On the server side, additionally supports `name` and `!name` syntax.

Closes gh-33309
This commit is contained in:
Simon Baslé
2024-08-09 12:46:02 +02:00
parent b61eee7fb0
commit bf5e218b35
14 changed files with 181 additions and 6 deletions

View File

@@ -431,6 +431,26 @@ class RequestMappingHandlerMappingTests {
.containsOnly(MediaType.valueOf("text/plain;charset=UTF-8"));
}
@SuppressWarnings("DataFlowIssue")
@Test
void httpExchangeWithCustomHeaders() throws Exception {
RequestMappingHandlerMapping mapping = createMapping();
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
HttpExchangeController.class.getMethod("customHeadersExchange"),
HttpExchangeController.class);
assertThat(mappingInfo.getPathPatternsCondition().getPatterns())
.extracting(PathPattern::toString)
.containsOnly("/exchange/headers");
assertThat(mappingInfo.getMethodsCondition().getMethods()).containsOnly(RequestMethod.GET);
assertThat(mappingInfo.getParamsCondition().getExpressions()).isEmpty();
assertThat(mappingInfo.getHeadersCondition().getExpressions().stream().map(Object::toString))
.containsExactly("h1=hv1", "!h2");
}
private static RequestMappingHandlerMapping createMapping() {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setApplicationContext(new StaticWebApplicationContext());
@@ -543,6 +563,12 @@ class RequestMappingHandlerMappingTests {
@PostExchange(url = "/custom", contentType = "application/json", accept = "text/plain;charset=UTF-8")
public void customValuesExchange(){}
@HttpExchange(method="GET", url = "/headers",
headers = {"h1=hv1", "!h2", "Accept=application/ignored"})
public String customHeadersExchange() {
return "info";
}
}