Skip to content

Commit

Permalink
Remove quality parameter from selected media type
Browse files Browse the repository at this point in the history
Prior to this commit, WebFlux application would keep the quality
parameter from the "Accept" request header when selecting a media type
for the response. It would then echo it back to the client.

While strictly not wrong, this is unnecessary and can confuse HTTP
clients. This commit aligns WebFlux's behavior with Spring MVC.

Fixes gh-24239
  • Loading branch information
bclozel committed Jan 2, 2020
1 parent 8082b33 commit 75fd391
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Expand Up @@ -156,6 +156,7 @@ else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
}

if (selected != null) {
selected = selected.removeQualityValue();
if (logger.isDebugEnabled()) {
logger.debug("Using '" + selected + "' given " + acceptableTypes +
" and supported " + producibleTypes);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -51,7 +51,7 @@ public class HandlerResultHandlerTests {


@Test
public void usesContentTypeResolver() throws Exception {
void usesContentTypeResolver() {
TestResultHandler resultHandler = new TestResultHandler(new FixedContentTypeResolver(IMAGE_GIF));
List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
Expand All @@ -61,7 +61,7 @@ public void usesContentTypeResolver() throws Exception {
}

@Test
public void producibleMediaTypesRequestAttribute() throws Exception {
void producibleMediaTypesRequestAttribute() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(IMAGE_GIF));

Expand All @@ -72,7 +72,7 @@ public void producibleMediaTypesRequestAttribute() throws Exception {
}

@Test // SPR-9160
public void sortsByQuality() throws Exception {
void sortsByQuality() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
.header("Accept", "text/plain; q=0.5, application/json"));

Expand All @@ -83,7 +83,7 @@ public void sortsByQuality() throws Exception {
}

@Test
public void charsetFromAcceptHeader() throws Exception {
void charsetFromAcceptHeader() {
MediaType text8859 = MediaType.parseMediaType("text/plain;charset=ISO-8859-1");
MediaType textUtf8 = MediaType.parseMediaType("text/plain;charset=UTF-8");
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path").accept(text8859));
Expand All @@ -93,14 +93,25 @@ public void charsetFromAcceptHeader() throws Exception {
}

@Test // SPR-12894
public void noConcreteMediaType() throws Exception {
void noConcreteMediaType() {
List<MediaType> producible = Collections.singletonList(ALL);
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
MediaType actual = this.resultHandler.selectMediaType(exchange, () -> producible);

assertThat(actual).isEqualTo(APPLICATION_OCTET_STREAM);
}

@Test
void removeQualityParameter() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
.header("Accept", "text/plain; q=0.5"));

List<MediaType> mediaTypes = Arrays.asList(APPLICATION_JSON, TEXT_PLAIN);
MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes);

assertThat(actual).isEqualTo(TEXT_PLAIN);
}


@SuppressWarnings("WeakerAccess")
private static class TestResultHandler extends HandlerResultHandlerSupport {
Expand Down

0 comments on commit 75fd391

Please sign in to comment.