Refine ContentNegotiationStrategy contract

Consistently return "*/*" if no media types were requested rather than
an empty list. Existing code has to check for both in any case to see
if nothing was requested.

Issue: SPR-16624
This commit is contained in:
Rossen Stoyanchev
2018-03-27 16:32:18 -04:00
parent 9a27bc9b3e
commit f3994467c4
11 changed files with 44 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -24,6 +24,7 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockServletContext;
@@ -32,7 +33,7 @@ import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Test fixture for {@link ContentNegotiationManagerFactoryBean} tests.
@@ -74,13 +75,13 @@ public class ContentNegotiationManagerFactoryBeanTests {
this.servletRequest.setRequestURI("/flower.foobarbaz");
assertEquals("Should ignore unknown extensions by default",
Collections.<MediaType>emptyList(), manager.resolveMediaTypes(this.webRequest));
ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
this.servletRequest.setRequestURI("/flower");
this.servletRequest.setParameter("format", "gif");
assertEquals("Should not resolve request parameters by default",
Collections.<MediaType>emptyList(), manager.resolveMediaTypes(this.webRequest));
ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
@@ -179,7 +180,7 @@ public class ContentNegotiationManagerFactoryBeanTests {
this.servletRequest.setRequestURI("/flower");
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
assertEquals(Collections.<MediaType>emptyList(), manager.resolveMediaTypes(this.webRequest));
assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
@@ -53,7 +53,7 @@ public class MappingContentNegotiationStrategyTests {
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
assertEquals(0, mediaTypes.size());
assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, mediaTypes);
}
@Test
@@ -63,7 +63,7 @@ public class MappingContentNegotiationStrategyTests {
List<MediaType> mediaTypes = strategy.resolveMediaTypes(null);
assertEquals(0, mediaTypes.size());
assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, mediaTypes);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -29,8 +29,7 @@ import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* A test fixture for PathExtensionContentNegotiationStrategy.
@@ -89,10 +88,12 @@ public class PathExtensionContentNegotiationStrategyTests {
this.servletRequest.setContextPath("/project-1.0.0.M3");
this.servletRequest.setRequestURI("/project-1.0.0.M3/");
assertTrue("Context path should be excluded", strategy.resolveMediaTypes(webRequest).isEmpty());
assertEquals("Context path should be excluded", ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST,
strategy.resolveMediaTypes(webRequest));
this.servletRequest.setRequestURI("/project-1.0.0.M3");
assertTrue("Context path should be excluded", strategy.resolveMediaTypes(webRequest).isEmpty());
assertEquals("Context path should be excluded", ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST,
strategy.resolveMediaTypes(webRequest));
}
// SPR-9390
@@ -118,7 +119,7 @@ public class PathExtensionContentNegotiationStrategyTests {
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy();
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
assertEquals(Collections.<MediaType>emptyList(), mediaTypes);
assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, mediaTypes);
}
@Test(expected = HttpMediaTypeNotAcceptableException.class)