Author: mrdon Date: Thu Apr 19 22:25:44 2007 New Revision: 530654 URL: http://svn.apache.org/viewvc?view=rev&rev=530654 Log: Improve documentation, add more tests, fix put accepting creations WW-1475
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java?view=diff&rev=530654&r1=530653&r2=530654 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapper.java Thu Apr 19 22:25:44 2007 @@ -58,14 +58,14 @@ * <p> * The following URL's will invoke its methods: * </p> - * <ul> - * <li><code>GET: /movie => method="index"</code></li> + * <ul> + * <li><code>GET: /movie => method="index"</code></li> * <li><code>GET: /movie/Thrillers => method="view", id="Thrillers"</code></li> * <li><code>GET: /movie/Thrillers!edit => method="edit", id="Thrillers"</code></li> - * <li><code>GET: /movie/new => method="editNew"</code></li> - * <li><code>POST: /movie/Thrillers => method="create"</code></li> - * <li><code>PUT: /movie/Thrillers => method="update", id="Thrillers""</code></li> - * <li><code>DELETE: /movie/Thrillers => method="remove"</code></li> + * <li><code>GET: /movie/new => method="editNew"</code></li> + * <li><code>POST: /movie/ => method="create"</code></li> + * <li><code>PUT: /movie/Thrillers => method="update", id="Thrillers"</code></li> + * <li><code>DELETE: /movie/Thrillers => method="remove", id="Thrillers"</code></li> * </ul> * <p> * To simulate the HTTP methods PUT and DELETE, since they aren't supported by HTML, @@ -83,7 +83,7 @@ public class Restful2ActionMapper extends DefaultActionMapper { protected static final Log LOG = LogFactory.getLog(Restful2ActionMapper.class); - private static final String HTTP_METHOD_PARAM = "__http_method"; + public static final String HTTP_METHOD_PARAM = "__http_method"; /* * (non-Javadoc) @@ -116,10 +116,6 @@ // Creating a new entry on POST e.g. foo/ } else if (isPost(request)) { mapping.setMethod("create"); - - // Updating an item e.g. foo/1 - } else if (isPut(request)) { - mapping.setMethod("update"); } } else if (lastSlashPos > -1) { @@ -136,6 +132,10 @@ // Removing an item e.g. foo/1 } else if (isDelete(request)) { mapping.setMethod("remove"); + + // Updating an item e.g. foo/1 + } else if (isPut(request)) { + mapping.setMethod("update"); } } } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java?view=diff&rev=530654&r1=530653&r2=530654 ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/Restful2ActionMapperTest.java Thu Apr 19 22:25:44 2007 @@ -88,18 +88,94 @@ assertEquals("1", mapping.getParams().get("bar")); } - public void testPutCreate() throws Exception { + public void testPostCreate() throws Exception { req.setupGetRequestURI("/my/namespace/bar/1/foo/"); req.setupGetServletPath("/my/namespace/bar/1/foo/"); req.setupGetAttribute(null); req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); - req.setupGetMethod("PUT"); + req.setupGetMethod("POST"); ActionMapping mapping = mapper.getMapping(req, configManager); assertEquals("/my/namespace", mapping.getNamespace()); assertEquals("foo/", mapping.getName()); + assertEquals("create", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testPutUpdate() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("PUT"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("update", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testPutUpdateWithFakePut() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "put"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("POST"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); assertEquals("update", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testDeleteRemove() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("DELETE"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("remove", mapping.getMethod()); + assertEquals(1, mapping.getParams().size()); + assertEquals("1", mapping.getParams().get("bar")); + } + + public void testDeleteRemoveWithFakeDelete() throws Exception { + + mapper.setSlashesInActionNames("true"); + req.setupGetRequestURI("/my/namespace/bar/1/foo/2"); + req.setupGetServletPath("/my/namespace/bar/1/foo/2"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE"); + req.setupAddParameter(Restful2ActionMapper.HTTP_METHOD_PARAM, "DELETE"); + req.setupGetAttribute(null); + req.addExpectedGetAttributeName("javax.servlet.include.servlet_path"); + req.setupGetMethod("POST"); + + ActionMapping mapping = mapper.getMapping(req, configManager); + + assertEquals("/my/namespace", mapping.getNamespace()); + assertEquals("foo/2", mapping.getName()); + assertEquals("remove", mapping.getMethod()); assertEquals(1, mapping.getParams().size()); assertEquals("1", mapping.getParams().get("bar")); }