📄 webformtest.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebFormTest.java,v 1.33 2004/07/11 03:11:32 russgold Exp $** Copyright (c) 2000-2002, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import com.meterware.pseudoserver.PseudoServlet;import com.meterware.pseudoserver.WebResource;import java.net.HttpURLConnection;import java.io.IOException;import junit.framework.Test;import junit.framework.TestSuite;import org.xml.sax.SAXException;/** * A test of the web form functionality. **/public class WebFormTest extends HttpUnitTest { public static void main(String args[]) { junit.textui.TestRunner.run( suite() ); } public static Test suite() { return new TestSuite( WebFormTest.class ); } public WebFormTest( String name ) { super( name ); } public void setUp() throws Exception { super.setUp(); _wc = new WebConversation(); defineWebPage( "OneForm", "<h2>Login required</h2>" + "<form method=POST action = \"/servlet/Login\"><B>" + "Enter the name 'master': <Input type=TEXT Name=name></B>" + "<input type=\"checkbox\" name=first>Disabled" + "<input type=\"checkbox\" name=second checked>Enabled" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); } public void testSubmitFromForm() throws Exception { defineWebPage( "Form", "<form method=GET id=main action = 'tryMe'>" + "<Input type=text Name=name>" + "<input type=\"checkbox\" name=second checked>Enabled" + "</form>" ); defineResource( "/tryMe?name=master&second=on", "You made it!" ); WebResponse wr = _wc.getResponse( getHostPath() + "/Form.html" ); WebForm form = wr.getFormWithID( "main" ); form.setParameter( "name", "master" ); form.submit(); assertEquals( "Expected response", "You made it!", _wc.getCurrentPage().getText() ); } public void testAmbiguousSubmitFromForm() throws Exception { defineWebPage( "Form", "<form method=GET id=main action = 'tryMe'>" + "<Input type=text Name=name>" + "<input type=\"checkbox\" name=second checked>Enabled" + "<input type='submit' name='left'><input type='submit' name='right'>" + "</form>" ); defineResource( "/tryMe?name=master&second=on", "You made it!" ); WebResponse wr = _wc.getResponse( getHostPath() + "/Form.html" ); WebForm form = wr.getFormWithID( "main" ); form.setParameter( "name", "master" ); try { form.submit(); fail( "Should have rejected request as ambiguous" ); } catch (IllegalRequestParameterException e) { } WebResponse noButton = form.submitNoButton(); assertEquals( "Expected response", "You made it!", noButton.getText() ); } public void testSubmitFromButton() throws Exception { defineWebPage( "Form", "<form method=GET id=main action = 'tryMe'>" + "<Input type=text Name=name>" + "<input type=\"checkbox\" name=second checked>Enabled" + "<input type=submit name=save value=none>" + "<input type=submit name=save value=all>" + "</form>" ); defineResource( "/tryMe?name=master&second=on&save=all", "You made it!" ); WebResponse wr = _wc.getResponse( getHostPath() + "/Form.html" ); WebForm form = wr.getFormWithID( "main" ); form.setParameter( "name", "master" ); SubmitButton button = form.getSubmitButton( "save", "all" ); button.click(); assertEquals( "Expected response", "You made it!", _wc.getCurrentPage().getText() ); } public void testSubmitFromPositionalButton() throws Exception { defineResource( "ask?age=12&update=name&update.x=5&update.y=15", "You made it!", "text/plain" ); defineWebPage( "Default", "<form id='form' method=GET action = \"/ask\">" + "<Input type=text name=age value=12>" + "<Input type=image name=update value=name src=\"\">" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); SubmitButton button = page.getFormWithID( "form" ).getSubmitButton( "update" ); button.click( 5, 15 ); assertEquals( "Result of click", "You made it!", _wc.getCurrentPage().getText() ); } public void testFindNoForm() throws Exception { defineWebPage( "NoForms", "This has no forms but it does" + "have <a href=\"/other.html\">an active link</A>" + " and <a name=here>an anchor</a>" ); WebForm[] forms = _wc.getResponse( getHostPath() + "/NoForms.html" ).getForms(); assertNotNull( forms ); assertEquals( 0, forms.length ); } public void testFindOneForm() throws Exception { WebForm[] forms = _wc.getResponse( getHostPath() + "/OneForm.html" ).getForms(); assertNotNull( forms ); assertEquals( 1, forms.length ); } public void testFindFormByName() throws Exception { defineWebPage( "Default", "<form name=oneForm method=POST action = \"/servlet/Login\">" + "<Input name=\"secret\" type=\"hidden\" value=\"surprise\">" + "<br><Input name=typeless value=nothing>" + "<B>Enter the name 'master': <Input type=TEXT Name=name></B>" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); assertNull( "Found nonexistent form", page.getFormWithName( "nobody" ) ); assertNotNull( "Did not find named form", page.getFormWithName( "oneform" ) ); } public void testFindFormByID() throws Exception { defineWebPage( "Default", "<form id=oneForm method=POST action = \"/servlet/Login\">" + "<Input name=\"secret\" type=\"hidden\" value=\"surprise\">" + "<br><Input name=typeless value=nothing>" + "<B>Enter the name 'master': <Input type=TEXT Name=name></B>" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); assertNull( "Found nonexistent form", page.getFormWithID( "nobody" ) ); assertNotNull( "Did not find specified form", page.getFormWithID( "oneForm" ) ); } public void testFormParameters() throws Exception { defineWebPage( "AForm", "<h2>Login required</h2>" + "<form method=POST action = \"/servlet/Login\"><B>" + "Enter the name 'master': <textarea Name=name>Something</textarea></B>" + "<input type=\"checkbox\" name=first>Disabled" + "<input type=\"checkbox\" name=second checked>Enabled" + "<input type=textbox name=third value=something>" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); WebForm form = _wc.getResponse( getHostPath() + "/AForm.html" ).getForms()[0]; String[] parameters = form.getParameterNames(); assertNotNull( parameters ); assertMatchingSet( "form parameter names", new String[] { "first", "name", "second", "third" }, parameters ); assertNull( "First checkbox has a non-null value", form.getParameterValue( "first" ) ); assertEquals( "Second checkbox", "on", form.getParameterValue( "second" ) ); assertNull( "Found extraneous value for unknown parameter 'magic'", form.getParameterValue( "magic" ) ); assertTrue( "Did not find parameter 'first'", form.hasParameterNamed( "first" ) ); assertTrue( "Did not find parameter with prefix 'sec'", form.hasParameterStartingWithPrefix( "sec" ) ); assertTrue( "Did not find parameter with prefix 'nam'", form.hasParameterStartingWithPrefix( "nam" ) ); assertTrue( "Did not find parameter named 'third'", form.hasParameterNamed( "third" ) ); assertEquals( "Value of parameter with unknown type", "something", form.getParameterValue( "third" ) ); assertEquals( "Original text area value", "Something", form.getParameterValue( "name" ) ); form.setParameter( "name", "Something Else" ); assertEquals( "Changed text area value", "Something Else", form.getParameterValue( "name" ) ); form.reset(); assertEquals( "Reset text area value", "Something", form.getParameterValue( "name" ) ); } public void testFormRequest() throws Exception { WebForm form = _wc.getResponse( getHostPath() + "/OneForm.html" ).getForms()[0]; WebRequest request = form.getRequest(); request.setParameter( "name", "master" ); assertTrue( "Should be a post request", !(request instanceof GetMethodWebRequest) ); assertEquals( getHostPath() + "/servlet/Login", request.getURL().toExternalForm() ); } public void testHiddenParameters() throws Exception { defineWebPage( "Default", "<form method=POST action = \"/servlet/Login\">" + "<Input name=\"secret\" type=\"hidden\" value=\"surprise\">" + "<br><Input name=typeless value=nothing>" + "<B>Enter the name 'master': <Input type=TEXT Name=name></B>" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; assertEquals( 3, form.getParameterNames().length ); WebRequest request = form.getRequest(); assertEquals( "surprise", request.getParameter( "secret" ) ); assertEquals( "nothing", request.getParameter( "typeless" ) ); form.setParameter( "secret", "surprise" ); assertEquals( "surprise", request.getParameter( "secret" ) ); try { form.setParameter( "secret", "illegal" ); fail( "Should have rejected change to hidden parameter 'secret'" ); } catch (IllegalRequestParameterException e) { } assertEquals( "surprise", request.getParameter( "secret" ) ); } // XXX turn this back on when the parser handles it properly public void notestNullTextValues() throws Exception { defineWebPage( "Default", "<form method=POST action = \"/servlet/Login\">" + "<Input name=\"secret\" type=\"hidden\" value=>" + "<br><Input name=typeless value=>" + "<B>Enter the name 'master': <Input type=TEXT Name=name></B>" + "<br><Input type=submit value = \"Log in\">" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; assertEquals( 3, form.getParameterNames().length ); WebRequest request = form.getRequest(); assertEquals( "", request.getParameter( "secret" ) ); assertEquals( "", request.getParameter( "typeless" ) ); } public void testTableForm() throws Exception { defineWebPage( "Default", "<form method=POST action = \"/servlet/Login\">" + "<table summary=\"\"><tr><td>" +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -