Created
September 27, 2023 20:26
-
-
Save 0xJchen/19e1d005533260690e450bd3c13aa2cc to your computer and use it in GitHub Desktop.
formelement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# human | |
package org.jsoup.nodes; | |
import org.jsoup.Connection; | |
import org.jsoup.Jsoup; | |
import org.junit.Test; | |
import java.util.List; | |
import static org.junit.Assert.*; | |
/** | |
* Tests for FormElement | |
* | |
* @author Jonathan Hedley | |
*/ | |
public class FormElementTest { | |
@Test public void hasAssociatedControls() { | |
//"button", "fieldset", "input", "keygen", "object", "output", "select", "textarea" | |
String html = "<form id=1><button id=1><fieldset id=2 /><input id=3><keygen id=4><object id=5><output id=6>" + | |
"<select id=7><option></select><textarea id=8><p id=9>"; | |
Document doc = Jsoup.parse(html); | |
FormElement form = (FormElement) doc.select("form").first(); | |
assertEquals(8, form.elements().size()); | |
} | |
@Test public void createsFormData() { | |
String html = "<form><input name='one' value='two'><select name='three'><option value='not'>" + | |
"<option value='four' selected><option value='five' selected><textarea name=six>seven</textarea>" + | |
"<input name='seven' type='radio' value='on' checked><input name='seven' type='radio' value='off'>" + | |
"<input name='eight' type='checkbox' checked><input name='nine' type='checkbox' value='unset'>" + | |
"<input name='ten' value='text' disabled>" + | |
"</form>"; | |
Document doc = Jsoup.parse(html); | |
FormElement form = (FormElement) doc.select("form").first(); | |
List<Connection.KeyVal> data = form.formData(); | |
assertEquals(6, data.size()); | |
assertEquals("one=two", data.get(0).toString()); | |
assertEquals("three=four", data.get(1).toString()); | |
assertEquals("three=five", data.get(2).toString()); | |
assertEquals("six=seven", data.get(3).toString()); | |
assertEquals("seven=on", data.get(4).toString()); // set | |
assertEquals("eight=on", data.get(5).toString()); // default | |
// nine should not appear, not checked checkbox | |
// ten should not appear, disabled | |
} | |
@Test public void createsSubmitableConnection() { | |
String html = "<form action='/search'><input name='q'></form>"; | |
Document doc = Jsoup.parse(html, "http://example.com/"); | |
doc.select("[name=q]").attr("value", "jsoup"); | |
FormElement form = ((FormElement) doc.select("form").first()); | |
Connection con = form.submit(); | |
assertEquals(Connection.Method.GET, con.request().method()); | |
assertEquals("http://example.com/search", con.request().url().toExternalForm()); | |
List<Connection.KeyVal> dataList = (List<Connection.KeyVal>) con.request().data(); | |
assertEquals("q=jsoup", dataList.get(0).toString()); | |
doc.select("form").attr("method", "post"); | |
Connection con2 = form.submit(); | |
assertEquals(Connection.Method.POST, con2.request().method()); | |
} | |
@Test public void actionWithNoValue() { | |
String html = "<form><input name='q'></form>"; | |
Document doc = Jsoup.parse(html, "http://example.com/"); | |
FormElement form = ((FormElement) doc.select("form").first()); | |
Connection con = form.submit(); | |
assertEquals("http://example.com/", con.request().url().toExternalForm()); | |
} | |
@Test public void actionWithNoBaseUri() { | |
String html = "<form><input name='q'></form>"; | |
Document doc = Jsoup.parse(html); | |
FormElement form = ((FormElement) doc.select("form").first()); | |
boolean threw = false; | |
try { | |
Connection con = form.submit(); | |
} catch (IllegalArgumentException e) { | |
threw = true; | |
assertEquals("Could not determine a form action URL for submit. Ensure you set a base URI when parsing.", | |
e.getMessage()); | |
} | |
assertTrue(threw); | |
} | |
@Test public void formsAddedAfterParseAreFormElements() { | |
Document doc = Jsoup.parse("<body />"); | |
doc.body().html("<form action='http://example.com/search'><input name='q' value='search'>"); | |
Element formEl = doc.select("form").first(); | |
assertTrue(formEl instanceof FormElement); | |
FormElement form = (FormElement) formEl; | |
assertEquals(1, form.elements().size()); | |
} | |
@Test public void controlsAddedAfterParseAreLinkedWithForms() { | |
Document doc = Jsoup.parse("<body />"); | |
doc.body().html("<form />"); | |
Element formEl = doc.select("form").first(); | |
formEl.append("<input name=foo value=bar>"); | |
assertTrue(formEl instanceof FormElement); | |
FormElement form = (FormElement) formEl; | |
assertEquals(1, form.elements().size()); | |
List<Connection.KeyVal> data = form.formData(); | |
assertEquals("foo=bar", data.get(0).toString()); | |
} | |
@Test public void usesOnForCheckboxValueIfNoValueSet() { | |
Document doc = Jsoup.parse("<form><input type=checkbox checked name=foo></form>"); | |
FormElement form = (FormElement) doc.select("form").first(); | |
List<Connection.KeyVal> data = form.formData(); | |
assertEquals("on", data.get(0).value()); | |
assertEquals("foo", data.get(0).key()); | |
} | |
@Test public void adoptedFormsRetainInputs() { | |
// test for https://github.com/jhy/jsoup/issues/249 | |
String html = "<html>\n" + | |
"<body> \n" + | |
" <table>\n" + | |
" <form action=\"/hello.php\" method=\"post\">\n" + | |
" <tr><td>User:</td><td> <input type=\"text\" name=\"user\" /></td></tr>\n" + | |
" <tr><td>Password:</td><td> <input type=\"password\" name=\"pass\" /></td></tr>\n" + | |
" <tr><td><input type=\"submit\" name=\"login\" value=\"login\" /></td></tr>\n" + | |
" </form>\n" + | |
" </table>\n" + | |
"</body>\n" + | |
"</html>"; | |
Document doc = Jsoup.parse(html); | |
FormElement form = (FormElement) doc.select("form").first(); | |
List<Connection.KeyVal> data = form.formData(); | |
assertEquals(3, data.size()); | |
assertEquals("user", data.get(0).key()); | |
assertEquals("pass", data.get(1).key()); | |
assertEquals("login", data.get(2).key()); | |
} | |
@Test public void removeFormElement() { | |
String html = "<html>\n" + | |
" <body> \n" + | |
" <form action=\"/hello.php\" method=\"post\">\n" + | |
" User:<input type=\"text\" name=\"user\" />\n" + | |
" Password:<input type=\"password\" name=\"pass\" />\n" + | |
" <input type=\"submit\" name=\"login\" value=\"login\" />\n" + | |
" </form>\n" + | |
" </body>\n" + | |
"</html> "; | |
Document doc = Jsoup.parse(html); | |
FormElement form = (FormElement) doc.selectFirst("form"); | |
Element pass = form.selectFirst("input[name=pass]"); | |
pass.remove(); | |
List<Connection.KeyVal> data = form.formData(); | |
assertEquals(2, data.size()); | |
assertEquals("user", data.get(0).key()); | |
assertEquals("login", data.get(1).key()); | |
assertEquals(null, doc.selectFirst("input[name=pass]")); | |
} | |
} | |
#evo | |
/* | |
* This file was automatically generated by EvoSuite | |
* Fri Sep 22 05:25:16 GMT 2023 | |
*/ | |
package org.jsoup.nodes; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
import static org.evosuite.runtime.EvoAssertions.*; | |
import java.util.List; | |
import org.evosuite.runtime.EvoRunner; | |
import org.evosuite.runtime.EvoRunnerParameters; | |
import org.jsoup.Connection; | |
import org.jsoup.nodes.Attributes; | |
import org.jsoup.nodes.CDataNode; | |
import org.jsoup.nodes.FormElement; | |
import org.jsoup.nodes.PseudoTextElement; | |
import org.jsoup.parser.ParseSettings; | |
import org.jsoup.parser.Tag; | |
import org.jsoup.select.Elements; | |
import org.junit.runner.RunWith; | |
@RunWith(EvoRunner.class) @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true, useJEE = true) | |
public class FormElement_ESTest extends FormElement_ESTest_scaffolding { | |
@Test(timeout = 4000) | |
public void test0() throws Throwable { | |
Tag tag0 = Tag.valueOf("select"); | |
FormElement formElement0 = new FormElement(tag0, "select", (Attributes) null); | |
PseudoTextElement pseudoTextElement0 = new PseudoTextElement(tag0, "select", (Attributes) null); | |
FormElement formElement1 = formElement0.addElement(pseudoTextElement0); | |
List<Connection.KeyVal> list0 = formElement1.formData(); | |
assertEquals(0, list0.size()); | |
} | |
@Test(timeout = 4000) | |
public void test1() throws Throwable { | |
Tag tag0 = Tag.valueOf("pmYw"); | |
FormElement formElement0 = new FormElement(tag0, "pmYw", (Attributes) null); | |
formElement0.attr("method", "POST"); | |
// Undeclared exception! | |
try { | |
formElement0.submit(); | |
fail("Expecting exception: IllegalArgumentException"); | |
} catch(IllegalArgumentException e) { | |
// | |
// Malformed URL: pmYw | |
// | |
verifyException("org.jsoup.helper.HttpConnection", e); | |
} | |
} | |
@Test(timeout = 4000) | |
public void test2() throws Throwable { | |
Tag tag0 = Tag.valueOf("gfe(vbC3@!)2Xgu"); | |
Attributes attributes0 = new Attributes(); | |
Attributes attributes1 = attributes0.put("action", ""); | |
FormElement formElement0 = new FormElement(tag0, "POST", attributes1); | |
// Undeclared exception! | |
try { | |
formElement0.submit(); | |
fail("Expecting exception: IllegalArgumentException"); | |
} catch(IllegalArgumentException e) { | |
// | |
// Could not determine a form action URL for submit. Ensure you set a base URI when parsing. | |
// | |
verifyException("org.jsoup.helper.Validate", e); | |
} | |
} | |
@Test(timeout = 4000) | |
public void test3() throws Throwable { | |
Tag tag0 = Tag.valueOf("pmYw"); | |
FormElement formElement0 = new FormElement(tag0, "pmYw", (Attributes) null); | |
// Undeclared exception! | |
try { | |
formElement0.submit(); | |
fail("Expecting exception: IllegalArgumentException"); | |
} catch(IllegalArgumentException e) { | |
// | |
// Malformed URL: pmYw | |
// | |
verifyException("org.jsoup.helper.HttpConnection", e); | |
} | |
} | |
@Test(timeout = 4000) | |
public void test4() throws Throwable { | |
ParseSettings parseSettings0 = new ParseSettings(true, true); | |
Tag tag0 = Tag.valueOf("h(n<rT|*aYX>n@[", parseSettings0); | |
CDataNode cDataNode0 = new CDataNode("La"); | |
Attributes attributes0 = cDataNode0.attributes(); | |
FormElement formElement0 = new FormElement(tag0, "La", attributes0); | |
Elements elements0 = formElement0.elements(); | |
assertTrue(elements0.isEmpty()); | |
} | |
@Test(timeout = 4000) | |
public void test5() throws Throwable { | |
Tag tag0 = Tag.valueOf(":%s(%dn%+d)"); | |
Attributes attributes0 = new Attributes(); | |
FormElement formElement0 = new FormElement(tag0, "s*cv", attributes0); | |
FormElement formElement1 = formElement0.addElement(formElement0); | |
List<Connection.KeyVal> list0 = formElement1.formData(); | |
assertTrue(list0.isEmpty()); | |
} | |
@Test(timeout = 4000) | |
public void test6() throws Throwable { | |
ParseSettings parseSettings0 = ParseSettings.htmlDefault; | |
Tag tag0 = Tag.valueOf("yxQj0}|>H- EWeS-&&", parseSettings0); | |
CDataNode cDataNode0 = new CDataNode("yxQj0}|>H- EWeS-&&"); | |
Attributes attributes0 = cDataNode0.attributes(); | |
FormElement formElement0 = new FormElement(tag0, "yxQj0}|>H- EWeS-&&", attributes0); | |
// Undeclared exception! | |
try { | |
formElement0.removeChild(cDataNode0); | |
fail("Expecting exception: IllegalArgumentException"); | |
} catch(IllegalArgumentException e) { | |
// | |
// Must be true | |
// | |
verifyException("org.jsoup.helper.Validate", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment