📄 pseudoservertest.java
字号:
package com.meterware.pseudoserver;/******************************************************************************************************************** * $Id: PseudoServerTest.java,v 1.15 2004/06/16 00:10:53 russgold Exp $ * * Copyright (c) 2000-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 junit.framework.TestSuite;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.BufferedOutputStream;import java.net.HttpURLConnection;import java.net.Socket;public class PseudoServerTest extends HttpUserAgentTest { public static void main( String args[] ) { junit.textui.TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( PseudoServerTest.class ); } public PseudoServerTest( String name ) { super( name ); } public void testNotFoundStatus() throws Exception { SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "GET", "/nothing.htm" ); assertEquals( "Response code", HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode() ); } public void testStatusSpecification() throws Exception { defineResource( "error.htm", "Not Modified", 304 ); SocketConnection conn = new SocketConnection( "localhost", getHostPort() ); SocketConnection.SocketResponse response = conn.getResponse( "GET", "/error.htm" ); assertEquals( "Response code", 304, response.getResponseCode() ); } /** * This tests simple access to the server without using any client classes. */ public void testGetViaSocket() throws Exception { defineResource( "sample", "Get this", "text/plain" ); Socket socket = new Socket( "localhost", getHostPort() ); OutputStream os = socket.getOutputStream(); InputStream is = new BufferedInputStream( socket.getInputStream() ); sendHTTPLine( os, "GET /sample HTTP/1.0" ); sendHTTPLine( os, "Host: meterware.com" ); sendHTTPLine( os, "" ); StringBuffer sb = new StringBuffer(); int b; while (-1 != (b = is.read())) sb.append( (char) b ); String result = sb.toString(); assertTrue( "Did not find matching protocol", result.startsWith( "HTTP/1.0" ) ); assertTrue( "Did not find expected text", result.indexOf( "Get this" ) > 0 ); } /** * This tests simple access to the server without using any client classes. */ public void testProxyGetViaSocket() throws Exception { defineResource( "http://someserver.com/sample", "Get this", "text/plain" ); Socket socket = new Socket( "localhost", getHostPort() ); OutputStream os = socket.getOutputStream(); InputStream is = new BufferedInputStream( socket.getInputStream() ); sendHTTPLine( os, "GET http://someserver.com/sample HTTP/1.0" ); sendHTTPLine( os, "Host: someserver.com" ); sendHTTPLine( os, "" ); StringBuffer sb = new StringBuffer(); int b; while (-1 != (b = is.read())) sb.append( (char) b ); String result = sb.toString(); assertTrue( "Did not find matching protocol", result.startsWith( "HTTP/1.0" ) ); assertTrue( "Did not find expected text", result.indexOf( "Get this" ) > 0 ); } private void sendHTTPLine( OutputStream os, final String line ) throws IOException { os.write( line.getBytes() ); os.write( 13 ); os.write( 10 ); } /** * This verifies that the PseudoServer detects and echoes its protocol. */ public void testProtocolMatching() throws Exception { defineResource( "sample", "Get this", "text/plain" ); Socket socket = new Socket( "localhost", getHostPort() ); OutputStream os = socket.getOutputStream(); InputStream is = new BufferedInputStream( socket.getInputStream() ); sendHTTPLine( os, "GET /sample HTTP/1.1" ); sendHTTPLine( os, "Host: meterware.com" ); sendHTTPLine( os, "Connection: close" ); sendHTTPLine( os, "" ); StringBuffer sb = new StringBuffer(); int b; while (-1 != (b = is.read())) sb.append( (char) b ); String result = sb.toString(); assertTrue( "Did not find matching protocol", result.startsWith( "HTTP/1.1" ) ); assertTrue( "Did not find expected text", result.indexOf( "Get this" ) > 0 ); } /** * This verifies that the PseudoServer can be restricted to a HTTP/1.0. */ public void testProtocolThrottling() throws Exception { getServer().setMaxProtocolLevel( 1, 0 ); defineResource( "sample", "Get this", "text/plain" ); Socket socket = new Socket( "localhost", getHostPort() ); OutputStream os = socket.getOutputStream(); InputStream is = new BufferedInputStream( socket.getInputStream() ); sendHTTPLine( os, "GET /sample HTTP/1.1" ); sendHTTPLine( os, "Host: meterware.com" ); sendHTTPLine( os, "Connection: close" ); sendHTTPLine( os, "" ); StringBuffer sb = new StringBuffer(); int b; while (-1 != (b = is.read())) sb.append( (char) b ); String result = sb.toString(); assertTrue( "Did not find matching protocol", result.startsWith( "HTTP/1.0" ) ); assertTrue( "Did not find expected text", result.indexOf( "Get this" ) > 0 ); } public void testPseudoServlet() throws Exception { String resourceName = "tellMe"; String name = "Charlie"; final String prefix = "Hello there, "; String expectedResponse = prefix + name; defineResource( resourceName, new PseudoServlet() { public WebResource getPostResponse() { return new WebResource( prefix + getParameter( "name" )[0], "text/plain" ); } } );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -