|
16 | 16 |
|
17 | 17 | package org.springframework.web.servlet.mvc.method.annotation; |
18 | 18 |
|
19 | | -import static org.junit.Assert.*; |
20 | | - |
21 | 19 | import java.beans.PropertyEditorSupport; |
22 | 20 | import java.io.IOException; |
23 | 21 | import java.io.Serializable; |
|
30 | 28 | import java.lang.reflect.Method; |
31 | 29 | import java.net.URI; |
32 | 30 | import java.net.URISyntaxException; |
| 31 | +import java.nio.charset.Charset; |
33 | 32 | import java.security.Principal; |
34 | 33 | import java.text.SimpleDateFormat; |
35 | 34 | import java.util.ArrayList; |
|
44 | 43 | import java.util.Locale; |
45 | 44 | import java.util.Map; |
46 | 45 | import java.util.Set; |
47 | | - |
48 | 46 | import javax.servlet.ServletConfig; |
49 | 47 | import javax.servlet.ServletContext; |
50 | 48 | import javax.servlet.ServletException; |
|
108 | 106 | import org.springframework.validation.Errors; |
109 | 107 | import org.springframework.validation.FieldError; |
110 | 108 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| 109 | +import org.springframework.web.accept.ContentNegotiationManager; |
| 110 | +import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; |
111 | 111 | import org.springframework.web.bind.WebDataBinder; |
112 | 112 | import org.springframework.web.bind.annotation.CookieValue; |
113 | 113 | import org.springframework.web.bind.annotation.ExceptionHandler; |
|
141 | 141 | import org.springframework.web.servlet.support.RequestContextUtils; |
142 | 142 | import org.springframework.web.servlet.view.InternalResourceViewResolver; |
143 | 143 |
|
| 144 | +import static org.junit.Assert.assertArrayEquals; |
| 145 | +import static org.junit.Assert.assertEquals; |
| 146 | +import static org.junit.Assert.assertFalse; |
| 147 | +import static org.junit.Assert.assertNotNull; |
| 148 | +import static org.junit.Assert.assertNull; |
| 149 | +import static org.junit.Assert.assertSame; |
| 150 | +import static org.junit.Assert.assertTrue; |
| 151 | +import static org.junit.Assert.fail; |
| 152 | + |
144 | 153 | /** |
145 | 154 | * The origin of this test class is {@link ServletAnnotationControllerHandlerMethodTests}. |
146 | 155 | * |
@@ -1624,6 +1633,84 @@ public void responseAsHttpHeadersNoHeader() throws Exception { |
1624 | 1633 | assertEquals("Expected an empty content", 0, response.getContentLength()); |
1625 | 1634 | } |
1626 | 1635 |
|
| 1636 | + @Test |
| 1637 | + public void responseBodyAsHtml() throws Exception { |
| 1638 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1639 | + @Override |
| 1640 | + public void initialize(GenericWebApplicationContext wac) { |
| 1641 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1642 | + factoryBean.afterPropertiesSet(); |
| 1643 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1644 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1645 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1646 | + } |
| 1647 | + }, TextRestController.class); |
| 1648 | + |
| 1649 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1650 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html"); |
| 1651 | + request.setContent(content); |
| 1652 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1653 | + |
| 1654 | + getServlet().service(request, response); |
| 1655 | + |
| 1656 | + assertEquals(200, response.getStatus()); |
| 1657 | + assertEquals("text/html", response.getContentType()); |
| 1658 | + assertEquals("attachment;filename=f.txt", response.getHeader("Content-Disposition")); |
| 1659 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1660 | + } |
| 1661 | + |
| 1662 | + @Test |
| 1663 | + public void responseBodyAsHtmlWithSuffixPresent() throws Exception { |
| 1664 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1665 | + @Override |
| 1666 | + public void initialize(GenericWebApplicationContext wac) { |
| 1667 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1668 | + factoryBean.afterPropertiesSet(); |
| 1669 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1670 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1671 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1672 | + } |
| 1673 | + }, TextRestController.class); |
| 1674 | + |
| 1675 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1676 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html"); |
| 1677 | + request.setContent(content); |
| 1678 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1679 | + |
| 1680 | + getServlet().service(request, response); |
| 1681 | + |
| 1682 | + assertEquals(200, response.getStatus()); |
| 1683 | + assertEquals("text/html", response.getContentType()); |
| 1684 | + assertNull(response.getHeader("Content-Disposition")); |
| 1685 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1686 | + } |
| 1687 | + |
| 1688 | + @Test |
| 1689 | + public void responseBodyAsHtmlWithProducesCondition() throws Exception { |
| 1690 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1691 | + @Override |
| 1692 | + public void initialize(GenericWebApplicationContext wac) { |
| 1693 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1694 | + factoryBean.afterPropertiesSet(); |
| 1695 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1696 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1697 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1698 | + } |
| 1699 | + }, TextRestController.class); |
| 1700 | + |
| 1701 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1702 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html"); |
| 1703 | + request.setContent(content); |
| 1704 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1705 | + |
| 1706 | + getServlet().service(request, response); |
| 1707 | + |
| 1708 | + assertEquals(200, response.getStatus()); |
| 1709 | + assertEquals("text/html", response.getContentType()); |
| 1710 | + assertNull(response.getHeader("Content-Disposition")); |
| 1711 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1712 | + } |
| 1713 | + |
1627 | 1714 | /* |
1628 | 1715 | * Controllers |
1629 | 1716 | */ |
@@ -3083,6 +3170,26 @@ public HttpHeaders createNoHeader() throws URISyntaxException { |
3083 | 3170 |
|
3084 | 3171 | } |
3085 | 3172 |
|
| 3173 | + @RestController |
| 3174 | + public static class TextRestController { |
| 3175 | + |
| 3176 | + @RequestMapping(path = "/a1", method = RequestMethod.GET) |
| 3177 | + public String a1(@RequestBody String body) { |
| 3178 | + return body; |
| 3179 | + } |
| 3180 | + |
| 3181 | + @RequestMapping(path = "/a2.html", method = RequestMethod.GET) |
| 3182 | + public String a2(@RequestBody String body) { |
| 3183 | + return body; |
| 3184 | + } |
| 3185 | + |
| 3186 | + @RequestMapping(path = "/a3", method = RequestMethod.GET, produces = "text/html") |
| 3187 | + public String a3(@RequestBody String body) throws IOException { |
| 3188 | + return body; |
| 3189 | + } |
| 3190 | + } |
| 3191 | + |
| 3192 | + |
3086 | 3193 |
|
3087 | 3194 | // Test cases deleted from the original ServletAnnotationControllerTests: |
3088 | 3195 |
|
|
0 commit comments