/* * Copyright (c) 2002-2008 Gargoyle Software Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gargoylesoftware.htmlunit.html; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Writer; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.Servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.NameValuePair; import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.mortbay.jetty.Server; import org.w3c.dom.NodeList; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.CollectingAlertHandler; import com.gargoylesoftware.htmlunit.ElementNotFoundException; import com.gargoylesoftware.htmlunit.HttpMethod; import com.gargoylesoftware.htmlunit.HttpWebConnectionTest; import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler; import com.gargoylesoftware.htmlunit.IncorrectnessListener; import com.gargoylesoftware.htmlunit.MockWebConnection; import com.gargoylesoftware.htmlunit.OnbeforeunloadHandler; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.StringWebResponse; import com.gargoylesoftware.htmlunit.TextUtil; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebRequestSettings; import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.WebTestCase; import com.gargoylesoftware.htmlunit.html.HtmlElementTest.HtmlAttributeChangeListenerTestImpl; /** * Tests for {@link HtmlPage}. * * @version $Revision$ * @author Mike Bowler * @author Noboru Sinohara * @author David K. Taylor * @author Andreas Hangler * @author Christian Sell * @author Marc Guillemot * @author Ahmed Ashour */ public class HtmlPageTest extends WebTestCase { private Server server_; /** * @exception Exception If the test fails */ @Test public void testConstructor() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "
\n" + "\n" + "\n" + "\n" + "\n" + "
\n" + ""; final HtmlPage page = loadPage(htmlContent); assertEquals("foo", page.getTitleText()); } /** * @throws Exception if the test fails */ @Test public void testGetInputByName() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "
\n" + "\n" + "\n" + "\n" + "\n" + "
\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlForm form = page.getHtmlElementById("form1"); final HtmlInput input = form.getInputByName("textInput1"); Assert.assertEquals("name", "textInput1", input.getNameAttribute()); Assert.assertEquals("value", "textInput1", input.getValueAttribute()); Assert.assertEquals("type", "text", input.getTypeAttribute()); } /** * @throws Exception if the test fails */ @Test public void testFormSubmit() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "
\n" + "\n" + "\n" + "\n" + "\n" + "
\n" + ""; final HtmlPage page = loadPage(htmlContent); final MockWebConnection webConnection = getMockConnection(page); final HtmlForm form = page.getHtmlElementById("form1"); final HtmlInput textInput = form.getInputByName("textInput1"); textInput.setValueAttribute("foo"); final HtmlSubmitInput button = form.getInputByName("submitInput1"); final HtmlPage secondPage = (HtmlPage) button.click(); final List expectedParameters = new ArrayList(); expectedParameters.add(new NameValuePair("textInput1", "foo")); expectedParameters.add(new NameValuePair("textInput2", "textInput2")); expectedParameters.add(new NameValuePair("hidden1", "hidden1")); expectedParameters.add(new NameValuePair("submitInput1", "push me")); final URL expectedUrl = new URL("http://www.gargoylesoftware.com/formSubmit"); final URL actualUrl = secondPage.getWebResponse().getRequestUrl(); assertEquals("url", expectedUrl, actualUrl); Assert.assertSame("method", HttpMethod.POST, webConnection.getLastMethod()); Assert.assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); assertNotNull(secondPage); } /** * Tests getHtmlElement() for all elements that can be loaded. * @throws Exception if the test fails */ @Test public void testGetHtmlElement() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "
\n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " foo.com\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
HeaderData
\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlForm form = page.getHtmlElementById("form1"); assertSame("form1", form, page.getHtmlElementById("form1")); //huh?? final HtmlInput input = form.getInputByName("textInput1"); assertSame("input1", input, form.getInputByName("textInput1")); //?? final HtmlButton button = form.getButtonByName("button1"); assertSame("button1", button, form.getButtonByName("button1")); final HtmlSelect select = form.getSelectsByName("select1").get(0); assertSame("select1", select, form.getSelectsByName("select1").get(0)); final HtmlOption option = select.getOptionByValue("option1"); assertSame("option1", option, select.getOptionByValue("option1")); final HtmlTable table = page.getHtmlElementById("table1"); assertSame("table1", table, page.getHtmlElementById("table1")); final HtmlAnchor anchor = page.getAnchorByName("anchor1"); assertSame("anchor1", anchor, page.getAnchorByName("anchor1")); assertSame("anchor3", anchor, page.getAnchorByHref("http://www.foo.com")); assertSame("anchor4", anchor, page.getFirstAnchorByText("foo.com")); final HtmlTableRow tableRow = table.getRow(0); assertSame("tableRow1", tableRow, table.getRow(0)); final HtmlTableHeaderCell tableHeaderCell = (HtmlTableHeaderCell) tableRow.getCell(0); assertSame("tableHeaderCell1", tableHeaderCell, tableRow.getCell(0)); assertSame("tableHeaderCell2", tableHeaderCell, page.getHtmlElementById("header1")); final HtmlTableDataCell tableDataCell = (HtmlTableDataCell) tableRow.getCell(1); assertSame("tableDataCell1", tableDataCell, tableRow.getCell(1)); assertSame("tableDataCell2", tableDataCell, page.getHtmlElementById("data1")); final HtmlTextArea textArea = form.getTextAreaByName("textArea1"); assertSame("textArea1", textArea, form.getTextAreaByName("textArea1")); } /** * @throws Exception if the test fails */ @Test public void testGetTabbableElements_None() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "
foo
\n" + ""; final HtmlPage page = loadPage(htmlContent); assertEquals(Collections.EMPTY_LIST, page.getTabbableElements()); } /** * @throws Exception if the test fails */ @Test public void testGetTabbableElements_OneEnabled_OneDisabled() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "

hello world

\n" + "\n" + "\n" + "
"; final HtmlPage page = loadPage(htmlContent); final List expectedElements = new ArrayList(); expectedElements.add(page.getHtmlElementById("bar")); assertEquals(expectedElements, page.getTabbableElements()); } /** * @throws Exception if the test fails */ @Test public void testGetTabbableElements() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "foo\n" + "foo\n" + "
\n" + "foo\n" + "foo\n" + "foo\n" + "
\n" + "foo\n" + "foo\n" + "foo\n" + "

hello world

\n" + "\n" + "\n" + "
"; final HtmlPage page = loadPage(htmlContent); final List expectedElements = Arrays.asList(new HtmlElement[] {page.getHtmlElementById("a"), page.getHtmlElementById("g"), page.getHtmlElementById("d"), page.getHtmlElementById("c"), page.getHtmlElementById("f"), page.getHtmlElementById("e"), page.getHtmlElementById("b"), page.getHtmlElementById("bar")}); assertEquals(expectedElements, page.getTabbableElements()); final String[] expectedIds = {"a", "g", "d", "c", "f", "e", "b", "bar"}; assertEquals(expectedIds, page.getTabbableElementIds()); } /** * @throws Exception if the test fails */ @Test public void testGetHtmlElementByAccessKey() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "foo\n" + "foo\n" + "
\n" + "foo\n" + "
\n" + "

hello world

\n" + "\n" + "\n" + "
"; final HtmlPage page = loadPage(htmlContent); assertEquals(page.getHtmlElementById("a"), page.getHtmlElementByAccessKey('A')); assertEquals(page.getHtmlElementById("c"), page.getHtmlElementByAccessKey('c')); assertNull(page.getHtmlElementByAccessKey('z')); } /** * @throws Exception if the test fails */ @Test public void testGetHtmlElementsByAccessKey() throws Exception { final String htmlContent = "\n" + "foo\n" + "foo\n" + "foo\n" + "
\n" + "foo\n" + "
"; final HtmlPage page = loadPage(htmlContent); final List expectedElements = Arrays.asList(new HtmlElement[] {page.getHtmlElementById("a"), page.getHtmlElementById("b")}); final List collectedElements = page.getHtmlElementsByAccessKey('a'); assertEquals(expectedElements, collectedElements); } /** * @throws Exception if the test fails */ @Test public void testGetFullQualifiedUrl_NoBaseSpecified() throws Exception { final String htmlContent = "foo\n" + "
\n" + "
\n" + "
"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setDefaultResponse(htmlContent); client.setWebConnection(webConnection); final String urlString = URL_GARGOYLE.toExternalForm(); final HtmlPage page = client.getPage(URL_GARGOYLE); assertEquals(urlString, page.getFullyQualifiedUrl("")); assertEquals(urlString + "foo", page.getFullyQualifiedUrl("foo")); assertEquals("http://foo.com/bar", page.getFullyQualifiedUrl("http://foo.com/bar")); assertEquals("mailto:me@foo.com", page.getFullyQualifiedUrl("mailto:me@foo.com")); assertEquals(urlString + "foo", page.getFullyQualifiedUrl("foo")); assertEquals(urlString + "bbb", page.getFullyQualifiedUrl("aaa/../bbb")); assertEquals(urlString + "c/d", page.getFullyQualifiedUrl("c/./d")); final HtmlPage secondPage = client.getPage(urlString + "foo/bar?a=b&c=d"); assertEquals(urlString + "foo/bar?a=b&c=d", secondPage.getFullyQualifiedUrl("")); assertEquals(urlString + "foo/one", secondPage.getFullyQualifiedUrl("one")); assertEquals(urlString + "two", secondPage.getFullyQualifiedUrl("/two")); assertEquals(urlString + "foo/two?a=b", secondPage.getFullyQualifiedUrl("two?a=b")); final HtmlPage thirdPage = client.getPage("http://foo.com/dog/cat/one.html"); assertEquals("http://foo.com/dog/cat/one.html", thirdPage.getFullyQualifiedUrl("")); assertEquals("http://foo.com/dog/cat/two.html", thirdPage.getFullyQualifiedUrl("two.html")); } /** * @throws Exception if the test fails */ @Test public void testGetFullQualifiedUrl_WithBase() throws Exception { testGetFullQualifiedUrl_WithBase("http", ""); testGetFullQualifiedUrl_WithBase("http", ":8080"); testGetFullQualifiedUrl_WithBase("https", ""); testGetFullQualifiedUrl_WithBase("https", ":2005"); } /** * @throws Exception if the test fails */ private void testGetFullQualifiedUrl_WithBase(final String baseProtocol, final String basePortPart) throws Exception { final String baseUrl = baseProtocol + "://second" + basePortPart; final String htmlContent = "foo\n" + "\n" + "\n" + "
\n" + "
\n" + "
"; final HtmlPage page = loadPage(htmlContent); assertEquals(baseUrl, page.getFullyQualifiedUrl("")); assertEquals(baseUrl + "/foo", page.getFullyQualifiedUrl("foo")); assertEquals(baseUrl + "/foo.js", page.getFullyQualifiedUrl("/foo.js")); assertEquals("http://foo.com/bar", page.getFullyQualifiedUrl("http://foo.com/bar")); assertEquals("mailto:me@foo.com", page.getFullyQualifiedUrl("mailto:me@foo.com")); assertEquals(baseUrl + "/bbb", page.getFullyQualifiedUrl("aaa/../bbb")); assertEquals(baseUrl + "/c/d", page.getFullyQualifiedUrl("c/./d")); } /** * @throws Exception if the test fails */ @Test public void testGetFullQualifiedUrl_WithInvalidBase() throws Exception { final String htmlContent = ""; final HtmlPage page = loadPage(htmlContent); // invalid base URL should be ignored assertEquals("http://somewhere.com/", page.getFullyQualifiedUrl("http://somewhere.com/")); assertEquals(page.getWebResponse().getRequestUrl() + "foo.html", page.getFullyQualifiedUrl("foo.html")); } /** * @throws Exception if an error occurs */ @Test public void testBase_Multiple() throws Exception { final String html = "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + " \n" + ""; final WebClient webClient = new WebClient(); final List collectedIncorrectness = new ArrayList(); final IncorrectnessListener listener = new IncorrectnessListener() { @Test public void notify(final String message, final Object origin) { collectedIncorrectness.add(message); } }; webClient.setIncorrectnessListener(listener); final MockWebConnection webConnection = new MockWebConnection(); webClient.setWebConnection(webConnection); webConnection.setDefaultResponse(html); final HtmlPage page = webClient.getPage(URL_FIRST); page.getAnchors().get(0).click(); final String[] expectedIncorrectness = {"Multiple 'base' detected, only the first is used."}; assertEquals(expectedIncorrectness, collectedIncorrectness); } /** * @throws Exception if an error occurs */ @Test public void testBase_InsideBody() throws Exception { final String html = "\n" + "\n" + "\n" + "\n" + " \n" + " \n" + ""; final WebClient webClient = new WebClient(); final List collectedIncorrectness = new ArrayList(); final IncorrectnessListener listener = new IncorrectnessListener() { @Test public void notify(final String message, final Object origin) { collectedIncorrectness.add(message); } }; webClient.setIncorrectnessListener(listener); final MockWebConnection webConnection = new MockWebConnection(); webClient.setWebConnection(webConnection); webConnection.setDefaultResponse(html); final HtmlPage page = webClient.getPage(URL_FIRST); final HtmlAnchor anchor = page.getAnchors().get(0); final HtmlPage secondPage = (HtmlPage) anchor.click(); final String[] expectedIncorrectness = { "Element 'base' must appear in , it is ignored." }; assertEquals(expectedIncorrectness, collectedIncorrectness); assertEquals(URL_FIRST + "somepage.html", secondPage.getWebResponse().getRequestUrl()); } /** * @throws Exception if the test fails */ @Test public void testOnLoadHandler_BodyStatement() throws Exception { final String htmlContent = "foo\n" + "\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"foo"}; assertEquals(expectedAlerts, collectedAlerts); } /** * If the onload handler contains two statements then only the first would execute. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_TwoBodyStatements() throws Exception { final String htmlContent = "foo\n" + "\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"foo", "bar"}; assertEquals(expectedAlerts, collectedAlerts); } /** * Regression test for bug 713646. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_BodyName() throws Exception { final String htmlContent = "foo\n" + "\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"foo"}; assertEquals(expectedAlerts, collectedAlerts); } /** * Regression test for bug 713646. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_BodyName_NotAFunction() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); page.getAnchorByName("alert").click(); final String[] expectedAlerts = {"4711"}; assertEquals(expectedAlerts, collectedAlerts); } /** * Regression test for window.onload property. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_ScriptName() throws Exception { final String htmlContent = "foo\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"foo"}; assertEquals(expectedAlerts, collectedAlerts); } /** * Regression test for window.onload property. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_ScriptNameRead() throws Exception { final String htmlContent = "foo\n" + ""; final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final String[] expectedAlerts = {"\nfunction () {\n}\n"}; assertEquals(expectedAlerts, collectedAlerts); } /** * @throws Exception if the test fails */ @Test public void testEmbeddedMetaTag_Regression() throws Exception { final String htmlContent = "foo\n" + "\n" + "
\n" + "\n" + "
\n" + " \n" + " \n" + "
\n" + "
\n" + ""; final List collectedAlerts = new ArrayList(); // This used to blow up on page load final HtmlPage page = loadPage(htmlContent, collectedAlerts); assertEquals("foo", page.getTitleText()); final List< ? > expectedAlerts = Collections.EMPTY_LIST; assertEquals(expectedAlerts, collectedAlerts); } /** * @throws Exception if the test fails */ @Test public void testGetPageEncoding() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "\n" + "
\n" + "\n" + "
\n" + " \n" + " \n" + "
\n" + "
\n" + ""; final HtmlPage page = loadPage(htmlContent); assertEquals("Shift_JIS", page.getPageEncoding()); } /** * @throws Exception if the test fails */ @Test public void testGetForms() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "
\n" + "foo\n" + "
\n" + "
\n" + "foo\n" + "
\n" + "\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); final List expectedForms = Arrays.asList(new HtmlForm[] {page.getFormByName("one"), page.getFormByName("two")}); assertEquals(expectedForms, page.getForms()); } /** * Test auto-refresh from a meta tag. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTag_DefaultRefreshHandler() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * @throws Exception if the test fails */ @Test public void testRefresh_MetaTag_caseSensitivity() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test auto-refresh from a meta tag with no URL. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTag_NoUrl() throws Exception { final String firstContent = "first\n" + "\n" + ""; final WebClient client = new WebClient(); final List collectedItems = new ArrayList(); client.setRefreshHandler(new LoggingRefreshHandler(collectedItems)); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); client.setWebConnection(webConnection); client.getPage(URL_FIRST); // avoid using equals() on URL because it takes to much time (due to ip resolution) assertEquals("first", collectedItems.get(0)); assertEquals(URL_FIRST, (URL) collectedItems.get(1)); assertEquals(new Integer(1), collectedItems.get(2)); } /** * Ensures that if a page is supposed to refresh itself every certain amount of * time, and the ImmediateRefreshHandler is being used, an OOME is avoided by * not performing the refresh. * @throws Exception if the test fails */ @Test public void testRefresh_ImmediateRefresh_AvoidOOME() throws Exception { final String firstContent = "first\n" + "\n" + ""; final WebClient client = new WebClient(); assertTrue(ImmediateRefreshHandler.class.isInstance(client.getRefreshHandler())); try { loadPage(firstContent); fail("should have thrown"); } catch (final RuntimeException e) { assertTrue(e.getMessage().indexOf("could have caused an OutOfMemoryError") > -1); } Thread.sleep(1000); } /** * Test auto-refresh from a meta tag with URL quoted. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTagQuoted() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test auto-refresh from a meta tag with URL partly quoted. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTagPartlyQuoted() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test auto-refresh from a meta tag inside noscript. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTagNoScript() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); HtmlPage page = client.getPage(URL_FIRST); assertEquals("first", page.getTitleText()); client.setJavaScriptEnabled(false); page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test auto-refresh from a meta tag with a refresh handler that doesn't refresh. * @throws Exception if the test fails */ @Test public void testRefresh_MetaTag_CustomRefreshHandler() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final List collectedItems = new ArrayList(); client.setRefreshHandler(new LoggingRefreshHandler(collectedItems)); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("first", page.getTitleText()); // avoid using equals() on URL because it takes to much time (due to ip resolution) assertEquals("first", collectedItems.get(0)); assertEquals(URL_SECOND, ((URL) collectedItems.get(1))); assertEquals(new Integer(3), collectedItems.get(2)); } /** * Test that whitespace before and after ';' is permitted. * * @throws Exception if the test fails */ @Test public void testRefresh_MetaTag_Whitespace() throws Exception { final String firstContent = "first\n" + "\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test auto-refresh from a response header. * @throws Exception if the test fails */ @Test public void testRefresh_HttpResponseHeader() throws Exception { final String firstContent = "first\n" + ""; final String secondContent = "second"; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", "text/html", Collections .singletonList(new NameValuePair("Refresh", "3;URL=" + URL_SECOND + ""))); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); assertEquals("second", page.getTitleText()); } /** * Test that the parent of the DOM Document (HtmlPage) is null. * @throws Exception if the test fails */ @Test public void testDocumentParentIsNull() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); assertNotNull(page); assertNull(page.getParentNode()); } /** * @throws Exception if the test fails */ @Test public void testDocumentElement() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlElement root = page.getDocumentElement(); assertNotNull(root); assertEquals("html", root.getTagName()); assertSame(page, root.getParentNode()); } /** * @throws Exception if the test fails */ @Test public void testDocumentNodeType() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlElement root = page.getDocumentElement(); assertEquals(org.w3c.dom.Node.DOCUMENT_NODE, page.getNodeType()); assertEquals(org.w3c.dom.Node.ELEMENT_NODE, root.getNodeType()); assertEquals("#document", page.getNodeName()); } /** * @throws Exception if the test fails */ @Test public void testDeregisterFrameWithoutSrc() throws Exception { final String htmlContent = "\n" + "foo\n" + "\n" + "" + ""; final WebClient client = new WebClient(); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setDefaultResponse(html); webConnection.setResponse(new URL(URL_FIRST, "script.js"), "", "text/javascript"); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); String xml = page.asXml(); final String prefix = ""; assertTrue(xml.startsWith(prefix)); xml = xml.substring(prefix.length()); assertEquals(html, xml .replaceAll("[\\n\\r]", "") .replaceAll("\\s\\s+", "") .replaceAll("\"", "'")); } /** * @exception Exception If the test fails */ @Test public void testAsXml2() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "

helloworld &amp; helloall

\n" + ""; final HtmlPage page = loadPage(htmlContent); assertNotNull("xml document could not be parsed", page.asXml()); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); builder.parse(TextUtil.toInputStream(page.asXml())); } /** * @throws Exception if the test fails */ @Test public void testAsXml_unicode() throws Exception { final String unicodeString = "\u064A\u0627 \u0644\u064A\u064A\u0644"; final String html = "\n" + "\n" + "" + unicodeString + ""; final WebClient client = new WebClient(BrowserVersion.getDefault()); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setDefaultResponse(TextUtil.stringToByteArray(html, "UTF-8"), 200, "OK", "text/html"); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_GARGOYLE); final String xml = page.asXml(); assertTrue(xml.contains("
foo
"; final HtmlPage page = loadPage(html); assertEquals(1, page.getHtmlElementsByName("a").size()); assertEquals(2, page.getHtmlElementsByName("b").size()); assertEquals(0, page.getHtmlElementsByName("c").size()); final HtmlElement a = page.getHtmlElementsByName("a").get(0); a.remove(); assertEquals(0, page.getHtmlElementsByName("a").size()); final HtmlElement b1 = page.getHtmlElementsByName("b").get(0); b1.appendChild(a); assertEquals(1, page.getHtmlElementsByName("a").size()); } /** * @exception Exception if the test fails */ @Test public void testGetHtmlElementsByIdAndOrName() throws Exception { final String html = "
foo
bar
" + "
bar
"; final HtmlPage page = loadPage(html); assertEquals(1, page.getHtmlElementsByIdAndOrName("a").size()); assertEquals(2, page.getHtmlElementsByIdAndOrName("b").size()); assertEquals(1, page.getHtmlElementsByIdAndOrName("c").size()); assertEquals(1, page.getHtmlElementsByIdAndOrName("d").size()); final HtmlElement a = page.getHtmlElementsByIdAndOrName("a").get(0); a.remove(); assertEquals(0, page.getHtmlElementsByIdAndOrName("a").size()); final HtmlElement b1 = page.getHtmlElementsByIdAndOrName("b").get(0); b1.appendChild(a); assertEquals(1, page.getHtmlElementsByIdAndOrName("a").size()); } /** * Regression test for bug 1233519. * @exception Exception If the test fails */ @Test public void testGetHtmlElementByIdAfterRemove() throws Exception { final String htmlContent = "foo\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlElement div1 = page.getHtmlElementById("div1"); page.getHtmlElementById("div2"); // would throw if not found div1.remove(); try { page.getHtmlElementById("div1"); // throws if not found fail("div1 should have been removed"); } catch (final ElementNotFoundException e) { // nothing } try { page.getHtmlElementById("div2"); // throws if not found fail("div2 should have been removed"); } catch (final ElementNotFoundException e) { // nothing } } /** * Test getHtmlElementById() when 2 elements have the same id and the first one * is removed. * @exception Exception If the test fails */ @Test public void testGetHtmlElementById_idTwice() throws Exception { final String htmlContent = "foo\n" + "\n" + "
foo
\n" + "bla\n" + "\n" + ""; final HtmlPage page = loadPage(htmlContent); final HtmlElement elt1 = page.getHtmlElementById("id1"); assertEquals("div", elt1.getNodeName()); elt1.remove(); assertEquals("span", page.getHtmlElementById("id1").getNodeName()); } /** * Test the "set-cookie" meta tag. * @throws Exception if the test fails */ @Test public void testSetCookieMetaTag() throws Exception { final String content = "first\n" + "\n" + "\n" + "\n" + ""; final String[] expectedAlerts = {"webm=none"}; createTestPageForRealBrowserIfNeeded(content, expectedAlerts); final List collectedAlerts = new ArrayList(); final HtmlPage page = loadPage(content, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); final Set cookies = page.getWebClient().getCookieManager().getCookies(); assertEquals(1, cookies.size()); final Cookie cookie = cookies.iterator().next(); assertEquals(page.getWebResponse().getRequestUrl().getHost(), cookie.getDomain()); assertEquals("webm", cookie.getName()); assertEquals("none", cookie.getValue()); assertEquals("/", cookie.getPath()); } /** * Regression test for bug 1658273. * @throws Exception if the test fails */ @Test public void testOnLoadHandler_idChange() throws Exception { final String content = "foo\n" + "
'" + "\n" + ""; final String[] expectedAlerts = {"cl2", "cl1"}; createTestPageForRealBrowserIfNeeded(content, expectedAlerts); final List collectedAlerts = new ArrayList(); loadPage(content, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); } /** * Test for bug 1714767. * @throws Exception if the test fails */ @Test public void testNoSlashURL() throws Exception { testNoSlashURL("http:/second"); testNoSlashURL("http:second"); } private void testNoSlashURL(final String url) throws Exception { final String firstContent = "\n" + "\n" + ""; final String secondContent = ""; final WebClient client = new WebClient(BrowserVersion.FIREFOX_2); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); client.setWebConnection(webConnection); final HtmlPage firstPage = client.getPage(URL_FIRST); final HtmlInlineFrame iframe = firstPage.getHtmlElementById("myIFrame"); assertEquals(URL_SECOND, iframe.getEnclosedPage().getWebResponse().getRequestUrl()); } /** * @throws Exception failure */ @Test public void testMetaTagWithEmptyURL() throws Exception { final WebClient client = new WebClient(); client.setRefreshHandler(new ImmediateRefreshHandler()); // connection will return a page with for the first call // and the same page without it for the other calls final MockWebConnection webConnection = new MockWebConnection() { private int nbCalls_ = 0; @Override public WebResponse getResponse(final WebRequestSettings settings) throws IOException { String content = "\n"; if (nbCalls_ == 0) { content += "\n"; } content += ""; nbCalls_++; return new StringWebResponse(content, settings.getUrl()) { private static final long serialVersionUID = 4945986137562358686L; @Override public HttpMethod getRequestMethod() { return settings.getHttpMethod(); } }; } }; client.setWebConnection(webConnection); final WebRequestSettings settings = new WebRequestSettings(URL_GARGOYLE); settings.setHttpMethod(HttpMethod.POST); client.getPage(settings); } /** * @throws Exception if the test fails */ @Test public void testSerialization() throws Exception { // The document.all and form.elements calls are important because they trigger the creation // of HTMLCollections, which have caused serialization problems in the past (see bug 1951047). final String content = "\n" + "
Hello there!
\n" + "\n" + "
\n" + "\n" + ""; final HtmlPage page1 = loadPage(content); final ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); final ObjectOutputStream objectOS = new ObjectOutputStream(byteOS); objectOS.writeObject(page1); final ByteArrayInputStream byteIS = new ByteArrayInputStream(byteOS.toByteArray()); final ObjectInputStream objectIS = new ObjectInputStream(byteIS); final HtmlPage page2 = (HtmlPage) objectIS.readObject(); final Iterator iterator1 = page1.getAllHtmlChildElements().iterator(); final Iterator iterator2 = page2.getAllHtmlChildElements().iterator(); while (iterator1.hasNext()) { assertTrue(iterator2.hasNext()); final HtmlElement element1 = iterator1.next(); final HtmlElement element2 = iterator2.next(); assertEquals(element1.getNodeName(), element2.getNodeName()); } assertFalse(iterator2.hasNext()); assertEquals("Hello there!", page2.getHtmlElementById("myId").getFirstChild().getNodeValue()); } /** * Verifies that a cloned HtmlPage has its own "idMap_". * @throws Exception if the test fails */ @Test public void testClonedPageHasOwnIdMap() throws Exception { final String content = "foo" + "" + "
" + ""; final HtmlPage page = loadPage(content); final HtmlElement id1 = (HtmlElement) page.getDocumentElement().getLastChild().getLastChild(); assertEquals("id1", id1.getId()); assertSame(id1, page.getHtmlElementById("id1")); final HtmlPage clone = page.cloneNode(true); assertSame(id1, page.getHtmlElementById("id1")); final HtmlElement id1clone = (HtmlElement) clone.getDocumentElement().getLastChild().getLastChild(); assertNotSame(id1, id1clone); assertEquals("id1", id1clone.getId()); assertSame(id1clone, clone.getHtmlElementById("id1")); assertNotSame(id1clone, page.getHtmlElementById("id1")); assertNotSame(id1, clone.getHtmlElementById("id1")); page.getHtmlElementById("id2").remove(); try { page.getHtmlElementById("id2"); fail("should have thrown ElementNotFoundException"); } catch (final ElementNotFoundException enfe) { // expected } assertNotNull(clone.getHtmlElementById("id2")); } /** * Verifies that a cloned HtmlPage has its own "documentElement". * @throws Exception if the test fails */ @Test public void testClonedPageHasOwnDocumentElement() throws Exception { final String content = "foo\n" + "\n" + "
\n" + ""; final HtmlPage page = loadPage(content); final HtmlPage clone = page.cloneNode(true); assertTrue(page != clone); final HtmlElement doc = page.getDocumentElement(); final HtmlElement docclone = clone.getDocumentElement(); assertTrue(doc != docclone); } /** * @throws Exception if the test fails */ @Test public void testHtmlAttributeChangeListener_AddAttribute() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "\n" + "

\n" + "\n" + ""; final String[] expectedValues = {"attributeAdded: p,title,myTitle"}; final HtmlPage page = loadPage(htmlContent); final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl(); page.addHtmlAttributeChangeListener(listenerImpl); final HtmlButtonInput myButton = page.getHtmlElementById("myButton"); myButton.click(); assertEquals(expectedValues, listenerImpl.getCollectedValues()); } /** * @throws Exception if the test fails */ @Test public void testHtmlAttributeChangeListener_ReplaceAttribute() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "\n" + "

\n" + "\n" + ""; final String[] expectedValues = {"attributeReplaced: p,title,myTitle"}; final HtmlPage page = loadPage(htmlContent); final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl(); page.addHtmlAttributeChangeListener(listenerImpl); final HtmlButtonInput myButton = page.getHtmlElementById("myButton"); myButton.click(); assertEquals(expectedValues, listenerImpl.getCollectedValues()); } /** * @throws Exception if the test fails */ @Test public void testHtmlAttributeChangeListener_RemoveAttribute() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "\n" + "

\n" + "\n" + ""; final String[] expectedValues = {"attributeRemoved: p,title,myTitle"}; final HtmlPage page = loadPage(htmlContent); final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl(); page.addHtmlAttributeChangeListener(listenerImpl); final HtmlButtonInput myButton = page.getHtmlElementById("myButton"); myButton.click(); assertEquals(expectedValues, listenerImpl.getCollectedValues()); } /** * @throws Exception if the test fails */ @Test public void testHtmlAttributeChangeListener_RemoveListener() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "\n" + "

\n" + "\n" + ""; final String[] expectedValues = {"attributeReplaced: p,title,myTitle"}; final HtmlPage page = loadPage(htmlContent); final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl(); page.addHtmlAttributeChangeListener(listenerImpl); final HtmlButtonInput myButton = page.getHtmlElementById("myButton"); myButton.click(); page.removeHtmlAttributeChangeListener(listenerImpl); myButton.click(); assertEquals(expectedValues, listenerImpl.getCollectedValues()); } /** * @throws Exception if the test fails */ @Test public void testHtmlAttributeChangeListener_ListenerRegistersNewListener() throws Exception { final String htmlContent = "foo\n" + "\n" + "\n" + "

\n" + ""; final HtmlPage page = loadPage(htmlContent); final List collector = new ArrayList(); final HtmlAttributeChangeListener listener2 = new HtmlAttributeChangeListenerTestImpl() { @Override @Test public void attributeReplaced(final HtmlAttributeChangeEvent event) { collector.add("in listener 2"); } }; final HtmlAttributeChangeListener listener1 = new HtmlAttributeChangeListenerTestImpl() { @Override @Test public void attributeReplaced(final HtmlAttributeChangeEvent event) { collector.add("in listener 1"); page.addHtmlAttributeChangeListener(listener2); } }; page.addHtmlAttributeChangeListener(listener1); final HtmlElement p = page.getHtmlElementById("p1"); p.setAttributeValue("title", "new title"); final String[] expectedValues = {"in listener 1"}; assertEquals(expectedValues, collector); collector.clear(); p.setAttributeValue("title", "new new title"); final String[] expectedValues2 = {"in listener 1", "in listener 2"}; assertEquals(expectedValues2, collector); } /** * @throws Exception if the test fails */ @Test public void testCaseInsensitiveRegexReplacement() throws Exception { final String html = ""; final String[] expectedAlerts = {"0"}; final List actualAlerts = new ArrayList(); loadPage(html, actualAlerts); assertEquals(expectedAlerts, actualAlerts); } /** * @throws Exception if the test fails */ @Test public void testRegexReplacementWithFunction() throws Exception { final String html = ""; final String[] expectedAlerts = {"fontSize"}; final List collectedAlerts = new ArrayList(); loadPage(html, collectedAlerts); assertEquals(expectedAlerts, collectedAlerts); } /** * @throws Exception if the test fails */ @Test public void testTitle_EmptyXmlTagExpanded() throws Exception { final String content = "</head>\n" + "<body>Hello World!</body></html>"; final HtmlPage page = loadPage(content); assertTrue(page.asXml().indexOf("") != -1); } /** * Tests getElementById() of child element after appendChild(), removeChild(), then appendChild() * of the parent element. * * @throws Exception if the test fails */ @Test public void testGetElementById_AfterAppendRemoveAppendChild() throws Exception { final String content = "foo\n" + ""; final List collectedAlerts = new ArrayList(); loadPage(content, collectedAlerts); assertFalse(collectedAlerts.get(0).equals("null")); } /** * @throws Exception if the test fails */ @Test public void testGetElementById_AfterAppendingToNewlyCreatedElement() throws Exception { final String content = "foo\n" + ""; final List collectedAlerts = new ArrayList(); loadPage(content, collectedAlerts); assertTrue(collectedAlerts.get(0).equals("null")); } /** * @throws Exception if the test fails */ @Test public void testOnunLoadHandler() throws Exception { final String htmlContent = "foo\n" + "\n" + ""; final String[] expectedAlerts = {"foo", "bar"}; final List collectedAlerts = new ArrayList(); final WebClient client = new WebClient(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection conn = new MockWebConnection(); conn.setResponse(URL_FIRST, htmlContent); client.setWebConnection(conn); client.getPage(URL_FIRST); client.getPage(URL_FIRST); assertEquals(expectedAlerts, collectedAlerts); } /** * @throws Exception if the test fails */ @Test public void testOnbeforeunloadHandler() throws Exception { testOnbeforeunloadHandler(BrowserVersion.INTERNET_EXPLORER_7, false, "first"); testOnbeforeunloadHandler(BrowserVersion.INTERNET_EXPLORER_7, true, "second"); testOnbeforeunloadHandler(BrowserVersion.FIREFOX_2, false, "first"); testOnbeforeunloadHandler(BrowserVersion.FIREFOX_2, true, "second"); } /** * @param browserVersion the browser version to use * @param handlerOk whether OnbeforeunloadHandler.handleEvent will return true of false * @param expectedPageTitle the expected title of the page after clicking */ private void testOnbeforeunloadHandler(final BrowserVersion browserVersion, final boolean handlerOk, final String expectedPageTitle) throws Exception { final WebClient webClient = new WebClient(browserVersion); final MockWebConnection webConnection = new MockWebConnection(); final List collectedConfirms = new ArrayList(); webClient.setOnbeforeunloadHandler(new OnbeforeunloadHandler() { public boolean handleEvent(final Page page, final String message) { collectedConfirms.add(message); return handlerOk; } }); final String expectedMessage = "Any string value here forces a dialog box to appear before closing the window."; final String firstContent = "first\n" + "\n" + "\n" + " Second page\n" + ""; final String secondContent = "second\n" + "\n" + ""; webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); webClient.setWebConnection(webConnection); final HtmlPage page = webClient.getPage(URL_FIRST); final HtmlAnchor anchor = page.getAnchors().get(0); final HtmlPage secondPage = anchor.click(); assertEquals(new String[] {expectedMessage}, collectedConfirms); assertEquals(expectedPageTitle, secondPage.getTitleText()); } /** * @throws Exception if an error occurs */ @Test public void testSrcJavaScript() throws Exception { final String htmlContent = "foo\n" + "\n" + ""; loadPage(htmlContent); } /** * Regression test for asText() which would blow up. * * @exception Exception If the test fails */ @Test public void testAsText() throws Exception { final String htmlContent = "test\n" + "\n" + "\n" + "
a
"; final HtmlPage page = loadPage(htmlContent); page.asText(); } /** * @throws Exception if the test fails */ @Test public void getElementsByTagName() throws Exception { final String firstContent = "First\n" + "\n" + "
\n" + "
a
b
c
\n" + ""; final HtmlPage page = loadPage(firstContent); final NodeList inputs = page.getElementsByTagName("input"); assertEquals(1, inputs.getLength()); assertEquals("button", inputs.item(0).getAttributes().getNamedItem("type").getNodeValue()); final NodeList divs = page.getElementsByTagName("div"); assertEquals(3, divs.getLength()); final HtmlDivision newDiv = new HtmlDivision(null, HtmlDivision.TAG_NAME, page, null); page.getBody().appendChild(newDiv); assertEquals(4, divs.getLength()); } /** * HtmlPage.getReadyState() should give the same information than the document element. * @see 1592733 * @exception Exception If the test fails */ @Test public void testReadyState() throws Exception { final String htmlContent = "test\n" + "\n" + "\n" + "
a
"; final HtmlPage page = loadPage(htmlContent); assertEquals(DomNode.READY_STATE_COMPLETE, page.getReadyState()); } /** * @exception Exception If the test fails */ @Test public void cloneNode() throws Exception { final String html = "\n" + "foo\n" + "\n" + "

hello world

\n" + ""; final HtmlPage page = loadPage(html); page.getByXPath("//p"); final HtmlPage clonedPage = page.cloneNode(true); clonedPage.getByXPath("//p"); } /** * @exception Exception If the test fails */ @Test public void refresh() throws Exception { final Map> map = new HashMap>(); map.put("/one.html", RefreshServlet.class); map.put("/two.html", RefreshServlet.class); server_ = HttpWebConnectionTest.startWebServer(".", null, map); final WebClient client = new WebClient(); final HtmlPage page = client.getPage("http://localhost:" + HttpWebConnectionTest.PORT + "/one.html"); final HtmlSubmitInput submit = page.getHtmlElementById("myButton"); final HtmlPage secondPage = submit.click(); assertEquals("0\nPOST\nsome_name some_value\n", secondPage.getWebResponse().getContentAsString()); final HtmlPage secondPage2 = (HtmlPage) secondPage.refresh(); assertEquals("1\nPOST\nsome_name some_value\n", secondPage2.getWebResponse().getContentAsString()); } /** * Refresh servlet. */ public static class RefreshServlet extends HttpServlet { private int counter_; private static final long serialVersionUID = 4970162835902592484L; /** * {@inheritDoc} */ @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { final Writer writer = resp.getWriter(); resp.setContentType("text/html"); final String response = "\n" + "\n" + "
" + counter_++ + "
\n" + "
\n" + " \n" + " \n" + "
\n" + "\n" + ""; writer.write(response); writer.close(); } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); final StringBuilder builder = new StringBuilder(); builder.append(counter_++).append("\n"); builder.append(req.getMethod()).append("\n"); for (final Enumeration en = req.getParameterNames(); en.hasMoreElements();) { final String name = (String) en.nextElement(); final String value = req.getParameter(name); builder.append(name).append(' ').append(value).append('\n'); } resp.getWriter().write(builder.toString()); } } /** * Performs post-test deconstruction. * @throws Exception if an error occurs */ @After public void after() throws Exception { HttpWebConnectionTest.stopWebServer(server_); server_ = null; } }