📄 testclient.java
字号:
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* 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 acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <apache@apache.org>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* 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 *
* THE APACHE SOFTWARE FOUNDATION 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. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.tester;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* <p>This class contains a <strong>Task</strong> for Ant that is used to
* send HTTP requests to a servlet container, and examine the responses.
* It is similar in purpose to the <code>GTest</code> task in Watchdog,
* but uses the JDK's HttpURLConnection for underlying connectivity.</p>
*
* <p>The task is registered with Ant using a <code>taskdef</code> directive:
* <pre>
* <taskdef name="tester" classname="org.apache.tester.TestClient">
* </pre>
* and accepts the following configuration properties:</p>
* <ul>
* <li><strong>golden</strong> - The server-relative path of the static
* resource containing the golden file for this request.</li>
* <li><strong>host</strong> - The server name to which this request will be
* sent. Defaults to <code>localhost</code> if not specified.</li>
* <li><strong>inContent</strong> - The data content that will be submitted
* with this request. The test client will transparently add a carriage
* return and line feed, and set the content length header, if this is
* specified. Otherwise, no content will be included in the request.</li>
* <li><strong>inHeaders</strong> - The set of one or more HTTP headers that
* will be included on the request.</li>
* <li><strong>message</strong> - The HTTP response message that is expected
* in the response from the server. No check is made if no message
* is specified.</li>
* <li><strong>method</strong> - The HTTP request method to be used on this
* request. Defaults to <ocde>GET</code> if not specified.</li>
* <li><strong>outContent</strong> - The first line of the response data
* content that we expect to receive. No check is made if no content is
* specified.</li>
* <li><strong>outHeaders</strong> - The set of one or more HTTP headers that
* are expected in the response (order independent).</li>
* <li><strong>port</strong> - The port number to which this request will be
* sent. Defaults to <code>8080</code> if not specified.</li>
* <li><strong>redirect</strong> - If set to true, follow any redirect that
* is returned by the server. (Only works when using HttpURLConnection).
* </li>
* <li><strong>request</strong> - The request URI to be transmitted for this
* request. This value should start with a slash character ("/"), and
* be the server-relative URI of the requested resource.</li>
* <li><strong>status</strong> - The HTTP status code that is expected in the
* response from the server. Defaults to <code>200</code> if not
* specified. Set to zero to disable checking the return value.</li>
* </ul>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1.1.1 $ $Date: 2002/07/18 16:47:29 $
*/
public class TestClient extends Task {
// ----------------------------------------------------- Instance Variables
/**
* The saved golden file we will compare to the response. Each element
* contains a line of text without any line delimiters.
*/
protected ArrayList saveGolden = new ArrayList();
/**
* The saved headers we received in our response. The key is the header
* name (converted to lower case), and the value is an ArrayList of the
* string value(s) received for that header.
*/
protected HashMap saveHeaders = new HashMap();
/**
* The response file to be compared to the golden file. Each element
* contains a line of text without any line delimiters.
*/
protected ArrayList saveResponse = new ArrayList();
// ------------------------------------------------------------- Properties
/**
* The debugging detail level for this execution.
*/
protected int debug = 0;
public int getDebug() {
return (this.debug);
}
public void setDebug(int debug) {
this.debug = debug;
}
/**
* The server-relative request URI of the golden file for this request.
*/
protected String golden = null;
public String getGolden() {
return (this.golden);
}
public void setGolden(String golden) {
this.golden = golden;
}
/**
* The host name to which we will connect.
*/
protected String host = "localhost";
public String getHost() {
return (this.host);
}
public void setHost(String host) {
this.host = host;
}
/**
* The first line of the request data that will be included on this
* request.
*/
protected String inContent = null;
public String getInContent() {
return (this.inContent);
}
public void setInContent(String inContent) {
this.inContent = inContent;
}
/**
* The HTTP headers to be included on the request. Syntax is
* <code>{name}:{value}[##{name}:{value}] ...</code>.
*/
protected String inHeaders = null;
public String getInHeaders() {
return (this.inHeaders);
}
public void setInHeaders(String inHeaders) {
this.inHeaders = inHeaders;
}
/**
* Should we join the session whose session identifier was returned
* on the previous request.
*/
protected boolean joinSession = false;
public boolean getJoinSession() {
return (this.joinSession);
}
public void setJoinSession(boolean joinSession) {
this.joinSession = true;
}
/**
* The HTTP response message to be expected in the response.
*/
protected String message = null;
public String getMessage() {
return (this.message);
}
public void setMessage(String message) {
this.message = message;
}
/**
* The HTTP request method that will be used.
*/
protected String method = "GET";
public String getMethod() {
return (this.method);
}
public void setMethod(String method) {
this.method = method;
}
/**
* The first line of the response data content that we expect to receive.
*/
protected String outContent = null;
public String getOutContent() {
return (this.outContent);
}
public void setOutContent(String outContent) {
this.outContent = outContent;
}
/**
* The HTTP headers to be checked on the response. Syntax is
* <code>{name}:{value}[##{name}:{value}] ...</code>.
*/
protected String outHeaders = null;
public String getOutHeaders() {
return (this.outHeaders);
}
public void setOutHeaders(String outHeaders) {
this.outHeaders = outHeaders;
}
/**
* The port number to which we will connect.
*/
protected int port = 8080;
public int getPort() {
return (this.port);
}
public void setPort(int port) {
this.port = port;
}
/**
* The protocol and version to include in the request, if executed as
* a direct socket connection. Lack of a value here indicates that an
* HttpURLConnection should be used instead.
*/
protected String protocol = null;
public String getProtocol() {
return (this.protocol);
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
/**
* Should we follow redirects returned by the server?
*/
protected boolean redirect = false;
public boolean getRedirect() {
return (this.redirect);
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
/**
* The request URI to be sent to the server. This value is required.
*/
protected String request = null;
public String getRequest() {
return (this.request);
}
public void setRequest(String request) {
this.request = request;
}
/**
* The HTTP status code expected on the response.
*/
protected int status = 200;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -