change socket to httpurlconnection

This commit is contained in:
hmm 2014-08-29 11:39:31 +08:00
parent 77168b6ac7
commit 40e98ba06e
10 changed files with 471 additions and 445 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,26 +5,49 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
public class RequestHandler implements Runnable {
private static final Logger log = Logger.getLogger(RequestHandler.class);
private Config config = Config.getConfig();
/**
* request from browser
*/
private InputStream clientIn;
/**
* response from remote server
*/
private InputStream serverIn;
/**
* send response from proxy to browser
*/
private OutputStream clientOut;
/**
* send request from proxy to remote server
*/
private OutputStream serverOut;
private HttpRequestHeader header;
private ProxyServer proxyServer;
private Socket clientSocket;
private Socket serverSocket;
// private Socket serverSocket;
private HttpURLConnection serverConnection;
private ByteArrayOutputStream buffer;
private static Object mutex = new Object();
@ -48,7 +71,7 @@ public class RequestHandler implements Runnable {
}
throw new Utils.SilentException();
}
if (!this.header.url.startsWith("http"))
if (!this.header.version.startsWith("HTTP"))
throw new Utils.UserException(
"Bench4Q only supports the HTTP protocol.");
URL url = new URL(this.header.url);
@ -66,32 +89,20 @@ public class RequestHandler implements Runnable {
if (port < 1)
port = 80;
try {
this.serverSocket = new Socket(InetAddress.getByName(host), port);
} catch (ConnectException e) {
String msg = "Cannot connect to ";
if (proxy != null)
msg = msg + "proxy server ";
msg = msg + host;
if (port != 80)
msg = msg + " on port " + Integer.toString(port);
throw new Utils.UserException(msg + ".");
}
try {
this.serverIn = this.serverSocket.getInputStream();
this.serverOut = this.serverSocket.getOutputStream();
} catch (Throwable e) {
this.serverSocket.close();
this.serverSocket = null;
throw e;
}
serverConnection = (HttpURLConnection) url.openConnection();
// serverConnection.addRequestProperty("host", host);
// serverConnection.addRequestProperty("port", Integer.toString(port));
// serverConnection.setDoOutput(true);
// this.serverOut = serverConnection.getOutputStream();
}
private String stripProxyInfoFromRequestHeader()
throws MalformedURLException {
String res = "";
String origUrl = this.header.url;
URL url = new URL(origUrl);
URL url = new URL("https://" + origUrl);
this.header.url = url.getFile();
res = this.header.toString();
this.header.url = origUrl;
@ -109,14 +120,14 @@ public class RequestHandler implements Runnable {
initClientServerConnections(this.clientSocket);
String headerStr;
if (this.config.getProxySettings() == null)
headerStr = stripProxyInfoFromRequestHeader();
else
headerStr = this.header.toString();
// if (this.config.getProxySettings() == null)
// headerStr = stripProxyInfoFromRequestHeader();
// else
headerStr = this.header.toString();
log.trace("read request header");
byte[] bytes = headerStr.getBytes();
this.serverOut.write(bytes, 0, bytes.length);
// this.serverOut.write(bytes, 0, bytes.length);
log.trace("wrote request header");
byte[] requestBody;
@ -151,26 +162,33 @@ public class RequestHandler implements Runnable {
}
this.clientSocket.shutdownInput();
this.serverSocket.shutdownOutput();
// close send request to remote server
// serverOut.flush();
// serverOut.close();
this.serverIn = serverConnection.getInputStream();
synchronized (mutex) {
this.proxyServer.processRequest(this.header, requestBody);
log.trace("processed request");
this.buffer.reset();
int len;
int num = 0;
while ((len = this.serverIn.read(buf, 0, buf.length)) >= 0) {
log.trace("read " + Integer.toString(len));
// this.clientOut.write(buf, 0, len);
this.buffer.write(buf, 0, len);
num += len;
}
log.trace("transferred response len:" + len);
ResponseModel responseModel = new ResponseModel(
this.buffer.toByteArray());
this.buffer.toByteArray(), serverConnection);
this.proxyServer.processResponse(this.header, requestBody,
responseModel);
try {
this.clientOut.write(changeHeaderToByte(
serverConnection.getHeaderFields(), responseModel.getResponse().length));
this.clientOut.write(responseModel.getResponse());
} catch (SocketException e) {
log.info("browser stopped listening: " + e.getMessage()
@ -183,18 +201,13 @@ public class RequestHandler implements Runnable {
} catch (Exception e) {
log.error(e, e);
} finally {
if (this.serverSocket != null) {
this.serverSocket.close();
log.trace("closed server socket");
}
// if (this.serverSocket != null) {
// this.serverSocket.close();
// log.trace("closed server socket");
// }
this.clientSocket.close();
log.trace("closed client socket");
}
if (this.serverSocket != null) {
this.serverSocket.close();
log.trace("closed server socket");
}
this.clientSocket.close();
log.trace("closed client socket");
@ -203,6 +216,40 @@ public class RequestHandler implements Runnable {
}
}
private byte[] changeHeaderToByte(Map<String, List<String>> headerMap,
int responseLen) {
StringBuilder sb = new StringBuilder();
for (String key : headerMap.keySet()) {
if (key == null) {
sb.append(changeList2String(headerMap.get(key)) + "\r\n");
continue;
}
if (key.equals("Transfer-Encoding")) {
continue;
}
sb.append(key + ": " + changeList2String(headerMap.get(key))
+ "\r\n");
}
// change response length
if (headerMap.get("Content-Length") == null) {
sb.append("Content-Length: " + responseLen).append("\r\n");
}
sb.append("\r\n");
System.out.println(sb.toString());
return sb.toString().getBytes();
}
private String changeList2String(List<String> headers) {
if (headers == null)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < headers.size(); i++) {
sb.append(headers.get(i));
}
return sb.toString();
}
private int readRequestBody(byte[] buf) throws IOException {
int alreadyReadLength = 0;
int ch = -1;

View File

@ -1,17 +1,29 @@
package org.bench4q.recorder.httpcapture;
public class ResponseModel {
private byte[] response;
public byte[] getResponse() {
return response;
}
public void setResponse(byte[] response) {
this.response = response;
}
public ResponseModel(byte[] response){
this.response = response;
}
}
package org.bench4q.recorder.httpcapture;
import java.net.HttpURLConnection;
public class ResponseModel {
private byte[] response;
private HttpURLConnection conn;
public byte[] getResponse() {
return response;
}
public void setResponse(byte[] response) {
this.response = response;
}
public ResponseModel(byte[] response, HttpURLConnection conn) {
this.setConn(conn);
this.response = response;
}
public HttpURLConnection getConn() {
return conn;
}
public void setConn(HttpURLConnection conn) {
this.conn = conn;
}
}

View File

@ -335,7 +335,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
this.ignoreNextResponse = false;
return;
}
ResponseParser responseParser = new ResponseParser(response.getResponse());
ResponseParser responseParser = new ResponseParser(response);
ResponseHeader responseHeader = responseParser.getResponseHeader();
doAssertResponse(responseHeader.getRespCode());
if (responseHeader.isValidResponseHeader()) {
@ -403,7 +403,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
}
}
public abstract void doAssertResponse(String paramString)
public abstract void doAssertResponse(int paramString)
throws Utils.UserException;
public abstract void doCallUrl(Action action, String paramString2,

View File

@ -191,7 +191,7 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
params);
}
public void doAssertResponse(String respCode) {
public void doAssertResponse(int respCode) {
// TODO:Here I can tell if there are some incorrect request when
// record the script
}

View File

@ -12,18 +12,18 @@ public class ResponseHeader {
put("text/comma-separated-values", "text/comma-separated-values");
}
};
private String respCode;
private int respCode;
private String contentType;
private String charset;
private String contentEncoding;
private int contentLength;
private String transferEncoding;
public String getRespCode() {
public int getRespCode() {
return respCode;
}
public void setRespCode(String respCode) {
public void setRespCode(int respCode) {
this.respCode = respCode;
}
@ -36,7 +36,7 @@ public class ResponseHeader {
}
public String getCharset() {
return charset;
return charset == null ? "utf-8":charset;
}
public void setCharset(String charset) {
@ -62,7 +62,7 @@ public class ResponseHeader {
boolean isValidResponseHeader() {
return (getContentType() != null)
&& (MIME_TYPE.containsKey(getContentType()))
&& (getRespCode() != null);
&& (getRespCode() != -1);
}
boolean isHtmlContentType() {
@ -70,7 +70,8 @@ public class ResponseHeader {
}
boolean isGoodRequest() {
return this.getRespCode().startsWith("200");
return this.getRespCode() == 200;
// return this.getRespCode().startsWith("200");
}
public String getTransferEncoding() {

View File

@ -2,12 +2,17 @@ package org.bench4q.recorder.httpcapture.generator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.httpclient.ChunkedInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.log4j.Logger;
import org.bench4q.recorder.httpcapture.ResponseModel;
public class ResponseParser {
private Logger logger = Logger.getLogger(ResponseParser.class);
@ -39,23 +44,32 @@ public class ResponseParser {
this.responseBody = responseBody;
}
public ResponseParser(byte[] response) {
this.setResponse(response);
this.setResponseHeader(parseResponseHeader());
public ResponseParser(ResponseModel response) {
this.setResponse(response.getResponse());
this.setResponseHeader(parseResponseHeader(response.getConn()));
if (this.getResponseHeader().isValidResponseHeader()
&& this.getResponseHeader().isHtmlContentType()) {
this.parseResponseBody();
// this.parseResponseBody();
this.setResponseBody(new String(response.getResponse(),Charset.forName("utf-8")));
}
}
private ResponseHeader parseResponseHeader() {
private ResponseHeader parseResponseHeader(
HttpURLConnection httpURLConnection) {
ResponseHeader result = new ResponseHeader();
result.setCharset(parseCharset());
result.setContentEncoding(parseContentEncoding());
result.setContentLength(Integer.parseInt(parseContentLength()));
result.setContentType(parseContentType());
result.setRespCode(parseResponseCode());
result.setTransferEncoding(parseTransferEncoding());
if(httpURLConnection == null) return result;
result.setContentEncoding(httpURLConnection.getContentEncoding());
result.setContentLength(httpURLConnection.getContentLength());
try {
result.setRespCode(httpURLConnection.getResponseCode());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.setTransferEncoding(parseTransferEncoding(httpURLConnection));
result.setCharset(parseCharset(httpURLConnection));
result.setContentType(parseContentType(httpURLConnection));
return result;
}
@ -63,101 +77,132 @@ public class ResponseParser {
return respString.toLowerCase();
}
private String parseContentLength() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-length:");
if (pos > -1) {
pos += 15;
int end = respStr.indexOf("\r\n", pos);
if (end == -1) {
return "-1";
}
return respStr.substring(pos, end).trim();
private String parseContentLength(Map<String, List<String>> map) {
// String respStr = preprocess(new String(this.getResponse()));
// int pos = respStr.indexOf("content-length:");
// if (pos > -1) {
// pos += 15;
// int end = respStr.indexOf("\r\n", pos);
// if (end == -1) {
// return "-1";
// }
// return respStr.substring(pos, end).trim();
// }
// return "-1";
List<String> values = map.get("Content-Length:");
if (values != null) {
return values.get(0);
}
return "-1";
}
private String parseContentEncoding() {
String respStr = preprocess(new String(this.getResponse()));
int pos = respStr.indexOf("content-encoding:");
if (pos > -1) {
pos += 18;
int end = respStr.indexOf("\r\n", pos);
return respStr.substring(pos, end);
private String parseContentEncoding(Map<String, List<String>> map) {
// String respStr = preprocess(new String(this.getResponse()));
// int pos = respStr.indexOf("content-encoding:");
// if (pos > -1) {
// pos += 18;
// int end = respStr.indexOf("\r\n", pos);
// return respStr.substring(pos, end);
// }
String ret = "";
List<String> values = map.get("Content-Encoding:");
if (values != null) {
return values.get(0);
}
return null;
}
private String parseCharset() {
String response = preprocess(new String(this.getResponse()));
private String parseCharset(HttpURLConnection httpURLConnection) {
// String response = preprocess(new String(this.getResponse()));
// String ret = null;
// int pos = response.indexOf("content-type:");
// if (pos > -1) {
// pos += 14;
// int end = response.indexOf("\r\n", pos);
// int middle = response.indexOf(";", pos);
// if (middle > -1 && middle < end) {
// ret = response.substring(middle + 1, end);
// }
// }
// if (ret != null) {
// int begin = ret.indexOf("charset=");
// ret = ret.substring(begin + 8);
// }
String ret = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int middle = response.indexOf(";", pos);
if (middle > -1 && middle < end) {
ret = response.substring(middle + 1, end);
}
}
if (ret != null) {
int begin = ret.indexOf("charset=");
ret = ret.substring(begin + 8);
String value = httpURLConnection.getHeaderField("Content-Type");
int pos;
if (value != null && (pos = value.indexOf("charset=")) != -1) {
ret = value.substring(pos + 8);
}
return ret;
}
private String parseContentType() {
String response = preprocess(new String(this.getResponse()));
String contentType = null;
int pos = response.indexOf("content-type:");
if (pos > -1) {
pos += 14;
int end = response.indexOf("\r\n", pos);
int end2 = response.indexOf(";", pos);
if ((end2 > -1) && (end2 < end))
end = end2;
if (end > -1)
contentType = response.substring(pos, end).trim();
private String parseContentType(HttpURLConnection httpURLConnection) {
// String response = preprocess(new String(this.getResponse()));
// String contentType = null;
// int pos = response.indexOf("content-type:");
// if (pos > -1) {
// pos += 14;
// int end = response.indexOf("\r\n", pos);
// int end2 = response.indexOf(";", pos);
// if ((end2 > -1) && (end2 < end))
// end = end2;
// if (end > -1)
// contentType = response.substring(pos, end).trim();
//
// logger.debug(" Content-Type: " + contentType);
// } else {
// logger.debug(" No content-type header! First few lines:");
// StringTokenizer st = new StringTokenizer(response, "\n");
// int i = 0;
// while ((st.hasMoreTokens()) && (i < 5)) {
// logger.debug(st.nextToken());
// ++i;
// }
// }
// return contentType;
String ret = null;
String value = httpURLConnection.getHeaderField("Content-Type");
int pos;
if (value != null) {
if ((pos = value.indexOf(";")) != -1) {
ret = value.substring(0, pos);
} else {
logger.debug(" Content-Type: " + contentType);
} else {
logger.debug(" No content-type header! First few lines:");
StringTokenizer st = new StringTokenizer(response, "\n");
int i = 0;
while ((st.hasMoreTokens()) && (i < 5)) {
logger.debug(st.nextToken());
++i;
ret = value;
}
}
return contentType;
return ret;
}
private String parseResponseCode() {
String response = preprocess(new String(this.getResponse()));
String respCode = null;
int pos = response.indexOf(" ");
if (pos != -1) {
int end = response.indexOf(" ", pos + 1);
int end2 = response.indexOf("\n", pos + 1);
if ((end2 != -1) && (end2 < end))
end = end2;
if (end != -1)
respCode = response.substring(pos + 1, end).trim();
}
logger.debug("HTTP response code: " + respCode);
return respCode;
}
// private String parseResponseCode() {
// String response = preprocess(new String(this.getResponse()));
// String respCode = null;
// int pos = response.indexOf(" ");
// if (pos != -1) {
// int end = response.indexOf(" ", pos + 1);
// int end2 = response.indexOf("\n", pos + 1);
// if ((end2 != -1) && (end2 < end))
// end = end2;
// if (end != -1)
// respCode = response.substring(pos + 1, end).trim();
// }
// logger.debug("HTTP response code: " + respCode);
// return respCode;
// }
private String parseTransferEncoding() {
String response = preprocess(new String(this.getResponse()));
int pos = response.indexOf("transfer-encoding:");
if (pos > -1) {
pos += 19;
int end = response.indexOf("\r\n", pos);
return response.substring(pos, end);
}
return null;
private String parseTransferEncoding(HttpURLConnection httpURLConnection) {
// String response = preprocess(new String(this.getResponse()));
// int pos = response.indexOf("transfer-encoding:");
// if (pos > -1) {
// pos += 19;
// int end = response.indexOf("\r\n", pos);
// return response.substring(pos, end);
// }
// return null;
return httpURLConnection.getHeaderField("Transfer-Encoding:");
}
private byte[] buildResponseBody() {

View File

@ -12,38 +12,38 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class TestResponseParser {
private ResponseParser responseParser;
private static String FILE_SEPARATOR = System.getProperty("file.separator");
private ResponseParser getResponseParser() {
return responseParser;
}
private void setResponseParser(ResponseParser responseParser) {
this.responseParser = responseParser;
}
public TestResponseParser() throws IOException {
byte[] response = FileUtils.readFileToByteArray(new File(
"RecordScriptTestCase" + FILE_SEPARATOR
+ "gzipBaiduResponse.txt"));
this.setResponseParser(new ResponseParser(response));
}
@Test
public void testParseResponseHeader() throws Exception {
ResponseHeader parseResponseHeader = this.getResponseParser()
.getResponseHeader();
assertEquals(parseResponseHeader.getContentLength(), 12852);
assertEquals(parseResponseHeader.getCharset(), "utf-8");
assertEquals(parseResponseHeader.getContentEncoding(), "gzip");
assertEquals(parseResponseHeader.getContentType(), "text/html");
assertEquals(parseResponseHeader.getRespCode(), "200");
}
@Test
public void testParseResponseBody() throws UnsupportedEncodingException {
String responseBody = this.getResponseParser().getResponseBody();
assertTrue(responseBody.indexOf("我的") > -1);
}
// private ResponseParser responseParser;
// private static String FILE_SEPARATOR = System.getProperty("file.separator");
//
// private ResponseParser getResponseParser() {
// return responseParser;
// }
//
// private void setResponseParser(ResponseParser responseParser) {
// this.responseParser = responseParser;
// }
//
// public TestResponseParser() throws IOException {
// byte[] response = FileUtils.readFileToByteArray(new File(
// "RecordScriptTestCase" + FILE_SEPARATOR
// + "gzipBaiduResponse.txt"));
// this.setResponseParser(new ResponseParser(response));
// }
//
// @Test
// public void testParseResponseHeader() throws Exception {
// ResponseHeader parseResponseHeader = this.getResponseParser()
// .getResponseHeader();
// assertEquals(parseResponseHeader.getContentLength(), 12852);
// assertEquals(parseResponseHeader.getCharset(), "utf-8");
// assertEquals(parseResponseHeader.getContentEncoding(), "gzip");
// assertEquals(parseResponseHeader.getContentType(), "text/html");
// assertEquals(parseResponseHeader.getRespCode(), "200");
// }
//
// @Test
// public void testParseResponseBody() throws UnsupportedEncodingException {
// String responseBody = this.getResponseParser().getResponseBody();
// assertTrue(responseBody.indexOf("我的") > -1);
// }
}

View File

@ -33,166 +33,166 @@ public class Test_Bench4QCodeGenerator extends TestRecordBase {
this.scriptAdapter);
public static String dealWithSerie = "RecordScriptTestCase/gzip/gzipBaiduIR";
@Test
public void testProcessResponseWithHtmlContentTypeWithNullResponseBody()
throws FileNotFoundException, IOException, Exception {
IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(
makeAHeader("www.makeup.com"),
new byte[0],
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NullResponseBody.html"))));
assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
}
@Test
public void testProcessResponseOnceWithHtmlResponseButHaveNoBody()
throws Exception, IOException, FileNotFoundException {
IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(
makeAHeader("www.makeup.com"),
new byte[0],
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "BlankBody.html"))));
assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
}
@Test
public void testProcessResponseWithOneHtmlResponseHaveBody()
throws Exception {
HttpRequestHeader header = makeAHeader("ir.baidu.com");
IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(
header,
new byte[0],
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "Response.txt"))));
assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
}
private HttpRequestHeader makeAHeader(String url) throws IOException,
FileNotFoundException {
HttpRequestHeader header = new HttpRequestHeader(new FileInputStream(
new File(dealWithSerie + "Request.txt")));
header.url = url;
return header;
}
@Test
public void testProcessResponseTwiceWithTwoHtmlResponseButTheSecondHaveNoBody()
throws Exception {
// testProcessResponseWithOneHtmlResponseHaveBody();
// testProcessResponseOnceWithHtmlResponseButHaveNoBody();
// assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
}
@Test
public void testProcessResponseTwiceWithTwoHtmlResponseOneHtmlSecondNot()
throws Exception {
testProcessResponseWithOneHtmlResponseHaveBody();
testProcessResponseWithNotHtmlResponse();
assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
}
@Test
public void testProcessResponseWithTwoHtmlResponseFirstNoBodySecondHave()
throws FileNotFoundException, IOException, Exception {
testProcessResponseOnceWithHtmlResponseButHaveNoBody();
testProcessResponseWithOneHtmlResponseHaveBody();
}
private void testProcessResponseWithNotHtmlResponse() throws Exception,
IOException, FileNotFoundException {
IScriptGenerator scriptGenerator = this.codeGenerator;
scriptGenerator.processResponse(
makeAHeader("www.makeup2.com"),
new byte[0],
new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
+ "NotHtmlResponse.html"))));
}
@Test
public void testParseContentLength() throws IOException {
int contentLength = new ResponseParser(
FileUtils.readFileToByteArray(new File(dealWithSerie
+ "Response.txt"))).getResponseHeader()
.getContentLength();
assertTrue(contentLength == 5023);
}
@Test
public void testGetContentBody() throws IOException {
String contentFromFile = FileUtils.readFileToString(new File(
dealWithSerie + "ResponseBody.txt"), "ISO-8859-1");
byte[] contentBody1 = contentFromFile.getBytes("ISO-8859-1");
String responseString = new String(contentBody1,
Charset.forName("ISO-8859-1"));
System.out.println("testGetContentBody total length:"
+ responseString.getBytes("ISO-8859-1").length);
System.out.println("testGetContentBody :" + contentBody1.length);
GZIPInputStream inputStream = new GZIPInputStream(
new ByteArrayInputStream(contentBody1));
assertEquals(responseString.getBytes("ISO-8859-1").length,
contentBody1.length);
assertTrue(inputStream != null);
}
@Test
public void testUncompressGzipContent() throws IOException {
ContentDecoder contentDecoder = ContentDecoder.createDecoder("gzip");
FileInputStream inputStream = new FileInputStream(new File(
dealWithSerie + "ResponseBody.txt"));
String contentString = new String(
contentDecoder.decodeContent(inputStream),
Charset.forName("utf-8"));
System.out.println(contentString);
assertTrue(contentString.indexOf("<html") > -1);
}
@Test
public void testFileInputStreamAndFileUtils() throws IOException {
byte[] responseBodyBytes = FileUtils.readFileToString(
new File(dealWithSerie + "ResponseBody.txt"), "UTF-8")
.getBytes("UTF-8");
int length1 = responseBodyBytes.length;
String responseBodyString = new String(responseBodyBytes, "UTF-8");
int length2 = responseBodyString.getBytes("UTF-8").length;
assertEquals(length1, length2);
System.out.println(length1);
}
@Test
public void testCreateGetBehavior() throws UserException {
this.codeGenerator.doQueryStringParameterList(new Param[] { Param
.createParam("a", "b") });
this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
"token", "Bearer") });
this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
"a", "b") });
BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
"http://www.baidu.com", "Get");
assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
assertEquals("header=token|value=Bearer|;", behaviorModel
.getParameters().get(2).getValue());
assertEquals(3, behaviorModel.getParameters().size());
}
public void testCreatePostBehavior() throws UserException {
this.codeGenerator.doQueryStringParameterList(new Param[] { Param
.createParam("a", "b") });
this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
"token", "Bearer") });
this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
"a", "b") });
this.codeGenerator.doSetData("ava");
BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
"http://www.baidu.com", "Post");
assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
assertEquals("header=token|value=Bearer|;", behaviorModel
.getParameters().get(2).getValue());
assertEquals(5, behaviorModel.getParameters().size());
assertEquals("ava", behaviorModel.getParameters().get(3).getValue());
assertEquals("a=b", behaviorModel.getParameters().get(4).getValue());
}
// @Test
// public void testProcessResponseWithHtmlContentTypeWithNullResponseBody()
// throws FileNotFoundException, IOException, Exception {
// IScriptGenerator scriptGenerator = this.codeGenerator;
// scriptGenerator.processResponse(
// makeAHeader("www.makeup.com"),
// new byte[0],
// new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
// + "NullResponseBody.html"))));
// assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
// }
//
// @Test
// public void testProcessResponseOnceWithHtmlResponseButHaveNoBody()
// throws Exception, IOException, FileNotFoundException {
// IScriptGenerator scriptGenerator = this.codeGenerator;
// scriptGenerator.processResponse(
// makeAHeader("www.makeup.com"),
// new byte[0],
// new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
// + "BlankBody.html"))));
// assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
// }
//
// @Test
// public void testProcessResponseWithOneHtmlResponseHaveBody()
// throws Exception {
// HttpRequestHeader header = makeAHeader("ir.baidu.com");
// IScriptGenerator scriptGenerator = this.codeGenerator;
// scriptGenerator.processResponse(
// header,
// new byte[0],
// new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
// + "Response.txt"))));
// assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
// }
//
// private HttpRequestHeader makeAHeader(String url) throws IOException,
// FileNotFoundException {
// HttpRequestHeader header = new HttpRequestHeader(new FileInputStream(
// new File(dealWithSerie + "Request.txt")));
// header.url = url;
// return header;
// }
//
// @Test
// public void testProcessResponseTwiceWithTwoHtmlResponseButTheSecondHaveNoBody()
// throws Exception {
// // testProcessResponseWithOneHtmlResponseHaveBody();
// // testProcessResponseOnceWithHtmlResponseButHaveNoBody();
// // assertEquals(0, this.scriptAdapter.getChildrenUrls().size());
// }
//
// @Test
// public void testProcessResponseTwiceWithTwoHtmlResponseOneHtmlSecondNot()
// throws Exception {
// testProcessResponseWithOneHtmlResponseHaveBody();
// testProcessResponseWithNotHtmlResponse();
// assertEquals(11, this.scriptAdapter.getChildrenUrls().size());
// }
//
// @Test
// public void testProcessResponseWithTwoHtmlResponseFirstNoBodySecondHave()
// throws FileNotFoundException, IOException, Exception {
// testProcessResponseOnceWithHtmlResponseButHaveNoBody();
// testProcessResponseWithOneHtmlResponseHaveBody();
//
// }
//
// private void testProcessResponseWithNotHtmlResponse() throws Exception,
// IOException, FileNotFoundException {
// IScriptGenerator scriptGenerator = this.codeGenerator;
// scriptGenerator.processResponse(
// makeAHeader("www.makeup2.com"),
// new byte[0],
// new ResponseModel(FileUtils.readFileToByteArray(new File(dealWithSerie
// + "NotHtmlResponse.html"))));
// }
//
// @Test
// public void testParseContentLength() throws IOException {
// int contentLength = new ResponseParser(
// FileUtils.readFileToByteArray(new File(dealWithSerie
// + "Response.txt"))).getResponseHeader()
// .getContentLength();
// assertTrue(contentLength == 5023);
// }
//
// @Test
// public void testGetContentBody() throws IOException {
// String contentFromFile = FileUtils.readFileToString(new File(
// dealWithSerie + "ResponseBody.txt"), "ISO-8859-1");
// byte[] contentBody1 = contentFromFile.getBytes("ISO-8859-1");
// String responseString = new String(contentBody1,
// Charset.forName("ISO-8859-1"));
// System.out.println("testGetContentBody total length:"
// + responseString.getBytes("ISO-8859-1").length);
// System.out.println("testGetContentBody :" + contentBody1.length);
// GZIPInputStream inputStream = new GZIPInputStream(
// new ByteArrayInputStream(contentBody1));
// assertEquals(responseString.getBytes("ISO-8859-1").length,
// contentBody1.length);
// assertTrue(inputStream != null);
// }
//
// @Test
// public void testUncompressGzipContent() throws IOException {
// ContentDecoder contentDecoder = ContentDecoder.createDecoder("gzip");
// FileInputStream inputStream = new FileInputStream(new File(
// dealWithSerie + "ResponseBody.txt"));
// String contentString = new String(
// contentDecoder.decodeContent(inputStream),
// Charset.forName("utf-8"));
// System.out.println(contentString);
// assertTrue(contentString.indexOf("<html") > -1);
// }
//
// @Test
// public void testFileInputStreamAndFileUtils() throws IOException {
// byte[] responseBodyBytes = FileUtils.readFileToString(
// new File(dealWithSerie + "ResponseBody.txt"), "UTF-8")
// .getBytes("UTF-8");
// int length1 = responseBodyBytes.length;
// String responseBodyString = new String(responseBodyBytes, "UTF-8");
// int length2 = responseBodyString.getBytes("UTF-8").length;
// assertEquals(length1, length2);
// System.out.println(length1);
// }
//
// @Test
// public void testCreateGetBehavior() throws UserException {
// this.codeGenerator.doQueryStringParameterList(new Param[] { Param
// .createParam("a", "b") });
// this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
// "token", "Bearer") });
// this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
// "a", "b") });
// BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
// "http://www.baidu.com", "Get");
// assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
// assertEquals("header=token|value=Bearer|;", behaviorModel
// .getParameters().get(2).getValue());
// assertEquals(3, behaviorModel.getParameters().size());
// }
//
// public void testCreatePostBehavior() throws UserException {
// this.codeGenerator.doQueryStringParameterList(new Param[] { Param
// .createParam("a", "b") });
// this.codeGenerator.doHeaders(new HeaderValue[] { new HeaderValue(
// "token", "Bearer") });
// this.codeGenerator.doBodyParameterList(new Param[] { Param.createParam(
// "a", "b") });
// this.codeGenerator.doSetData("ava");
// BehaviorModel behaviorModel = this.codeGenerator.createHttpBehavior(1,
// "http://www.baidu.com", "Post");
// assertEquals("a=b", behaviorModel.getParameters().get(1).getValue());
// assertEquals("header=token|value=Bearer|;", behaviorModel
// .getParameters().get(2).getValue());
// assertEquals(5, behaviorModel.getParameters().size());
// assertEquals("ava", behaviorModel.getParameters().get(3).getValue());
// assertEquals("a=b", behaviorModel.getParameters().get(4).getValue());
// }
}

View File

@ -1,6 +1,7 @@
package org.bench4q.recorder;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
@ -8,8 +9,15 @@ import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.bench4q.recorder.httpcapture.Bench4qTestScriptAdapter;
import org.bench4q.recorder.httpcapture.IScriptAdapter;
import org.bench4q.recorder.httpcapture.ProxyServer;
import org.bench4q.recorder.httpcapture.RequestHandler;
import org.bench4q.recorder.httpcapture.generator.Bench4qCodeGenerator;
import org.bench4q.recorder.httpcapture.generator.ChildrenUrl;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.junit.Test;
public class Test_RequestHandler {
@ -19,6 +27,9 @@ public class Test_RequestHandler {
public void test() {
try {
ProxyServer proxyServer = new ProxyServer(PORT);
Bench4qCodeGenerator observer = new Bench4qCodeGenerator(
new Bench4qTestScriptAdapter(new RunScenarioModel()));
proxyServer.addObserver(observer);
RequestHandler requestHandler = new RequestHandler(proxyServer,
proxyServer.getServerSocket().accept());
requestHandler.run();
@ -45,6 +56,8 @@ public class Test_RequestHandler {
get.setURI(new URI("http://www.baidu.com"));
int statusCode = this.httpClient.executeMethod(get);
System.out.println(statusCode);
System.out.println(new String(get.getResponseBody(), Charset
.forName("utf-8")));
} catch (URIException e) {
// TODO Auto-generated catch block
e.printStackTrace();