diff --git a/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java b/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java index bb5001e62..79a46dda8 100644 --- a/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java +++ b/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java @@ -80,7 +80,7 @@ public class HalBrowserIntegrationTests { mvc.perform(get(BASE_PATH).accept(MediaType.ALL)).// andExpect(status().isOk()).// - andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaTypes.HAL_JSON.toString()))); + andExpect(header().string(HttpHeaders.CONTENT_TYPE, startsWith(MediaTypes.HAL_JSON.toString()))); } /** diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java index 8dfee41e2..c80aa61cb 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java @@ -23,6 +23,7 @@ import java.net.URI; import java.security.Principal; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Enumeration; import java.util.List; import java.util.Locale; @@ -563,6 +564,7 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping { static class CustomAcceptHeaderHttpServletRequest extends HttpServletRequestWrapper { private final List acceptMediaTypes; + private final List acceptMediaTypeStrings; /** * Creates a new {@link CustomAcceptHeaderHttpServletRequest} for the given delegate {@link HttpServletRequest} and @@ -578,6 +580,14 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping { Assert.notEmpty(acceptMediaTypes, "MediaTypes must not be empty!"); this.acceptMediaTypes = acceptMediaTypes; + + List acceptMediaTypeStrings = new ArrayList(acceptMediaTypes.size()); + + for (MediaType mediaType : acceptMediaTypes) { + acceptMediaTypeStrings.add(mediaType.toString()); + } + + this.acceptMediaTypeStrings = acceptMediaTypeStrings; } /* @@ -593,5 +603,19 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping { return super.getHeader(name); } + + /* + * (non-Javadoc) + * @see javax.servlet.http.HttpServletRequestWrapper#getHeaders(java.lang.String) + */ + @Override + public Enumeration getHeaders(String name) { + + if (HttpHeaders.ACCEPT.equalsIgnoreCase(name) && acceptMediaTypes != null) { + return Collections.enumeration(acceptMediaTypeStrings); + } + + return super.getHeaders(name); + } } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java new file mode 100644 index 000000000..fec19ac52 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Test; +import org.springframework.data.rest.webmvc.BasePathAwareHandlerMapping.CustomAcceptHeaderHttpServletRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.util.StringUtils; + +/** + * Unit tests for {@link CustomAcceptHeaderHttpServletRequest}. + * + * @author Oliver Gierke + * @soundtrack Spring engineering team meeting @ SpringOne Platform 2016 + */ +public class CustomAcceptHeaderHttpServletRequestUnitTests { + + HttpServletRequest request = new MockHttpServletRequest(); + + /** + * @see DATAREST-863 + */ + @Test + public void returnsRegisterdHeadersOnAccessForMultipleOnes() { + + List mediaTypes = Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_ATOM_XML); + HttpServletRequest servletRequest = new CustomAcceptHeaderHttpServletRequest(request, mediaTypes); + + assertThat(servletRequest.getHeader(HttpHeaders.ACCEPT), + is(StringUtils.collectionToCommaDelimitedString(mediaTypes))); + + List expected = Collections.list(servletRequest.getHeaders(HttpHeaders.ACCEPT)); + + assertThat(expected, hasSize(2)); + assertThat(expected, hasItems(MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE)); + } +}