Negated produces works with no Accept header present

Issue: SPR-14299
This commit is contained in:
Rossen Stoyanchev 2016-05-31 14:44:27 -04:00
parent 8343ce9e44
commit 27215b5061
2 changed files with 29 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.annotation.RequestMapping;
@ -193,7 +194,18 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
iterator.remove();
}
}
return (result.isEmpty()) ? null : new ProducesRequestCondition(result, this.contentNegotiationManager);
if (!result.isEmpty()) {
return new ProducesRequestCondition(result, this.contentNegotiationManager);
}
try {
if (getAcceptedMediaTypes(request).contains(MediaType.ALL)) {
return new ProducesRequestCondition();
}
}
catch (HttpMediaTypeException ex) {
// Ignore
}
return null;
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-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.
@ -24,9 +24,14 @@ import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.ProduceMediaTypeExpression;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit tests for {@link ProducesRequestCondition}.
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
@ -52,6 +57,15 @@ public class ProducesRequestConditionTests {
assertNull(condition.getMatchingCondition(request));
}
@Test
public void matchNegatedWithoutAcceptHeader() {
ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain");
MockHttpServletRequest request = new MockHttpServletRequest();
assertNotNull(condition.getMatchingCondition(request));
assertEquals(Collections.emptySet(), condition.getProducibleMediaTypes());
}
@Test
public void getProducibleMediaTypes() {
ProducesRequestCondition condition = new ProducesRequestCondition("!application/xml");