📄 testclient.java
字号:
/*
* Copyright 1999, 2000 ,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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: 303278 $ $Date: 2004-09-25 17:27:31 -0400 (Sat, 25 Sep 2004) $
*/
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;
public int getStatus() {
return (this.status);
}
public void setStatus(int status) {
this.status = status;
}
// ------------------------------------------------------- Static Variables
/**
* The session identifier returned by the most recent request, or
* <code>null</code> if the previous request did not specify a session
* identifier.
*/
protected static String sessionId = null;
// --------------------------------------------------------- Public Methods
/**
* Execute the test that has been configured by our property settings.
*
* @exception BuildException if an exception occurs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -