📄 webformtest.java
字号:
"<B>Enter the name 'master': <Input type=TEXT Name=name></B>" + "</td><td><Input type=Radio name=sex value=male>Masculine" + "</td><td><Input type=Radio name=sex value=female checked>Feminine" + "</td><td><Input type=Radio name=sex value=neuter>Neither" + "<Input type=submit value = \"Log in\"></tr></table>" + "</form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; String[] parameterNames = form.getParameterNames(); assertEquals( "Number of parameters", 2, parameterNames.length ); assertMatchingSet( "parameter names", new String[] { "name","sex" }, parameterNames ); assertEquals( "Default name", "", form.getParameterValue( "name" ) ); assertEquals( "Default sex", "female", form.getParameterValue( "sex" ) ); form.setParameter( "sex", "neuter" ); assertEquals( "New value for sex", "neuter", form.getParameterValue( "sex" ) ); try { form.setParameter( "sex", "illegal" ); fail( "Should have rejected change to radio parameter 'sex'" ); } catch (IllegalRequestParameterException e) { } assertEquals( "Preserved value for sex", "neuter", form.getParameterValue( "sex" ) ); form.reset(); assertEquals( "Reverted value", "female", form.getParameterValue( "sex" ) ); } public void testSelect() throws Exception { defineWebPage( "Default", "<form method=POST action = \"/servlet/Login\">" + "<Select name=color><Option>blue<Option selected>red \n" + "<Option>green</select>" + "<TextArea name=\"text\">Sample text</TextArea>" + "<Input type=submit></form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; String[] parameterNames = form.getParameterNames(); assertEquals( "Number of parameters", 2, parameterNames.length ); assertEquals( "Default color", "red", form.getParameterValue( "color" ) ); assertEquals( "Default text", "Sample text", form.getParameterValue( "text" ) ); WebRequest request = form.getRequest(); assertEquals( "Submitted color", "red", request.getParameter( "color" ) ); assertEquals( "Submitted text", "Sample text", request.getParameter( "text" ) ); form.setParameter( "color", "green" ); assertEquals( "New select value", "green", form.getParameterValue( "color" ) ); try { form.setParameter( "color", new String[] { "green", "red" } ); fail( "Should have rejected set with multiple values" ); } catch (IllegalRequestParameterException e) { } form.setParameter( "color", "green" ); assertEquals( "Pre-reset color", "green", form.getParameterValue( "color" ) ); form.reset(); assertEquals( "Reverted color", "red", form.getParameterValue( "color" ) ); } public void testSizedSelect() throws Exception { defineWebPage( "Default", "<form method=POST action = '/servlet/Login'>" + "<Select name=poems><Option>limerick<Option>haiku</select>" + "<Select name=songs size=2><Option>aria<Option>folk</select>" + "<Input type=submit></form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; assertEquals( "Default poem", "limerick", form.getParameterValue( "poems" ) ); assertNull( "Default song should be null", form.getParameterValue( "songs" ) ); } public void testSingleSelectParameterOrdering() throws Exception { StringBuffer sb = new StringBuffer( "<form action='sendIt' id='theform'>" ); for (int i= 0; i < 4; i++) { sb.append( "<select name='enabled'><option value='true'>Enabled<option value='false' selected>Disabled</select>" ); } sb.append( "</form>" ); defineWebPage( "OnCommand", sb.toString() ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithID( "theform" ); form.setParameter( "enabled", new String[] { "true", "false", "false", "true" } ); WebRequest request = form.getRequest(); assertEquals( "request", getHostPath() + "/sendIt?enabled=true&enabled=false&enabled=false&enabled=true", request.getURL().toExternalForm() ); } public void testMultiSelect() throws Exception { defineWebPage( "Default", "<form method=GET action = \"/ask\">" + "<Select multiple size=4 name=colors>" + "<Option>blue<Option selected>red \n" + "<Option>green<Option value=\"pink\" selected>salmon</select>" + "<Input type=submit></form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; String[] parameterNames = form.getParameterNames(); assertEquals( "num parameters", 1, parameterNames.length ); assertEquals( "parameter name", "colors", parameterNames[0] ); assertTrue( "Found extraneous values for unknown parameter 'magic'", form.getParameterValues( "magic" ).length == 0 ); assertMatchingSet( "Select defaults", new String[] { "red", "pink" }, form.getParameterValues( "colors" ) ); assertMatchingSet( "Select options", new String[] { "blue", "red", "green", "salmon" }, form.getOptions( "colors" ) ); assertEquals( "Select values", new String[] { "blue", "red", "green", "pink" }, form.getOptionValues( "colors" ) ); WebRequest request = form.getRequest(); assertMatchingSet( "Request defaults", new String[] { "red", "pink" }, request.getParameterValues( "colors" ) ); assertEquals( "URL", getHostPath() + "/ask?colors=red&colors=pink", request.getURL().toExternalForm() ); form.setParameter( "colors", "green" ); assertEquals( "New select value", new String[] { "green" }, form.getParameterValues( "colors" ) ); form.setParameter( "colors", new String[] { "blue", "pink" } ); assertEquals( "New select value", new String[] { "blue", "pink" }, form.getParameterValues( "colors" ) ); try { form.setParameter( "colors", new String[] { "red", "colors" } ); fail( "Should have rejected set with bad values" ); } catch (IllegalRequestParameterException e) { } form.reset(); assertMatchingSet( "Reverted colors", new String[] { "red", "pink" }, form.getParameterValues( "colors" ) ); } public void testUnspecifiedDefaults() throws Exception { defineWebPage( "Default", "<form method=GET action = \"/ask\">" + "<Select name=colors><Option>blue<Option>red</Select>" + "<Select name=fish><Option value=red>snapper<Option value=pink>salmon</select>" + "<Select name=media multiple size=2><Option>TV<Option>Radio</select>" + "<Input type=submit></form>" ); WebResponse page = _wc.getResponse( getHostPath() + "/Default.html" ); WebForm form = page.getForms()[0]; assertEquals( "inferred color default", "blue", form.getParameterValue( "colors" ) ); assertEquals( "inferred fish default", "red", form.getParameterValue( "fish" ) ); assertMatchingSet( "inferred media default", new String[0], form.getParameterValues( "media" ) ); WebRequest request = form.getRequest(); assertEquals( "inferred color request", "blue", request.getParameter( "colors" ) ); assertEquals( "inferred fish request", "red", request.getParameter( "fish" ) ); assertMatchingSet( "inferred media default", new String[0], request.getParameterValues( "media" ) ); } public void testCheckboxControls() throws Exception { defineWebPage( "Default", "<form method=GET action = \"/ask\">" + "<Input type=checkbox name=ready value=yes checked>" + "<Input type=checkbox name=color value=red checked>" + "<Input type=checkbox name=color value=blue checked>" + "<Input type=checkbox name=gender value=male checked>" + "<Input type=checkbox name=gender value=female>" + "<Input type=submit></form>" ); WebResponse response = _wc.getResponse( getHostPath() + "/Default.html" ); assertNotNull( response.getForms() ); assertEquals( "Num forms in page", 1, response.getForms().length ); WebForm form = response.getForms()[0]; assertEquals( "ready state", "yes", form.getParameterValue( "ready" ) ); assertMatchingSet( "default genders allowed", new String[] { "male" }, form.getParameterValues( "gender" ) ); assertMatchingSet( "default colors", new String[] { "red", "blue" }, form.getParameterValues( "color" ) ); form.setParameter( "color", "red" ); assertMatchingSet( "modified colors", new String[] { "red" }, form.getParameterValues( "color" ) ); try { form.setParameter( "color", new String[] { "red", "purple" } ); fail( "Should have rejected set with bad values" ); } catch (IllegalRequestParameterException e) { } form.reset(); assertMatchingSet( "reverted colors", new String[] { "red", "blue" }, form.getParameterValues( "color" ) ); } public void testGetWithQueryString() throws Exception { defineResource( "QueryForm.html", "<html><head></head>" + "<form method=GET action=\"SayHello?speed=fast\">" + "<input type=text name=name><input type=submit></form></body></html>" ); defineResource( "SayHello?speed=fast&name=me", new PseudoServlet() { public WebResource getGetResponse() { WebResource result = new WebResource( "<html><body><table><tr><td>Hello, there" + "</td></tr></table></body></html>" ); return result; } } ); WebConversation wc = new WebConversation(); WebResponse formPage = wc.getResponse( getHostPath() + "/QueryForm.html" ); WebForm form = formPage.getForms()[0]; form.setParameter( "name", "me" ); WebRequest request = form.getRequest(); assertEquals( "Request URL", getHostPath() + "/SayHello?speed=fast&name=me", request.getURL().toExternalForm() ); WebResponse answer = wc.getResponse( request ); String[][] cells = answer.getTables()[0].asText(); assertEquals( "Message", "Hello, there", cells[0][0] ); } public void testPostWithQueryString() throws Exception { defineResource( "QueryForm.html", "<html><head></head>" + "<form method=POST action=\"SayHello?speed=fast\">" + "<input type=text name=name><input type=submit></form></body></html>" ); defineResource( "SayHello?speed=fast", new PseudoServlet() { public WebResource getPostResponse() { WebResource result = new WebResource( "<html><body><table><tr><td>Hello, there" + "</td></tr></table></body></html>" ); return result; } } ); WebConversation wc = new WebConversation(); WebResponse formPage = wc.getResponse( getHostPath() + "/QueryForm.html" ); WebForm form = formPage.getForms()[0]; WebRequest request = form.getRequest(); request.setParameter( "name", "Charlie" ); assertEquals( "Request URL", getHostPath() + "/SayHello?speed=fast", request.getURL().toExternalForm() ); WebResponse answer = wc.getResponse( request ); String[][] cells = answer.getTables()[0].asText(); assertEquals( "Message", "Hello, there", cells[0][0] ); } public void testPostWithEmbeddedSpace() throws Exception { String sessionID = "/ID=03.019c010101010001.00000001.a202000000000019. 0d09"; defineResource( "login", "redirectoring", HttpURLConnection.HTTP_MOVED_PERM ); super.addResourceHeader( "login", "Location: " + getHostPath() + sessionID + "/login" ); defineResource( sessionID + "/login", "<html><head></head>" + "<form method=POST action='SayHello'>" + "<input type=text name=name><input type=submit></form></body></html>" ); defineResource( sessionID + "/SayHello", new PseudoServlet() { public WebResource getPostResponse() { return new WebResource( "<html><body><table><tr><td>Hello, there</td></tr></table></body></html>" ); } } ); WebConversation wc = new WebConversation(); WebResponse formPage = wc.getResponse( getHostPath() + "/login" ); WebForm form = formPage.getForms()[0]; WebRequest request = form.getRequest(); request.setParameter( "name", "Charlie" ); WebResponse answer = wc.getResponse( request ); String[][] cells = answer.getTables()[0].asText(); assertEquals( "Message", "Hello, there", cells[0][0] ); } private WebConversation _wc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -