📄 testclient.java
字号:
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
*/
public void execute() throws BuildException {
saveHeaders.clear();
try {
readGolden();
} catch (IOException e) {
System.out.println("FAIL: readGolden(" + golden + ")");
e.printStackTrace(System.out);
}
if ((protocol == null) || (protocol.length() == 0))
executeHttp();
else
executeSocket();
}
// ------------------------------------------------------ Protected Methods
/**
* Execute the test via use of an HttpURLConnection.
*
* @exception BuildException if an exception occurs
*/
protected void executeHttp() throws BuildException {
// Construct a summary of the request we will be sending
String summary = "[" + method + " " + request + "]";
if (debug >= 1)
System.out.println("RQST: " + summary);
boolean success = true;
String result = null;
Throwable throwable = null;
HttpURLConnection conn = null;
try {
// Configure an HttpURLConnection for this request
URL url = new URL("http", host, port, request);
conn = (HttpURLConnection) url.openConnection();
conn.setAllowUserInteraction(false);
conn.setDoInput(true);
if (inContent != null) {
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length",
"" + inContent.length());
if (debug >= 1)
System.out.println("INPH: Content-Length: " +
inContent.length());
} else {
conn.setDoOutput(false);
}
// Send the session id cookie (if any)
if (joinSession && (sessionId != null)) {
conn.setRequestProperty("Cookie",
"JSESSIONID=" + sessionId);
if (debug >= 1)
System.out.println("INPH: Cookie: JSESSIONID=" +
sessionId);
}
if (this.redirect && (debug >= 1))
System.out.println("FLAG: setInstanceFollowRedirects(" +
this.redirect + ")");
conn.setInstanceFollowRedirects(this.redirect);
conn.setRequestMethod(method);
if (inHeaders != null) {
String headers = inHeaders;
while (headers.length() > 0) {
int delimiter = headers.indexOf("##");
String header = null;
if (delimiter < 0) {
header = headers;
headers = "";
} else {
header = headers.substring(0, delimiter);
headers = headers.substring(delimiter + 2);
}
int colon = header.indexOf(":");
if (colon < 0)
break;
String name = header.substring(0, colon).trim();
String value = header.substring(colon + 1).trim();
conn.setRequestProperty(name, value);
if (debug >= 1)
System.out.println("INPH: " + name + ": " + value);
}
}
// Connect to the server and send our output if necessary
conn.connect();
if (inContent != null) {
if (debug >= 1)
System.out.println("INPD: " + inContent);
OutputStream os = conn.getOutputStream();
for (int i = 0; i < inContent.length(); i++)
os.write(inContent.charAt(i));
os.close();
}
// Acquire the response data, if there is any
String outData = "";
String outText = "";
boolean eol = false;
InputStream is = conn.getInputStream();
int lines = 0;
while (true) {
String line = read(is);
if (line == null)
break;
if (lines == 0)
outData = line;
else
outText += line + "\r\n";
saveResponse.add(line);
lines++;
}
is.close();
// Dump out the response stuff
if (debug >= 1)
System.out.println("RESP: " + conn.getResponseCode() + " " +
conn.getResponseMessage());
for (int i = 1; i < 1000; i++) {
String name = conn.getHeaderFieldKey(i);
String value = conn.getHeaderField(i);
if ((name == null) || (value == null))
break;
if (debug >= 1)
System.out.println("HEAD: " + name + ": " + value);
save(name, value);
if ("Set-Cookie".equals(name))
parseSession(value);
}
if (debug >= 1) {
System.out.println("DATA: " + outData);
if (outText.length() > 2)
System.out.println("TEXT: " + outText);
}
// Validate the response against our criteria
if (success) {
result = validateStatus(conn.getResponseCode());
if (result != null)
success = false;
}
if (success) {
result = validateMessage(conn.getResponseMessage());
if (result != null)
success = false;
}
if (success) {
result = validateHeaders();
if (result != null)
success = false;
}
if (success) {
result = validateData(outData);
if (result != null)
success = false;
}
if (success) {
result = validateGolden();
if (result != null)
success = false;
}
} catch (Throwable t) {
if (t instanceof FileNotFoundException) {
if (status == 404) {
success = true;
result = "Not Found";
throwable = null;
} else {
success = false;
try {
result = "Status=" + conn.getResponseCode() +
", Message=" + conn.getResponseMessage();
} catch (IOException e) {
result = e.toString();
}
throwable = null;
}
} else if (t instanceof ConnectException) {
success = false;
result = t.getMessage();
throwable = null;
} else {
success = false;
result = t.getMessage();
throwable = t;
}
}
// Log the results of executing this request
if (success)
System.out.println("OK " + summary);
else {
System.out.println("FAIL " + summary + " " + result);
if (throwable != null)
throwable.printStackTrace(System.out);
}
}
/**
* Execute the test via use of a socket with direct input/output.
*
* @exception BuildException if an exception occurs
*/
protected void executeSocket() throws BuildException {
// Construct a summary of the request we will be sending
String command = method + " " + request + " " + protocol;
String summary = "[" + command + "]";
if (debug >= 1)
System.out.println("RQST: " + summary);
boolean success = true;
String result = null;
Socket socket = null;
OutputStream os = null;
PrintWriter pw = null;
InputStream is = null;
Throwable throwable = null;
int outStatus = 0;
String outMessage = null;
try {
// Open a client socket for this request
socket = new Socket(host, port);
os = socket.getOutputStream();
pw = new PrintWriter(os);
is = socket.getInputStream();
// Send the command and content length header (if any)
pw.print(command + "\r\n");
if (inContent != null) {
if (debug >= 1)
System.out.println("INPH: " + "Content-Length: " +
inContent.length());
pw.print("Content-Length: " + inContent.length() + "\r\n");
}
// Send the session id cookie (if any)
if (joinSession && (sessionId != null)) {
pw.println("Cookie: JSESSIONID=" + sessionId);
if (debug >= 1)
System.out.println("INPH: Cookie: JSESSIONID=" +
sessionId);
}
// Send the specified headers (if any)
if (inHeaders != null) {
String headers = inHeaders;
while (headers.length() > 0) {
int delimiter = headers.indexOf("##");
String header = null;
if (delimiter < 0) {
header = headers;
headers = "";
} else {
header = headers.substring(0, delimiter);
headers = headers.substring(delimiter + 2);
}
int colon = header.indexOf(":");
if (colon < 0)
break;
String name = header.substring(0, colon).trim();
String value = header.substring(colon + 1).trim();
if (debug >= 1)
System.out.println("INPH: " + name + ": " + value);
pw.print(name + ": " + value + "\r\n");
}
}
pw.print("\r\n");
// Send our content (if any)
if (inContent != null) {
if (debug >= 1)
System.out.println("INPD: " + inContent);
for (int i = 0; i < inContent.length(); i++)
pw.print(inContent.charAt(i));
}
pw.flush();
// Read the response status and associated message
String line = read(is);
if (line == null) {
outStatus = -1;
outMessage = "NO RESPONSE";
} else {
line = line.trim();
if (debug >= 1)
System.out.println("RESP: " + line);
int space = line.indexOf(" ");
if (space >= 0) {
line = line.substring(space + 1).trim();
space = line.indexOf(" ");
}
try {
if (space < 0) {
outStatus = Integer.parseInt(line);
outMessage = "";
} else {
outStatus = Integer.parseInt(line.substring(0, space));
outMessage = line.substring(space + 1).trim();
}
} catch (NumberFormatException e) {
outStatus = -1;
outMessage = "NUMBER FORMAT EXCEPTION";
}
}
if (debug >= 1)
System.out.println("STAT: " + outStatus + " MESG: " +
outMessage);
// Read the response headers (if any)
String headerName = null;
String headerValue = null;
while (true) {
line = read(is);
if ((line == null) || (line.length() == 0))
break;
int colon = line.indexOf(":");
if (colon < 0) {
if (debug >= 1)
System.out.println("????: " + line);
} else {
headerName = line.substring(0, colon).trim();
headerValue = line.substring(colon + 1).trim();
if (debug >= 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -