📄 webpagetest.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebPageTest.java,v 1.40 2004/08/10 20:28:53 russgold Exp $** Copyright (c) 2000-2003, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import java.io.PrintWriter;import java.io.FileWriter;import java.io.File;import java.io.IOException;import java.net.URL;import junit.framework.Test;import junit.framework.TestSuite;/** * Unit tests for page structure, style, and headers. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> * @author <a href="mailto:bx@bigfoot.com">Benoit Xhenseval</a> **/public class WebPageTest extends HttpUnitTest { public static void main( String args[] ) { junit.textui.TestRunner.run( suite() ); } public static Test suite() { return new TestSuite( WebPageTest.class ); } public WebPageTest( String name ) { super( name ); } public void testNoResponse() throws Exception { WebConversation wc = new WebConversation(); try { WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); wc.getResponse( request ); fail( "Did not complain about missing page" ); } catch (HttpNotFoundException e) { } } public void testProxyServerAccess() throws Exception { defineResource( "http://someserver.com/sample", "Get this", "text/plain" ); WebConversation wc = new WebConversation(); try { wc.setProxyServer( "localhost", getHostPort() ); WebResponse wr = wc.getResponse( "http://someserver.com/sample" ); String result = wr.getText(); assertEquals( "Expected text", "Get this", result.trim() ); } finally { wc.clearProxyServer(); } } public void testHtmlRequirement() throws Exception { defineResource( "TextPage.txt", "Just text", "text/plain" ); defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head><body>Something here</body></html>", "text/html" ); defineResource( "StructuredPage.html", "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML Basic 1.0//EN' 'http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd'>" + "<html><head><title>A Structured Page</title></head><body>Something here</body></html>", "text/xhtml" ); defineResource( "XHTMLPage.html", "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML Basic 1.0//EN' 'http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd'>" + "<html><head><title>An XHTML Page</title></head><body>Something here</body></html>", "application/xhtml+xml" ); WebConversation wc = new WebConversation(); try { wc.getResponse( getHostPath() + "/TextPage.txt" ).getReceivedPage().getTitle(); fail( "Should have rejected attempt to get a title from a text page" ); } catch (NotHTMLException e ) {} WebResponse simplePage = wc.getResponse( getHostPath() + "/SimplePage.html" ); assertEquals( "HTML Title", "A Sample Page", simplePage.getReceivedPage().getTitle() ); WebResponse structuredPage = wc.getResponse( getHostPath() + "/StructuredPage.html" ); assertEquals( "XHTML Title", "A Structured Page", structuredPage.getReceivedPage().getTitle() ); WebResponse xhtmlPage = wc.getResponse( getHostPath() + "/XHTMLPage.html" ); assertEquals( "XHTML Title", "An XHTML Page", xhtmlPage.getReceivedPage().getTitle() ); } public void testTitle() throws Exception { defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head>\n" + "<body>This has no forms but it does\n" + "have <a href=\"/other.html\">an <b>active</b> link</A>\n" + " and <a name=here>an anchor</a>\n" + "<a href=\"basic.html\"><IMG SRC=\"/images/arrow.gif\" ALT=\"Next -->\" WIDTH=1 HEIGHT=4></a>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Title", "A Sample Page", simplePage.getTitle() ); assertEquals( "Character set", "iso-8859-1", simplePage.getCharacterSet() ); assertNull( "No refresh request should have been found", simplePage.getRefreshRequest() ); } public void testLocalFile() throws Exception { File file = new File( "temp.html" ); FileWriter fw = new FileWriter( file ); PrintWriter pw = new PrintWriter( fw ); pw.println( "<html><head><title>A Sample Page</title></head>" ); pw.println( "<body>This is a very simple page<p>With not much text</body></html>" ); pw.close(); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( "file:" + file.getAbsolutePath() ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Title", "A Sample Page", simplePage.getTitle() ); assertEquals( "Character set", System.getProperty( "file.encoding" ), simplePage.getCharacterSet() ); file.delete(); } public void testNoLocalFile() throws Exception { File file = new File( "temp.html" ); file.delete(); try { WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( "file:" + file.getAbsolutePath() ); wc.getResponse( request ); fail( "Should have complained about missing file" ); } catch (java.io.FileNotFoundException e) { } } public void testRefreshHeader() throws Exception { String refreshURL = getHostPath() + "/NextPage.html"; String page = "<html><head><title>Sample</title></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); addResourceHeader( "SimplePage.html", "Refresh: 2;URL=NextPage.html" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertNotNull( "No Refresh header found", simplePage.getRefreshRequest() ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 2, simplePage.getRefreshDelay() ); } public void testMetaRefreshRequest() throws Exception { String refreshURL = getHostPath() + "/NextPage.html"; String page = "<html><head><title>Sample</title>" + "<meta Http_equiv=refresh content='2;\"" + refreshURL + "\"'></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 2, simplePage.getRefreshDelay() ); } public void testMetaRefreshURLRequest() throws Exception { String refreshURL = getHostPath() + "/NextPage.html"; String page = "<html><head><title>Sample</title>" + "<meta Http-equiv=refresh content='2;URL=\"NextPage.html\"></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 2, simplePage.getRefreshDelay() ); } public void testMetaRefreshAbsoluteURLRequestWithAmpersandEncoding() throws Exception { String refreshURL = "http://localhost:8080/someapp/secure/?username=abc&somevalue=abc"; String page = "<html><head><title>Sample</title>" + "<meta Http-equiv=refresh content='2;URL=\"http://localhost:8080/someapp/secure/?username=abc&somevalue=abc\"></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 2, simplePage.getRefreshDelay() ); } public void testMetaRefreshURLRequestNoDelay() throws Exception { String refreshURL = getHostPath() + "/NextPage.html"; String page = "<html><head><title>Sample</title>" + "<meta Http-equiv=refresh content='URL=\"NextPage.html\"></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 0, simplePage.getRefreshDelay() ); } public void testMetaRefreshURLRequestDelayOnly() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -