package org.jsoup.nodes;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
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></form>";
Document doc = Jsoup.parse(html);
FormElement form = (FormElement) doc.select("form").first();
List<Connection.KeyVal> data = form.formData();
assertEquals(4, 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());
}
@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());
}
}