Merge branch '2.0.x'

pull/12016/merge
Brian Clozel 7 years ago
commit fe3de2816c

@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicate;
@ -184,11 +185,16 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
*/ */
protected RequestPredicate acceptsTextHtml() { protected RequestPredicate acceptsTextHtml() {
return (serverRequest) -> { return (serverRequest) -> {
try {
List<MediaType> acceptedMediaTypes = serverRequest.headers().accept(); List<MediaType> acceptedMediaTypes = serverRequest.headers().accept();
acceptedMediaTypes.remove(MediaType.ALL); acceptedMediaTypes.remove(MediaType.ALL);
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
return acceptedMediaTypes.stream() return acceptedMediaTypes.stream()
.anyMatch(MediaType.TEXT_HTML::isCompatibleWith); .anyMatch(MediaType.TEXT_HTML::isCompatibleWith);
}
catch (InvalidMediaTypeException ex) {
return false;
}
}; };
} }

@ -279,6 +279,16 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
}); });
} }
@Test
public void invalidAcceptMediaType() {
this.contextRunner.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.get().uri("/notfound").header("Accept", "v=3.0").exchange()
.expectStatus().isEqualTo(HttpStatus.NOT_FOUND);
});
}
@Configuration @Configuration
public static class Application { public static class Application {

Loading…
Cancel
Save