📄 webclienttest.java
字号:
package com.meterware.httpunit;/******************************************************************************************************************** * $Id: WebClientTest.java,v 1.25 2006/03/09 01:51:36 russgold Exp $ * * Copyright (c) 2002-2004, 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 com.meterware.pseudoserver.PseudoServlet;import com.meterware.pseudoserver.WebResource;import junit.framework.TestSuite;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.net.*;import java.util.ArrayList;import java.util.List;import java.util.zip.GZIPOutputStream;/** * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> **/public class WebClientTest extends HttpUnitTest { public static void main( String args[] ) { junit.textui.TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( WebClientTest.class ); } public WebClientTest( String name ) { super( name ); } public void ntestNoSuchServer() throws Exception { WebConversation wc = new WebConversation(); try { wc.getResponse( "http://no.such.host" ); fail( "Should have rejected the request" ); } catch (UnknownHostException e) { } catch (IOException e) {// if (!(e.getCause() instanceof UnknownHostException)) throw e; } } public void testNotFound() throws Exception { WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/nothing.htm" ); try { wc.getResponse( request ); fail( "Should have rejected the request" ); } catch (HttpNotFoundException e) { assertEquals( "Response code", HttpURLConnection.HTTP_NOT_FOUND, e.getResponseCode() ); assertEquals( "Response message", "unable to find /nothing.htm", e.getResponseMessage() ); } } public void testNotModifiedResponse() throws Exception { defineResource( "error.htm", "Not Modified", 304 ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/error.htm" ); WebResponse response = wc.getResponse( request ); assertEquals( "Response code", 304, response.getResponseCode() ); response.getText(); response.getInputStream().read(); } public void testInternalErrorException() throws Exception { defineResource( "error.htm", "Internal error", 501 ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/error.htm" ); try { wc.getResponse( request ); fail( "Should have rejected the request" ); } catch (HttpException e) { assertEquals( "Response code", 501, e.getResponseCode() ); } } public void testInternalErrorDisplay() throws Exception { defineResource( "error.htm", "Internal error", 501 ); WebConversation wc = new WebConversation(); wc.setExceptionsThrownOnErrorStatus( false ); WebRequest request = new GetMethodWebRequest( getHostPath() + "/error.htm" ); WebResponse response = wc.getResponse( request ); assertEquals( "Response code", 501, response.getResponseCode() ); assertEquals( "Message contents", "Internal error", response.getText().trim() ); } public void testSimpleGet() throws Exception { String resourceName = "something/interesting"; String resourceValue = "the desired content"; defineResource( resourceName, resourceValue ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + '/' + resourceName ); WebResponse response = wc.getResponse( request ); assertEquals( "requested resource", resourceValue, response.getText().trim() ); assertEquals( "content type", "text/html", response.getContentType() ); } public void testFunkyGet() throws Exception { String resourceName = "ID=03.019c010101010001.00000001.a202000000000019. 0d09/login/"; String resourceValue = "the desired content"; defineResource( resourceName, resourceValue ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + '/' + resourceName ); WebResponse response = wc.getResponse( request ); assertEquals( "requested resource", resourceValue, response.getText().trim() ); assertEquals( "content type", "text/html", response.getContentType() ); } public void testCookies() throws Exception { String resourceName = "something/baking"; String resourceValue = "the desired content"; defineResource( resourceName, resourceValue ); addResourceHeader( resourceName, "Set-Cookie: HSBCLoginFailReason=; path=/" ); addResourceHeader( resourceName, "Set-Cookie: age=12, name= george" ); addResourceHeader( resourceName, "Set-Cookie: type=short" ); addResourceHeader( resourceName, "Set-Cookie: funky=ab$==" ); addResourceHeader( resourceName, "Set-Cookie: p30waco_sso=3.0,en,us,AMERICA,Drew;path=/, PORTAL30_SSO_TEST=X" ); addResourceHeader( resourceName, "Set-Cookie: SESSION_ID=17585,Dzm5LzbRPnb95QkUyIX+7w5RDT7p6OLuOVZ91AMl4hsDATyZ1ej+FA==; path=/;" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + '/' + resourceName ); WebResponse response = wc.getResponse( request ); assertEquals( "requested resource", resourceValue, response.getText().trim() ); assertEquals( "content type", "text/html", response.getContentType() ); assertEquals( "number of cookies", 8, wc.getCookieNames().length ); assertEquals( "cookie 'HSBCLoginFailReason' value", "", wc.getCookieValue( "HSBCLoginFailReason" ) ); assertEquals( "cookie 'age' value", "12", wc.getCookieValue( "age" ) ); assertEquals( "cookie 'name' value", "george", wc.getCookieValue( "name" ) ); assertEquals( "cookie 'type' value", "short", wc.getCookieValue( "type" ) ); assertEquals( "cookie 'funky' value", "ab$==", wc.getCookieValue( "funky" ) ); assertEquals( "cookie 'p30waco_sso' value", "3.0,en,us,AMERICA,Drew", wc.getCookieValue( "p30waco_sso" ) ); assertEquals( "cookie 'PORTAL30_SSO_TEST' value", "X", wc.getCookieValue( "PORTAL30_SSO_TEST" ) ); assertEquals( "cookie 'SESSION_ID' value", "17585,Dzm5LzbRPnb95QkUyIX+7w5RDT7p6OLuOVZ91AMl4hsDATyZ1ej+FA==", wc.getCookieValue( "SESSION_ID" ) ); } public void testCookiesDisabled() throws Exception { String resourceName = "something/baking"; String resourceValue = "the desired content"; defineResource( resourceName, resourceValue ); addResourceHeader( resourceName, "Set-Cookie: age=12" ); WebConversation wc = new WebConversation(); wc.getClientProperties().setAcceptCookies( false ); WebRequest request = new GetMethodWebRequest( getHostPath() + '/' + resourceName ); WebResponse response = wc.getResponse( request ); assertEquals( "requested resource", resourceValue, response.getText().trim() ); assertEquals( "content type", "text/html", response.getContentType() ); assertEquals( "number of cookies", 0, wc.getCookieNames().length ); } public void testOldCookies() throws Exception { String resourceName = "something/baking"; String resourceValue = "the desired content"; defineResource( resourceName, resourceValue ); addResourceHeader( resourceName, "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + '/' + resourceName ); WebResponse response = wc.getResponse( request ); assertEquals( "requested resource", resourceValue, response.getText().trim() ); assertEquals( "content type", "text/html", response.getContentType() ); assertEquals( "number of cookies", 1, wc.getCookieNames().length ); assertEquals( "cookie 'CUSTOMER' value", "WILE_E_COYOTE", wc.getCookieValue( "CUSTOMER" ) ); } public void testManualCookies() throws Exception { defineResource( "bounce", new CookieEcho() ); WebConversation wc = new WebConversation(); wc.putCookie( "CUSTOMER", "WILE_E_COYOTE" ); WebResponse response = wc.getResponse( getHostPath() + "/bounce" ); assertEquals( "Cookies sent", "CUSTOMER=WILE_E_COYOTE", response.getText() ); wc.putCookie( "CUSTOMER", "ROAD RUNNER" ); response = wc.getResponse( getHostPath() + "/bounce" ); assertEquals( "Cookies sent", "CUSTOMER=ROAD RUNNER", response.getText() ); } class CookieEcho extends PseudoServlet { public WebResource getGetResponse() throws IOException { return new WebResource( getHeader( "Cookie" ) ); } } public void testHeaderFields() throws Exception { defineResource( "getHeaders", new PseudoServlet() { public WebResource getGetResponse() { StringBuffer sb = new StringBuffer(); sb.append( getHeader( "Junky" ) ).append( "<-->" ).append( getHeader( "User-Agent" ) ); return new WebResource( sb.toString(), "text/plain" ); } } ); WebConversation wc = new WebConversation(); wc.getClientProperties().setUserAgent( "me alone" ); wc.setHeaderField( "junky", "Mozilla 6" ); WebResponse wr = wc.getResponse( getHostPath() + "/getHeaders" ); assertEquals( "headers found", "Mozilla 6<-->me alone", wr.getText() ); } public void testBasicAuthentication() throws Exception { defineResource( "getAuthorization", new PseudoServlet() { public WebResource getGetResponse() { return new WebResource( getHeader( "Authorization" ), "text/plain" ); } } ); WebConversation wc = new WebConversation(); wc.setAuthorization( "user", "password" ); WebResponse wr = wc.getResponse( getHostPath() + "/getAuthorization" ); assertEquals( "authorization", "Basic dXNlcjpwYXNzd29yZA==", wr.getText() ); } public void testProxyServerAccessWithAuthentication() throws Exception { defineResource( "http://someserver.com/sample", new PseudoServlet() { public WebResource getGetResponse() { return new WebResource( getHeader( "Proxy-Authorization" ), "text/plain" ); } } ); WebConversation wc = new WebConversation(); wc.setProxyServer( "localhost", getHostPort(), "user", "password" ); WebResponse wr = wc.getResponse( "http://someserver.com/sample" ); assertEquals( "authorization", "Basic dXNlcjpwYXNzd29yZA==", wr.getText() ); } public void testRefererHeader() throws Exception { String resourceName = "tellMe"; String linkSource = "fromLink"; String formSource = "fromForm"; String page0 = getHostPath() + '/' + resourceName; String page1 = getHostPath() + '/' + linkSource;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -