package com.meterware.pseudoserver;
/********************************************************************************************************************
* $Id: PseudoServerTest.java 915 2008-04-14 19:39:44Z russgold $
*
* 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 testBadlyFormedMessageViaSocket() throws Exception {
defineResource( "sample", "Get this", "text/plain" );
Socket socket = new Socket( "localhost", getHostPort() );
OutputStream os = socket.getOutputStream();
InputStream is = new BufferedInputStream( socket.getInputStream() );
os.write( "GET /sample HTTP/1.0".getBytes() );
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 error message", result.indexOf( "400" ) > 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" );
}
} );
SocketConne
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
httpunit-1.7 (468个子文件)
site.css 2KB
stylesheet.css 1KB
tutorial.css 982B
manual.css 780B
JCE_DSA.DSA 2KB
APPLEPRO.DSA 1KB
web-app_2_3.dtd 32KB
web-app_2_2.dtd 18KB
arrow_yellow.gif 114B
inherit.gif 57B
index-all.html 394KB
WebResponse.html 98KB
release_notes.html 87KB
WebForm.html 69KB
FormControl.html 58KB
WebClient.html 51KB
HttpUnitOptions.html 50KB
WebRequestSource.html 45KB
ParsedHTML.html 43KB
BlockElement.html 41KB
HTMLSegment.html 40KB
WebTable.html 40KB
WebRequest.html 40KB
ServletUnitServletContext.html 37KB
WebApplet.html 36KB
WebList.html 34KB
WebLink.html 30KB
Button.html 30KB
HttpUserAgentTest.html 30KB
ClientProperties.html 30KB
SubmitButton.html 30KB
TableRow.html 29KB
RadioButtonFormControl.html 28KB
overview-tree.html 28KB
WebForm.Scriptable.html 28KB
ServletRunner.html 27KB
FormControl.Scriptable.html 27KB
MessageBodyWebRequest.html 26KB
ScriptableDelegate.html 26KB
HTMLParserFactory.html 26KB
ServletUnitClient.html 25KB
RadioGroupFormControl.html 25KB
TextBlock.html 25KB
WebResponse.Scriptable.html 24KB
package-summary.html 24KB
WebImage.html 24KB
PostMethodWebRequest.html 24KB
WebConversation.html 23KB
PseudoServer.html 23KB
HTMLPage.Scriptable.html 22KB
WebImage.Scriptable.html 21KB
WebLink.Scriptable.html 21KB
TableCell.html 21KB
package-tree.html 21KB
HttpUnitUtils.html 21KB
constant-values.html 21KB
HeaderOnlyWebRequest.html 20KB
InvocationContext.html 20KB
WebWindow.html 20KB
ResetButton.html 19KB
HTMLElement.html 19KB
CookieJar.html 19KB
deprecated-list.html 19KB
PutMethodWebRequest.html 18KB
HTMLPage.html 18KB
GetMethodWebRequest.html 17KB
PseudoServlet.html 17KB
HttpException.html 16KB
HttpsProtocolSupport.html 16KB
WebResource.html 16KB
HeadMethodWebRequest.html 15KB
ScriptingEngineFactory.html 15KB
RecursiveRedirectionException.html 15KB
CookieProperties.html 15KB
FormParameter.html 14KB
NodeUtils.html 14KB
allclasses-frame.html 14KB
Input.html 14KB
Cookie.html 14KB
serialized-form.html 13KB
task1editor-validation.html 13KB
CookieListener.html 13KB
HTMLParser.html 13KB
ServletRunner.JasperJSPServletDescriptor.html 13KB
SelectionOption.html 12KB
JUnitServlet.html 12KB
WebClient.HeaderDictionary.html 12KB
allclasses-noframe.html 12KB
ScriptingEngine.html 12KB
HttpHeader.html 12KB
MessageBodyWebRequest.InputStreamMessageBody.html 12KB
AuthorizationRequiredException.html 12KB
HttpNotFoundException.html 12KB
HttpInternalErrorException.html 12KB
ScriptingHandler.html 12KB
ServletTestCase.html 12KB
DialogAdapter.html 12KB
ScriptingEventHandler.html 11KB
InvocationContextFactory.html 11KB
HttpRequest.html 11KB
共 468 条
- 1
- 2
- 3
- 4
- 5
资源评论
- jaty1632018-11-08还是不错的,用的挺好的
chai168
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功