|
28 | 28 | import java.lang.reflect.Method;
|
29 | 29 | import java.net.URI;
|
30 | 30 | import java.net.URISyntaxException;
|
| 31 | +import java.nio.charset.Charset; |
31 | 32 | import java.security.Principal;
|
32 | 33 | import java.text.SimpleDateFormat;
|
33 | 34 | import java.util.ArrayList;
|
|
105 | 106 | import org.springframework.validation.Errors;
|
106 | 107 | import org.springframework.validation.FieldError;
|
107 | 108 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
| 109 | +import org.springframework.web.accept.ContentNegotiationManager; |
| 110 | +import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; |
108 | 111 | import org.springframework.web.bind.WebDataBinder;
|
109 | 112 | import org.springframework.web.bind.annotation.CookieValue;
|
110 | 113 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
137 | 140 | import org.springframework.web.servlet.support.RequestContextUtils;
|
138 | 141 | import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
139 | 142 |
|
140 |
| -import static org.junit.Assert.*; |
| 143 | +import static org.junit.Assert.assertArrayEquals; |
| 144 | +import static org.junit.Assert.assertEquals; |
| 145 | +import static org.junit.Assert.assertFalse; |
| 146 | +import static org.junit.Assert.assertNotNull; |
| 147 | +import static org.junit.Assert.assertNull; |
| 148 | +import static org.junit.Assert.assertSame; |
| 149 | +import static org.junit.Assert.assertTrue; |
| 150 | +import static org.junit.Assert.fail; |
141 | 151 |
|
142 | 152 | /**
|
143 | 153 | * The origin of this test class is {@link ServletAnnotationControllerHandlerMethodTests}.
|
@@ -1599,6 +1609,84 @@ public void responseAsHttpHeadersNoHeader() throws Exception {
|
1599 | 1609 | assertEquals("Expected an empty content", 0, response.getContentLength());
|
1600 | 1610 | }
|
1601 | 1611 |
|
| 1612 | + @Test |
| 1613 | + public void responseBodyAsHtml() throws Exception { |
| 1614 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1615 | + @Override |
| 1616 | + public void initialize(GenericWebApplicationContext wac) { |
| 1617 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1618 | + factoryBean.afterPropertiesSet(); |
| 1619 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1620 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1621 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1622 | + } |
| 1623 | + }, TextRestController.class); |
| 1624 | + |
| 1625 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1626 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html"); |
| 1627 | + request.setContent(content); |
| 1628 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1629 | + |
| 1630 | + getServlet().service(request, response); |
| 1631 | + |
| 1632 | + assertEquals(200, response.getStatus()); |
| 1633 | + assertEquals("text/html", response.getContentType()); |
| 1634 | + assertEquals("attachment;filename=f.txt", response.getHeader("Content-Disposition")); |
| 1635 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1636 | + } |
| 1637 | + |
| 1638 | + @Test |
| 1639 | + public void responseBodyAsHtmlWithSuffixPresent() throws Exception { |
| 1640 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1641 | + @Override |
| 1642 | + public void initialize(GenericWebApplicationContext wac) { |
| 1643 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1644 | + factoryBean.afterPropertiesSet(); |
| 1645 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1646 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1647 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1648 | + } |
| 1649 | + }, TextRestController.class); |
| 1650 | + |
| 1651 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1652 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html"); |
| 1653 | + request.setContent(content); |
| 1654 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1655 | + |
| 1656 | + getServlet().service(request, response); |
| 1657 | + |
| 1658 | + assertEquals(200, response.getStatus()); |
| 1659 | + assertEquals("text/html", response.getContentType()); |
| 1660 | + assertNull(response.getHeader("Content-Disposition")); |
| 1661 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1662 | + } |
| 1663 | + |
| 1664 | + @Test |
| 1665 | + public void responseBodyAsHtmlWithProducesCondition() throws Exception { |
| 1666 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1667 | + @Override |
| 1668 | + public void initialize(GenericWebApplicationContext wac) { |
| 1669 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1670 | + factoryBean.afterPropertiesSet(); |
| 1671 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1672 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1673 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1674 | + } |
| 1675 | + }, TextRestController.class); |
| 1676 | + |
| 1677 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1678 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html"); |
| 1679 | + request.setContent(content); |
| 1680 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1681 | + |
| 1682 | + getServlet().service(request, response); |
| 1683 | + |
| 1684 | + assertEquals(200, response.getStatus()); |
| 1685 | + assertEquals("text/html", response.getContentType()); |
| 1686 | + assertNull(response.getHeader("Content-Disposition")); |
| 1687 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1688 | + } |
| 1689 | + |
1602 | 1690 | /*
|
1603 | 1691 | * Controllers
|
1604 | 1692 | */
|
@@ -3037,6 +3125,26 @@ public HttpHeaders createNoHeader() throws URISyntaxException {
|
3037 | 3125 |
|
3038 | 3126 | }
|
3039 | 3127 |
|
| 3128 | + @RestController |
| 3129 | + public static class TextRestController { |
| 3130 | + |
| 3131 | + @RequestMapping(value = "/a1", method = RequestMethod.GET) |
| 3132 | + public String a1(@RequestBody String body) { |
| 3133 | + return body; |
| 3134 | + } |
| 3135 | + |
| 3136 | + @RequestMapping(value = "/a2.html", method = RequestMethod.GET) |
| 3137 | + public String a2(@RequestBody String body) { |
| 3138 | + return body; |
| 3139 | + } |
| 3140 | + |
| 3141 | + @RequestMapping(value = "/a3", method = RequestMethod.GET, produces = "text/html") |
| 3142 | + public String a3(@RequestBody String body) throws IOException { |
| 3143 | + return body; |
| 3144 | + } |
| 3145 | + } |
| 3146 | + |
| 3147 | + |
3040 | 3148 |
|
3041 | 3149 | // Test cases deleted from the original ServletAnnotationControllerTests:
|
3042 | 3150 |
|
|
0 commit comments