Fix issue with parsing media types

Invalid Content-Type or Accept header values previously resulted in the
IllegalArgumentException getting propagated. After this change such
errors are detected and generally treated as a "no match", which
may for example result in a 406 in the case of the Accept header.

Issue: SPR-9148
This commit is contained in:
Rossen Stoyanchev
2012-04-02 17:36:23 -04:00
parent 0b02933938
commit ca8b98e947
6 changed files with 107 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -44,7 +44,7 @@ public class ConsumesRequestConditionTests {
assertNotNull(condition.getMatchingCondition(request));
}
@Test
public void negatedConsumesMatch() {
ConsumesRequestCondition condition = new ConsumesRequestCondition("!text/plain");
@@ -60,7 +60,7 @@ public class ConsumesRequestConditionTests {
ConsumesRequestCondition condition = new ConsumesRequestCondition("!application/xml");
assertEquals(Collections.emptySet(), condition.getConsumableMediaTypes());
}
@Test
public void consumesWildcardMatch() {
ConsumesRequestCondition condition = new ConsumesRequestCondition("text/*");
@@ -91,6 +91,26 @@ public class ConsumesRequestConditionTests {
assertNull(condition.getMatchingCondition(request));
}
@Test
public void consumesParseError() {
ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("01");
assertNull(condition.getMatchingCondition(request));
}
@Test
public void consumesParseErrorWithNegation() {
ConsumesRequestCondition condition = new ConsumesRequestCondition("!text/plain");
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("01");
assertNull(condition.getMatchingCondition(request));
}
@Test
public void compareToSingle() {
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -128,7 +148,7 @@ public class ConsumesRequestConditionTests {
ConsumesRequestCondition result = condition1.combine(condition2);
assertEquals(condition2, result);
}
@Test
public void combineWithDefault() {
ConsumesRequestCondition condition1 = new ConsumesRequestCondition("text/plain");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -34,7 +34,7 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.Pr
* @author Rossen Stoyanchev
*/
public class ProducesRequestConditionTests {
@Test
public void producesMatch() {
ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
@@ -44,7 +44,7 @@ public class ProducesRequestConditionTests {
assertNotNull(condition.getMatchingCondition(request));
}
@Test
public void negatedProducesMatch() {
ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
@@ -60,7 +60,7 @@ public class ProducesRequestConditionTests {
ProducesRequestCondition condition = new ProducesRequestCondition("!application/xml");
assertEquals(Collections.emptySet(), condition.getProducibleMediaTypes());
}
@Test
public void producesWildcardMatch() {
ProducesRequestCondition condition = new ProducesRequestCondition("text/*");
@@ -91,6 +91,26 @@ public class ProducesRequestConditionTests {
assertNull(condition.getMatchingCondition(request));
}
@Test
public void producesParseError() {
ProducesRequestCondition condition = new ProducesRequestCondition("text/plain");
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept", "bogus");
assertNull(condition.getMatchingCondition(request));
}
@Test
public void producesParseErrorWithNegation() {
ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept", "bogus");
assertNull(condition.getMatchingCondition(request));
}
@Test
public void compareTo() {
ProducesRequestCondition html = new ProducesRequestCondition("text/html");
@@ -126,12 +146,12 @@ public class ProducesRequestConditionTests {
assertTrue(html.compareTo(xml, request) > 0);
assertTrue(xml.compareTo(html, request) < 0);
}
@Test
public void compareToWithSingleExpression() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("Accept", "text/plain");
ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain");
ProducesRequestCondition condition2 = new ProducesRequestCondition("text/*");
@@ -188,7 +208,7 @@ public class ProducesRequestConditionTests {
@Test
public void compareToMediaTypeAll() {
MockHttpServletRequest request = new MockHttpServletRequest();
ProducesRequestCondition condition1 = new ProducesRequestCondition();
ProducesRequestCondition condition2 = new ProducesRequestCondition("application/json");
@@ -202,7 +222,7 @@ public class ProducesRequestConditionTests {
assertTrue(condition1.compareTo(condition2, request) < 0);
assertTrue(condition2.compareTo(condition1, request) > 0);
request.addHeader("Accept", "*/*");
condition1 = new ProducesRequestCondition();
@@ -255,7 +275,7 @@ public class ProducesRequestConditionTests {
ProducesRequestCondition result = condition1.combine(condition2);
assertEquals(condition2, result);
}
@Test
public void combineWithDefault() {
ProducesRequestCondition condition1 = new ProducesRequestCondition("text/plain");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -56,7 +56,7 @@ import org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodPro
/**
* Test fixture with {@link HttpEntityMethodProcessor} and mock {@link HttpMessageConverter}.
*
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
@@ -106,7 +106,7 @@ public class HttpEntityMethodProcessorTests {
returnTypeResponseEntityProduces = new MethodParameter(getClass().getMethod("handle4"), -1);
mavContainer = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest();
servletResponse = new MockHttpServletResponse();
webRequest = new ServletWebRequest(servletRequest, servletResponse);
@@ -219,7 +219,7 @@ public class HttpEntityMethodProcessorTests {
fail("Expected exception");
}
@Test(expected = HttpMediaTypeNotAcceptableException.class)
public void handleReturnValueNotAcceptableProduces() throws Exception {
String body = "Foo";
@@ -238,6 +238,17 @@ public class HttpEntityMethodProcessorTests {
fail("Expected exception");
}
// SPR-9142
@Test(expected=HttpMediaTypeNotAcceptableException.class)
public void handleReturnValueNotAcceptableParseError() throws Exception {
ResponseEntity<String> returnValue = new ResponseEntity<String>("Body", HttpStatus.ACCEPTED);
servletRequest.addHeader("Accept", "01");
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
fail("Expected exception");
}
@Test
public void responseHeaderNoBody() throws Exception {
HttpHeaders headers = new HttpHeaders();
@@ -269,7 +280,7 @@ public class HttpEntityMethodProcessorTests {
assertEquals("headerValue", outputMessage.getValue().getHeaders().get("header").get(0));
verify(messageConverter);
}
public ResponseEntity<String> handle1(HttpEntity<String> httpEntity, ResponseEntity<String> responseEntity, int i) {
return responseEntity;
}