Improvements to the way the content of RequestConditions is exposed.
RequestCondition types keep individual expression types (e.g. the discrete header or param expressions) package private. Although the implementation of these types should remain private, there is no reason not to provide access to the underlying expression data -- e.g. for creating a REST endpoint documentation tool, or if you want to know which of the "consumes"/"produces" media types are negated. This change ensures that all RequestCondition types have a public getter that makes available the basic expression data.
This commit is contained in:
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -54,6 +55,12 @@ public class ConsumesRequestConditionTests {
|
||||
assertNull(condition.getMatchingCondition(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getConsumableMediaTypesNegatedExpression() {
|
||||
ConsumesRequestCondition condition = new ConsumesRequestCondition("!application/xml");
|
||||
assertEquals(Collections.emptySet(), condition.getConsumableMediaTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesWildcardMatch() {
|
||||
ConsumesRequestCondition condition = new ConsumesRequestCondition("text/*");
|
||||
|
||||
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -30,11 +31,12 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.Pr
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ProducesRequestConditionTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void consumesMatch() {
|
||||
public void producesMatch() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -44,7 +46,7 @@ public class ProducesRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void negatedConsumesMatch() {
|
||||
public void negatedProducesMatch() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -54,7 +56,13 @@ public class ProducesRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesWildcardMatch() {
|
||||
public void getProducibleMediaTypesNegatedExpression() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("!application/xml");
|
||||
assertEquals(Collections.emptySet(), condition.getProducibleMediaTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void producesWildcardMatch() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("text/*");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -64,7 +72,7 @@ public class ProducesRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesMultipleMatch() {
|
||||
public void producesMultipleMatch() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("text/plain", "application/xml");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -74,7 +82,7 @@ public class ProducesRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesSingleNoMatch() {
|
||||
public void producesSingleNoMatch() {
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@@ -244,10 +252,10 @@ public class ProducesRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseConsumesAndHeaders() {
|
||||
String[] consumes = new String[] {"text/plain"};
|
||||
public void parseProducesAndHeaders() {
|
||||
String[] produces = new String[] {"text/plain"};
|
||||
String[] headers = new String[]{"foo=bar", "accept=application/xml,application/pdf"};
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition(consumes, headers);
|
||||
ProducesRequestCondition condition = new ProducesRequestCondition(produces, headers);
|
||||
|
||||
assertConditions(condition, "text/plain", "application/xml", "application/pdf");
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -213,6 +214,23 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
assertEquals("/1/2", request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void producibleMediaTypesAttribute() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/content");
|
||||
request.addHeader("Accept", "application/xml");
|
||||
this.mapping.getHandler(request);
|
||||
|
||||
assertEquals(Collections.singleton(MediaType.APPLICATION_XML),
|
||||
request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE));
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/content");
|
||||
request.addHeader("Accept", "application/json");
|
||||
this.mapping.getHandler(request);
|
||||
|
||||
assertNull("Negated expression should not be listed as a producible type",
|
||||
request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mappedInterceptors() throws Exception {
|
||||
String path = "/foo";
|
||||
@@ -261,6 +279,16 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
public String produces() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/content", produces="application/xml")
|
||||
public String xmlContent() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/content", produces="!application/xml")
|
||||
public String nonXmlContent() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping {
|
||||
|
||||
Reference in New Issue
Block a user