Support "Accept-Patch" for OPTIONS requests
This commit introduces support in both servlet and webflux for the "Accept-Patch" header in OPTIONS requests, as defined in section 3.1 of RFC 5789. See gh-26759
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -44,6 +45,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.reactive.BindingContext;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
@@ -192,10 +194,12 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
List<HttpMethod> allMethodExceptTrace = new ArrayList<>(Arrays.asList(HttpMethod.values()));
|
||||
allMethodExceptTrace.remove(HttpMethod.TRACE);
|
||||
|
||||
testHttpOptions("/foo", EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS));
|
||||
testHttpOptions("/person/1", EnumSet.of(HttpMethod.PUT, HttpMethod.OPTIONS));
|
||||
testHttpOptions("/persons", EnumSet.copyOf(allMethodExceptTrace));
|
||||
testHttpOptions("/something", EnumSet.of(HttpMethod.PUT, HttpMethod.POST));
|
||||
testHttpOptions("/foo", EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), null);
|
||||
testHttpOptions("/person/1", EnumSet.of(HttpMethod.PUT, HttpMethod.OPTIONS), null);
|
||||
testHttpOptions("/persons", EnumSet.copyOf(allMethodExceptTrace), null);
|
||||
testHttpOptions("/something", EnumSet.of(HttpMethod.PUT, HttpMethod.POST), null);
|
||||
testHttpOptions("/qux", EnumSet.of(HttpMethod.PATCH,HttpMethod.GET,HttpMethod.HEAD,HttpMethod.OPTIONS),
|
||||
new MediaType("foo", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -332,7 +336,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
assertError(mono, UnsupportedMediaTypeStatusException.class, ex -> assertThat(ex.getSupportedMediaTypes()).as("Invalid supported consumable media types").isEqualTo(Collections.singletonList(new MediaType("application", "xml"))));
|
||||
}
|
||||
|
||||
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) {
|
||||
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods, @Nullable MediaType acceptPatch) {
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options(requestURI));
|
||||
HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
|
||||
|
||||
@@ -346,7 +350,13 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
Object value = result.getReturnValue();
|
||||
assertThat(value).isNotNull();
|
||||
assertThat(value.getClass()).isEqualTo(HttpHeaders.class);
|
||||
assertThat(((HttpHeaders) value).getAllow()).isEqualTo(allowedMethods);
|
||||
|
||||
HttpHeaders headers = (HttpHeaders) value;
|
||||
assertThat(headers.getAllow()).hasSameElementsAs(allowedMethods);
|
||||
|
||||
if (acceptPatch != null && headers.getAllow().contains(HttpMethod.PATCH) ) {
|
||||
assertThat(headers.getAcceptPatch()).containsExactly(acceptPatch);
|
||||
}
|
||||
}
|
||||
|
||||
private void testMediaTypeNotAcceptable(String url) {
|
||||
@@ -430,6 +440,16 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
return headers;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/qux", method = RequestMethod.GET, produces = "application/xml")
|
||||
public String getBaz() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/qux", method = RequestMethod.PATCH, consumes = "foo/bar")
|
||||
public void patchBaz(String value) {
|
||||
}
|
||||
|
||||
|
||||
public void dummy() { }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user