SEC-2221: Add MediaTypeRequestMatcher
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.security.web.util;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.accept.ContentNegotiationStrategy;
|
||||
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
||||
|
||||
/**
|
||||
* Verify how integrates with {@link HeaderContentNegotiationStrategy}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
public class MediaTypeRequestMatcherRequestHCNSTests {
|
||||
private MediaTypeRequestMatcher matcher;
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private ContentNegotiationStrategy negotiationStrategy;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
negotiationStrategy = new HeaderContentNegotiationStrategy();
|
||||
request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaAllMatches() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_XHTML_XML);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
// ignoreMediaTypeAll
|
||||
|
||||
@Test
|
||||
public void mediaAllIgnoreMediaTypeAll() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaAllAndTextHtmlIgnoreMediaTypeAll() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE + "," + MediaType.TEXT_HTML_VALUE);
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
// JavaDoc
|
||||
|
||||
@Test
|
||||
public void javadocJsonJson() {
|
||||
request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocAllJson() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocAllJsonIgnoreAll() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocJsonJsonIgnoreAll() {
|
||||
request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocJsonJsonUseEquals() {
|
||||
request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void javadocAllJsonUseEquals() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocApplicationAllJsonUseEquals() {
|
||||
request.addHeader("Accept", new MediaType("application","*"));
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void javadocAllJsonUseFalse() {
|
||||
request.addHeader("Accept", MediaType.ALL_VALUE);
|
||||
MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.security.web.util;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.accept.ContentNegotiationStrategy;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MediaTypeRequestMatcherTests {
|
||||
private MediaTypeRequestMatcher matcher;
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private ContentNegotiationStrategy negotiationStrategy;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void constructorNullCNSVarargs() {
|
||||
new MediaTypeRequestMatcher(null, MediaType.ALL);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void constructorNullCNSSet() {
|
||||
new MediaTypeRequestMatcher(null, Collections.singleton(MediaType.ALL));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void constructorNoVarargs() {
|
||||
new MediaTypeRequestMatcher(negotiationStrategy);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void constructorNullMediaTypes() {
|
||||
Collection<MediaType> mediaTypes = null;
|
||||
new MediaTypeRequestMatcher(negotiationStrategy, mediaTypes);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void constructorEmtpyMediaTypes() {
|
||||
new MediaTypeRequestMatcher(negotiationStrategy, Collections.<MediaType>emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void negotiationStrategyThrowsHMTNAE() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenThrow(new HttpMediaTypeNotAcceptableException("oops"));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.ALL);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaAllMatches() throws Exception {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.ALL));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_XHTML_XML);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleMediaType() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.TEXT_PLAIN,MediaType.APPLICATION_XHTML_XML,MediaType.TEXT_HTML));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_HTML);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_JSON);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveTextPlainMatchesTextAll() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, new MediaType("text","*"));
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveTextAllMatchesTextPlain() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(new MediaType("text","*")));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_PLAIN);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
// useEquals
|
||||
|
||||
@Test
|
||||
public void useEqualsResolveTextAllMatchesTextPlain() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(new MediaType("text","*")));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_PLAIN);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useEqualsResolveTextPlainMatchesTextAll() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, new MediaType("text","*"));
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useEqualsSame() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.TEXT_PLAIN));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_PLAIN);
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useEqualsWithCustomMediaType() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(new MediaType("text","unique")));
|
||||
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, new MediaType("text","unique"));
|
||||
matcher.setUseEquals(true);
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
|
||||
// ignoreMediaTypeAll
|
||||
|
||||
@Test
|
||||
public void mediaAllIgnoreMediaTypeAll() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.ALL));
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
|
||||
assertThat(matcher.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mediaAllAndTextHtmlIgnoreMediaTypeAll() throws HttpMediaTypeNotAcceptableException {
|
||||
when(negotiationStrategy.resolveMediaTypes(any(NativeWebRequest.class))).thenReturn(Arrays.asList(MediaType.ALL,MediaType.TEXT_HTML));
|
||||
matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
|
||||
matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL));
|
||||
|
||||
assertThat(matcher.matches(request)).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user