From: <no...@us...> - 2003-09-03 19:15:59
|
Log Message: ----------- Added support for the about: protocol - patch from Jun Chen Modified Files: -------------- /cvsroot/htmlunit/htmlunit/src/xdocs: changes.xml /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host: WindowTest.java /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit: WebClient.java Added Files: ----------- /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/protocol/about: Handler.java AboutURLConnection.java Revision Data ------------- Index: changes.xml =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v retrieving revision 1.122 retrieving revision 1.123 diff -u -d -r1.122 -r1.123 --- changes.xml 3 Sep 2003 12:49:36 -0000 1.122 +++ changes.xml 3 Sep 2003 19:15:56 -0000 1.123 @@ -10,6 +10,10 @@ <action type="update" dev="mbowler" id="713646"> Patch for bug in onload handler submitted by Andreas Hangler </action> + <action type="update" dev="mbowler" id="797144 "> + Added support for the about: protocol - patch from Jun Chen + </action> + </release> Index: WindowTest.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WindowTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- WindowTest.java 18 Jul 2003 13:09:58 -0000 1.5 +++ WindowTest.java 3 Sep 2003 19:15:56 -0000 1.6 @@ -61,6 +61,7 @@ * * @version $Revision$ * @author <a href="mailto:mb...@Ga...">Mike Bowler</a> + * @author <a href="mailto:che...@us...">Chen Jun</a> */ public class WindowTest extends WebTestCase { public WindowTest( final String name ) { @@ -164,6 +165,7 @@ /** * _self is a magic name. If we call open(url, '_self') then the current window must be * reloaded. + * @throws Exception If the test fails. */ public void testOpenWindow_self() throws Exception { final WebClient webClient = new WebClient(); @@ -212,6 +214,7 @@ /** * Regression test to reproduce a known bug + * @throws Exception if the test fails */ public void testAlert_NoAlertHandler() throws Exception { final WebClient webClient = new WebClient(); @@ -474,5 +477,70 @@ Thread.sleep(waitTime); } fail("No alerts written within "+maxTime+"ms"); + } + + + public void testAboutURL() throws Exception { + final WebClient webClient = new WebClient(); + final FakeWebConnection webConnection = + new FakeWebConnection(webClient); + final String firstContent = + "<html><body><script language='JavaScript'>" + + "w2=window.open(\"about:blank\", \"AboutBlank\");" + + "w2.document.open();" + + "w2.document.write(\"<html><head><title>hello</title></head><body></body></html>\");" + + "w2.document.close();" + + "</script></body></html>"; + webConnection.setResponse( + new URL("http://first"), + firstContent, + 200, + "OK", + "text/html", + Collections.EMPTY_LIST); + webClient.setWebConnection(webConnection); + + webClient.getPage( + new URL("http://first"), + SubmitMethod.POST, + Collections.EMPTY_LIST); + final WebWindow webWindow = webClient.getWebWindowByName("AboutBlank"); + assertNotNull(webWindow); + + // final HtmlPage page = (HtmlPage) webWindow.getEnclosedPage(); + // assertEquals("<html><head><title>hello</title></head><body></body></html>",page.getDocument().toString()); + } + + /** + * + * @throws Exception If the test fails + */ + public void testWindowFrames() throws Exception { + final WebClient webClient = new WebClient(); + final FakeWebConnection webConnection = + new FakeWebConnection(webClient); + final List collectedAlerts = new ArrayList(); + + webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); + + final String firstContent = + "<html><body><script language='JavaScript'>" + + "if (typeof top.frames[\"anyXXXname\"] == \"undefined\") {" + + "alert('one')};" + + "</script></body></html>"; + webConnection.setResponse( + new URL("http://first"), + firstContent, + 200, + "OK", + "text/html", + Collections.EMPTY_LIST); + webClient.setWebConnection(webConnection); + + webClient.getPage( + new URL("http://first"), + SubmitMethod.POST, + Collections.EMPTY_LIST); + assertEquals(1, collectedAlerts.size()); } } --- NEW FILE: Handler.java --- /* * Copyright (c) 2002, 2003 Gargoyle Software Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Gargoyle Software Inc. * (http://www.GargoyleSoftware.com/)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * 4. The name "Gargoyle Software" must not be used to endorse or promote * products derived from this software without prior written permission. * For written permission, please contact in...@Ga.... * 5. Products derived from this software may not be called "HtmlUnit", nor may * "HtmlUnit" appear in their name, without prior written permission of * Gargoyle Software Inc. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.gargoylesoftware.htmlunit.protocol.about; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; /** * Stream handler for "about:" urls * * @author <a href="mailto:che...@us...">Chen Jun</a> * @version $Revision: 1.1 $ */ public class Handler extends URLStreamHandler { /** * Return a new URLConnection for this url. * @param url The "about:" url. * @return The connection. * @throws IOException If an IO problem occurs */ protected URLConnection openConnection( final URL url ) throws IOException { return new AboutURLConnection(url); } } --- NEW FILE: AboutURLConnection.java --- /* * Copyright (c) 2002, 2003 Gargoyle Software Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Gargoyle Software Inc. * (http://www.GargoyleSoftware.com/)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * 4. The name "Gargoyle Software" must not be used to endorse or promote * products derived from this software without prior written permission. * For written permission, please contact in...@Ga.... * 5. Products derived from this software may not be called "HtmlUnit", nor may * "HtmlUnit" appear in their name, without prior written permission of * Gargoyle Software Inc. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.gargoylesoftware.htmlunit.protocol.about; import java.io.IOException; import java.net.URL; import java.net.URLConnection; /** * A URLConnection for supporting "about:" urls * * @author <a href="mailto:che...@us...">Chen Jun</a> * @version 1.0, 2003-8-24 */ public class AboutURLConnection extends URLConnection { // private final String content_; /** * Create an instance * @param url The "about:" url. */ public AboutURLConnection(final URL url) { super(url); // content_ = url.toExternalForm().substring("about:".length()); } /** * @see java.net.URLConnection#connect() */ public void connect() throws IOException { } } Index: WebClient.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/WebClient.java,v retrieving revision 1.45 retrieving revision 1.46 diff -u -d -r1.45 -r1.46 --- WebClient.java 16 Jul 2003 20:11:21 -0000 1.45 +++ WebClient.java 3 Sep 2003 19:15:57 -0000 1.46 @@ -68,6 +68,7 @@ * @author <a href="mailto:gud...@sf...">Mike J. Bresnahan</a> * @author Dominique Broeglin * @author Noboru Sinohara + * @author <a href="mailto:che...@us..."> Chen Jun</a> */ public class WebClient { @@ -94,9 +95,51 @@ private final List webWindows_ = new ArrayList(); private WebWindow currentWindow_ = new TopLevelWindow("", this); - + private static URLStreamHandler JavaScriptUrlStreamHandler_ = new com.gargoylesoftware.htmlunit.protocol.javascript.Handler(); + private static URLStreamHandler AboutUrlStreamHandler_ + = new com.gargoylesoftware.htmlunit.protocol.about.Handler(); + //singleton WebResponse for "about:blank" + private static final WebResponse WEB_RESPONSE_FOR_ABOUT_BLANK = new WebResponse() { + public int getStatusCode() { + return 200; + } + public String getStatusMessage() { + return "OK"; + } + public String getContentType() { + return "text/html"; + } + public String getContentAsString() { + return ""; + } + public InputStream getContentAsStream() { + return TextUtil.toInputStream(""); + } + public URL getUrl() { + try { + return new URL(null,"about:blank",AboutUrlStreamHandler_); + } + catch (MalformedURLException e) { + // impossible + e.printStackTrace(); + return null; + } + } + public String getResponseHeaderValue(final String key) { + return ""; + } + public long getLoadTimeInMilliSeconds() { + return 0; + } + public byte[] getResponseBody() { + return "".getBytes(); + } + public String getContentCharSet() { + return "ISO-8859-1"; + } + }; /** @@ -136,6 +179,7 @@ Assert.notNull("browserVersion", browserVersion); Assert.notNull( "proxyHost", proxyHost ); + browserVersion_ = browserVersion; proxyHost_ = proxyHost; proxyPort_ = proxyPort; try { @@ -334,7 +378,10 @@ final WebResponse webResponse; if( protocol.equals("javascript") ) { webResponse = makeWebResponseForJavaScriptUrl(webWindow, url); - } + } + else if (protocol.equals("about")) { + webResponse = makeWebResponseForAboutUrl(webWindow,url); + } else { webResponse = loadWebResponse( url, method, parameters ); } @@ -825,7 +872,10 @@ if( TextUtil.startsWithIgnoreCase(urlString, "javascript:") ) { return new URL(null, urlString, JavaScriptUrlStreamHandler_); - } + } + else if (TextUtil.startsWithIgnoreCase(urlString,"about:")){ + return new URL(null, urlString, AboutUrlStreamHandler_); + } else { return new URL(urlString); } @@ -908,6 +958,15 @@ buffer.append("/"); } return makeUrl( buffer.toString() ); + } + private WebResponse makeWebResponseForAboutUrl( + final WebWindow webWindow, + final URL url) { + if (!url.toExternalForm().substring("about:".length()).equalsIgnoreCase("blank")){ + throw new IllegalArgumentException( + url.toExternalForm()+"is not supported, only about:blank is supported now."); + } + return WEB_RESPONSE_FOR_ABOUT_BLANK; } |