forked from spring-projects/spring-data-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAREST-863 - Adapt to changes in Accept header lookup in Spring 4.3.
Spring 4.3's HeaderContentNegotiationStrategy switched from looking up the Accept header via the method returning a single (potentially comma separated) one to the method returning multiple headers in the first place. We now added an override of HttpServletRequestWrapper.getHeaders(…) to out custom adapter defaulting the media type to make sure the defaulting is visible and thus the right handler methods are looked up. That previously missing caused the DelegatingHandlerMapping selecting the redirect to the HAL browser in case a request to the API root was sent without an accept header as the defaulting of the header to application/hal+json wasn't properly exposed anymore and the redirect to the browser — declaring a produces clause of text/html — was not causing any media type mismatch anymore.
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...a/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MediaType> 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<String> expected = Collections.list(servletRequest.getHeaders(HttpHeaders.ACCEPT)); | ||
|
||
assertThat(expected, hasSize(2)); | ||
assertThat(expected, hasItems(MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE)); | ||
} | ||
} |