SPR-5267 Support for PUT and DELETE in spring:form tag
Any HTTP method passed to the spring-form:form tag other then GET or POST will automatically be defaulted to POST on the form tag and a hidden input field is created with the requested HTTP method. By default, the hidden field is named '_method', the name can be overridden using the methodParam attribute on the spring-form:form tag.
This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||
* @author Rob Harrop
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
* @author Scott Andrews
|
||||
*/
|
||||
public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||
|
||||
@@ -169,6 +170,100 @@ public class FormTagTests extends AbstractHtmlElementTagTests {
|
||||
assertEquals("<form id=\"command\" action=\"/my/form?foo=bar&stuff="><script>alert('XSS!')</script>\" method=\"post\">",
|
||||
getOutput());
|
||||
}
|
||||
|
||||
public void testGet() throws Exception {
|
||||
this.tag.setMethod("get");
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
this.tag.doFinally();
|
||||
|
||||
String output = getOutput();
|
||||
String formOutput = getFormTag(output);
|
||||
String inputOutput = getInputTag(output);
|
||||
|
||||
assertContainsAttribute(formOutput, "method", "get");
|
||||
assertEquals("", inputOutput);
|
||||
}
|
||||
|
||||
public void testPost() throws Exception {
|
||||
this.tag.setMethod("post");
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
this.tag.doFinally();
|
||||
|
||||
String output = getOutput();
|
||||
String formOutput = getFormTag(output);
|
||||
String inputOutput = getInputTag(output);
|
||||
|
||||
assertContainsAttribute(formOutput, "method", "post");
|
||||
assertEquals("", inputOutput);
|
||||
}
|
||||
|
||||
public void testPut() throws Exception {
|
||||
this.tag.setMethod("put");
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
this.tag.doFinally();
|
||||
|
||||
String output = getOutput();
|
||||
String formOutput = getFormTag(output);
|
||||
String inputOutput = getInputTag(output);
|
||||
|
||||
assertContainsAttribute(formOutput, "method", "post");
|
||||
assertContainsAttribute(inputOutput, "name", "_method");
|
||||
assertContainsAttribute(inputOutput, "value", "put");
|
||||
assertContainsAttribute(inputOutput, "type", "hidden");
|
||||
}
|
||||
|
||||
public void testDelete() throws Exception {
|
||||
this.tag.setMethod("delete");
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
this.tag.doFinally();
|
||||
|
||||
String output = getOutput();
|
||||
String formOutput = getFormTag(output);
|
||||
String inputOutput = getInputTag(output);
|
||||
|
||||
assertContainsAttribute(formOutput, "method", "post");
|
||||
assertContainsAttribute(inputOutput, "name", "_method");
|
||||
assertContainsAttribute(inputOutput, "value", "delete");
|
||||
assertContainsAttribute(inputOutput, "type", "hidden");
|
||||
}
|
||||
|
||||
public void testCustomMethodParameter() throws Exception {
|
||||
this.tag.setMethod("put");
|
||||
this.tag.setMethodParam("methodParameter");
|
||||
|
||||
this.tag.doStartTag();
|
||||
this.tag.doEndTag();
|
||||
this.tag.doFinally();
|
||||
|
||||
String output = getOutput();
|
||||
String formOutput = getFormTag(output);
|
||||
String inputOutput = getInputTag(output);
|
||||
|
||||
assertContainsAttribute(formOutput, "method", "post");
|
||||
assertContainsAttribute(inputOutput, "name", "methodParameter");
|
||||
assertContainsAttribute(inputOutput, "value", "put");
|
||||
assertContainsAttribute(inputOutput, "type", "hidden");
|
||||
}
|
||||
|
||||
private String getFormTag(String output) {
|
||||
int inputStart = output.indexOf("<", 1);
|
||||
int inputEnd = output.lastIndexOf(">", output.length() - 2);
|
||||
return output.substring(0, inputStart) + output.substring(inputEnd + 1);
|
||||
}
|
||||
|
||||
private String getInputTag(String output) {
|
||||
int inputStart = output.indexOf("<", 1);
|
||||
int inputEnd = output.lastIndexOf(">", output.length() - 2);
|
||||
return output.substring(inputStart, inputEnd + 1);
|
||||
}
|
||||
|
||||
|
||||
private static void assertFormTagOpened(String output) {
|
||||
|
||||
Reference in New Issue
Block a user