|
34 | 34 | import java.lang.annotation.RetentionPolicy;
|
35 | 35 | import java.lang.annotation.Target;
|
36 | 36 | import java.lang.reflect.Method;
|
| 37 | +import java.net.URI; |
| 38 | +import java.net.URISyntaxException; |
| 39 | +import java.nio.charset.Charset; |
37 | 40 | import java.security.Principal;
|
38 | 41 | import java.text.SimpleDateFormat;
|
39 | 42 | import java.util.ArrayList;
|
|
111 | 114 | import org.springframework.validation.Errors;
|
112 | 115 | import org.springframework.validation.FieldError;
|
113 | 116 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
| 117 | +import org.springframework.web.accept.ContentNegotiationManager; |
| 118 | +import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; |
114 | 119 | import org.springframework.web.bind.WebDataBinder;
|
115 | 120 | import org.springframework.web.bind.annotation.CookieValue;
|
116 | 121 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
142 | 147 | import org.springframework.web.servlet.support.RequestContextUtils;
|
143 | 148 | import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
144 | 149 |
|
| 150 | +import static org.junit.Assert.assertArrayEquals; |
| 151 | +import static org.junit.Assert.assertEquals; |
| 152 | +import static org.junit.Assert.assertFalse; |
| 153 | +import static org.junit.Assert.assertNotNull; |
| 154 | +import static org.junit.Assert.assertNull; |
| 155 | +import static org.junit.Assert.assertSame; |
| 156 | +import static org.junit.Assert.assertTrue; |
| 157 | +import static org.junit.Assert.fail; |
| 158 | + |
145 | 159 | /**
|
146 | 160 | * The origin of this test class is {@link ServletAnnotationControllerHandlerMethodTests}.
|
147 | 161 | *
|
@@ -1569,6 +1583,85 @@ public void initialize(GenericWebApplicationContext context) {
|
1569 | 1583 | assertEquals("count:3", response.getContentAsString());
|
1570 | 1584 | }
|
1571 | 1585 |
|
| 1586 | + @Test |
| 1587 | + public void responseBodyAsHtml() throws Exception { |
| 1588 | + initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() { |
| 1589 | + @Override |
| 1590 | + public void initialize(GenericWebApplicationContext wac) { |
| 1591 | + ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean(); |
| 1592 | + factoryBean.afterPropertiesSet(); |
| 1593 | + RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); |
| 1594 | + adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject()); |
| 1595 | + wac.registerBeanDefinition("handlerAdapter", adapterDef); |
| 1596 | + } |
| 1597 | + }, TextRestController.class); |
| 1598 | + |
| 1599 | + byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1")); |
| 1600 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html"); |
| 1601 | + request.setContent(content); |
| 1602 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 1603 | + |
| 1604 | + getServlet().service(request, response); |
| 1605 | + |
| 1606 | + assertEquals(200, response.getStatus()); |
| 1607 | + assertEquals("text/html", response.getContentType()); |
| 1608 | + assertEquals("attachment;filename=f.txt", response.getHeader("Content-Disposition")); |
| 1609 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1610 | + } |
| 1611 | + |
| 1612 | + @Test |
| 1613 | + public void responseBodyAsHtmlWithSuffixPresent() 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", "/a2.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 | + assertNull(response.getHeader("Content-Disposition")); |
| 1635 | + assertArrayEquals(content, response.getContentAsByteArray()); |
| 1636 | + } |
| 1637 | + |
| 1638 | + @Test |
| 1639 | + public void responseBodyAsHtmlWithProducesCondition() 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", "/a3.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 | + |
1572 | 1665 | /*
|
1573 | 1666 | * Controllers
|
1574 | 1667 | */
|
@@ -2979,7 +3072,31 @@ public void message(int param, Writer writer) throws IOException {
|
2979 | 3072 | }
|
2980 | 3073 | }
|
2981 | 3074 |
|
2982 |
| -// Test cases deleted from the original SevletAnnotationControllerTests: |
| 3075 | + @Controller |
| 3076 | + public static class TextRestController { |
| 3077 | + |
| 3078 | + @RequestMapping(value = "/a1", method = RequestMethod.GET) |
| 3079 | + @ResponseBody |
| 3080 | + public String a1(@RequestBody String body) { |
| 3081 | + return body; |
| 3082 | + } |
| 3083 | + |
| 3084 | + @RequestMapping(value = "/a2.html", method = RequestMethod.GET) |
| 3085 | + @ResponseBody |
| 3086 | + public String a2(@RequestBody String body) { |
| 3087 | + return body; |
| 3088 | + } |
| 3089 | + |
| 3090 | + @RequestMapping(value = "/a3", method = RequestMethod.GET, produces = "text/html") |
| 3091 | + @ResponseBody |
| 3092 | + public String a3(@RequestBody String body) throws IOException { |
| 3093 | + return body; |
| 3094 | + } |
| 3095 | + } |
| 3096 | + |
| 3097 | + |
| 3098 | + |
| 3099 | +// Test cases deleted from the original ServletAnnotationControllerTests: |
2983 | 3100 |
|
2984 | 3101 | // @Ignore("Controller interface => no method-level @RequestMapping annotation")
|
2985 | 3102 | // public void standardHandleMethod() throws Exception {
|
|
0 commit comments