Improve documentation, add more tests, fix put accepting creations

WW-1475


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@530654 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2007-04-20 05:25:44 +00:00
parent 0cabb1c2f5
commit 1dec646207
2 changed files with 89 additions and 13 deletions
@@ -58,14 +58,14 @@ import org.apache.commons.logging.LogFactory;
* <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 @@ import org.apache.commons.logging.LogFactory;
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 @@ public class Restful2ActionMapper extends DefaultActionMapper {
// 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 @@ public class Restful2ActionMapper extends DefaultActionMapper {
// 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");
}
}
}
@@ -88,19 +88,95 @@ public class Restful2ActionMapperTest extends StrutsTestCase {
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("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/", mapping.getName());
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"));
}
}