This commit is contained in:
coderfengyun 2013-12-18 15:35:48 +08:00
parent cf30603c7a
commit 1289751bc3
4 changed files with 123 additions and 187 deletions

View File

@ -3,207 +3,145 @@ package org.bench4q.master.scriptrecord.httpcapture;
import java.util.Vector; import java.util.Vector;
public class Action { public class Action {
private static String LINE_SEP = System.getProperty("line.separator"); private String url;
private String url; private String method;
private String method; private String expected_result;
private String expected_result; private int delayTime;
private int delayTime; private int expected_size;
private int expected_size; private int timeout;
private int timeout; private Vector<HeaderValue> headers;
private Vector<HeaderValue> headers; private Vector<Param> queryStringParams;
private Vector<Param> queryStringParams; private Vector<Param> bodyParams;
private Vector<Param> bodyParams; private String multiPartData;
private String multiPartData;
public Action() public Action() {
{ this.headers = new Vector<HeaderValue>();
this.headers = new Vector<HeaderValue>(); this.queryStringParams = new Vector<Param>();
this.queryStringParams = new Vector<Param>(); this.bodyParams = new Vector<Param>();
this.bodyParams = new Vector<Param>(); this.expected_result = "";
this.expected_result = ""; this.multiPartData = "";
this.multiPartData = ""; }
}
public void setUrl(String url) public void setUrl(String url) {
{ this.url = url;
this.url = url; }
}
public String getUrl() public String getUrl() {
{ return this.url;
return this.url; }
}
public void setMethod(String method) public void setMethod(String method) {
{ this.method = method;
this.method = method; }
}
public String getMethod() public String getMethod() {
{ return this.method;
return this.method; }
}
public void setExpectedResult(String str) public void setExpectedResult(String str) {
{ this.expected_result = str;
this.expected_result = str; }
}
public String getExpectedResult() public String getExpectedResult() {
{ return this.expected_result;
return this.expected_result; }
}
public void setDelayTime(int seconds) public void setDelayTime(int seconds) {
{ this.delayTime = seconds;
this.delayTime = seconds; }
}
public int getDelayTime() public int getDelayTime() {
{ return this.delayTime;
return this.delayTime; }
}
public void setExpectedSize(int bytes) public void setExpectedSize(int bytes) {
{ this.expected_size = bytes;
this.expected_size = bytes; }
}
public int getExpectedSize() public int getExpectedSize() {
{ return this.expected_size;
return this.expected_size; }
}
public void setTimeout(int seconds) public void setTimeout(int seconds) {
{ this.timeout = seconds;
this.timeout = seconds; }
}
public int getTimeout() public int getTimeout() {
{ return this.timeout;
return this.timeout; }
}
public int getHeaderCount() public int getHeaderCount() {
{ return this.headers.size();
return this.headers.size(); }
}
public HeaderValue[] getHeaders() public HeaderValue[] getHeaders() {
{ HeaderValue[] list = new HeaderValue[this.headers.size()];
HeaderValue[] list = new HeaderValue[this.headers.size()]; this.headers.copyInto(list);
this.headers.copyInto(list); return list;
return list; }
}
public void addHeader(HeaderValue p) public void addHeader(HeaderValue p) {
{ this.headers.addElement(p);
this.headers.addElement(p); }
}
public void removeHeader(HeaderValue p) public void removeHeader(HeaderValue p) {
{ this.headers.removeElement(p);
this.headers.removeElement(p); }
}
public int getParamsCount() public int getParamsCount() {
{ return getParams().length;
return getParams().length; }
}
public int getQueryStringParamCount() public int getQueryStringParamCount() {
{ return this.queryStringParams.size();
return this.queryStringParams.size(); }
}
public int getBodyParamCount() public int getBodyParamCount() {
{ return this.bodyParams.size();
return this.bodyParams.size(); }
}
public Param[] getQueryStringParams() public Param[] getQueryStringParams() {
{ Param[] list = new Param[this.queryStringParams.size()];
Param[] list = new Param[this.queryStringParams.size()]; this.queryStringParams.copyInto(list);
this.queryStringParams.copyInto(list); return list;
return list; }
}
public Param[] getBodyParams() public Param[] getBodyParams() {
{ Param[] list = new Param[this.bodyParams.size()];
Param[] list = new Param[this.bodyParams.size()]; this.bodyParams.copyInto(list);
this.bodyParams.copyInto(list); return list;
return list; }
}
public Param[] getParams() public Param[] getParams() {
{ @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") Vector<Param> v = (Vector<Param>) this.queryStringParams.clone();
Vector<Param> v = (Vector<Param>)this.queryStringParams.clone(); v.addAll(this.bodyParams);
v.addAll(this.bodyParams); Param[] list = new Param[v.size()];
Param[] list = new Param[v.size()]; v.copyInto(list);
v.copyInto(list); return list;
return list; }
}
public void addQueryStringParam(Param p) public void addQueryStringParam(Param p) {
{ this.queryStringParams.addElement(p);
this.queryStringParams.addElement(p); }
}
public void addBodyParam(Param p) public void addBodyParam(Param p) {
{ this.bodyParams.addElement(p);
this.bodyParams.addElement(p); }
}
public void removeQueryStringParam(Param p) public void removeQueryStringParam(Param p) {
{ this.queryStringParams.removeElement(p);
this.queryStringParams.removeElement(p); }
}
public void removeBodyParam(Param p) public void removeBodyParam(Param p) {
{ this.bodyParams.removeElement(p);
this.bodyParams.removeElement(p); }
}
public String toXML() public void setMultiPartData(String multiPartData) {
{ this.multiPartData = multiPartData;
StringBuffer str = new StringBuffer(1024); }
str.append(" ");
str.append("<action url=\"").append(this.url).append("\" ");
str.append(LINE_SEP).append(" ");
str.append("method=\"").append(this.method).append("\" ");
str.append(LINE_SEP).append(" ");
str.append("delayTime=\"").append(this.delayTime).append("\" ");
str.append(LINE_SEP).append(" ");
str.append("expected-result=\"").append(this.expected_result).append("\"");
str.append(LINE_SEP).append(" ");
str.append("expected-result-size=\"").append(this.expected_size).append("\"");
str.append(LINE_SEP).append(" ");
str.append("timeout=\"").append(this.timeout).append("\">");
Param[] params = getQueryStringParams();
for (int i = 0; i < params.length; ++i) {
str.append(LINE_SEP).append(" ");
str.append("<param name=\"").append(params[i].name);
str.append("\" value=\"").append(params[i].value).append("\"/>");
}
params = getBodyParams();
for (int i = 0; i < params.length; ++i) {
str.append(LINE_SEP).append(" ");
str.append("<param name=\"").append(params[i].name);
str.append("\" value=\"").append(params[i].value).append("\"/>");
}
str.append(LINE_SEP);
str.append(" </action>");
return str.toString();
}
public void setMultiPartData(String multiPartData) public String getMultiPartData() {
{ return this.multiPartData;
this.multiPartData = multiPartData; }
}
public String getMultiPartData()
{
return this.multiPartData;
}
} }

View File

@ -205,10 +205,9 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
doSetData(new String(requestBody, newCharset)); doSetData(new String(requestBody, newCharset));
else else
doSetData(new String(requestBody)); doSetData(new String(requestBody));
} else if (newCharset != null) {
} else if (newCharset != null)
setBodyParams(action, new String(requestBody, newCharset)); setBodyParams(action, new String(requestBody, newCharset));
else { } else {
setBodyParams(action, new String(requestBody)); setBodyParams(action, new String(requestBody));
} }
@ -216,7 +215,6 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
if (action.getParamsCount() > 0) { if (action.getParamsCount() > 0) {
params = action.getParams(); params = action.getParams();
for (i = 0; i < params.length; ++i) { for (i = 0; i < params.length; ++i) {
name = params[i].name; name = params[i].name;
value = params[i].value; value = params[i].value;
@ -277,7 +275,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
} }
this.requestCount++; this.requestCount++;
doCallUrl(action.getUrl(), method, data_str, cont_len_str); doCallUrl(action, method, data_str, cont_len_str);
} }
private String doForCharset(String contentType) throws UserException { private String doForCharset(String contentType) throws UserException {
@ -385,7 +383,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
public abstract void doAssertResponse(String paramString) public abstract void doAssertResponse(String paramString)
throws Utils.UserException; throws Utils.UserException;
public abstract void doCallUrl(String paramString1, String paramString2, public abstract void doCallUrl(Action action, String paramString2,
String paramString3, String paramString4) String paramString3, String paramString4)
throws Utils.UserException; throws Utils.UserException;

View File

@ -8,6 +8,7 @@ import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.scriptrecord.httpcapture.Action;
import org.bench4q.master.scriptrecord.httpcapture.Config; import org.bench4q.master.scriptrecord.httpcapture.Config;
import org.bench4q.master.scriptrecord.httpcapture.HeaderValue; import org.bench4q.master.scriptrecord.httpcapture.HeaderValue;
import org.bench4q.master.scriptrecord.httpcapture.IScriptAdapter; import org.bench4q.master.scriptrecord.httpcapture.IScriptAdapter;
@ -154,13 +155,12 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
public void doSetData(String data) { public void doSetData(String data) {
} }
public void doCallUrl(String url, String method, String data, public void doCallUrl(Action action, String method, String data,
String contentLength) throws Utils.UserException { String contentLength) throws Utils.UserException {
if ((!isFirstRequest()) if (!isFirstRequest())
&& (!this.getScriptAdapter().isInChildrenUrl(url)))
doInsertDelay(getTimeElapsedSinceLastestRequest()); doInsertDelay(getTimeElapsedSinceLastestRequest());
UserBehaviorModel userBehavior = createHttpBehavior( UserBehaviorModel userBehavior = createHttpBehavior(
this.getRequestCount(), url, method); this.getRequestCount(), action.getUrl(), method);
insertBehavior(userBehavior); insertBehavior(userBehavior);
this.queryStringParameters = ""; this.queryStringParameters = "";
@ -191,7 +191,6 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator {
userBehavior.setName(method); userBehavior.setName(method);
userBehavior.setUse("http"); userBehavior.setUse("http");
userBehavior.setParameters(params); userBehavior.setParameters(params);
return userBehavior; return userBehavior;
} }

View File

@ -25,7 +25,7 @@ public class TestCodeGenerator extends TestRecordBase {
new RunScenarioModel()); new RunScenarioModel());
private Bench4qCodeGenerator codeGenerator = new Bench4qCodeGenerator( private Bench4qCodeGenerator codeGenerator = new Bench4qCodeGenerator(
this.scriptAdapter); this.scriptAdapter);
private static String dealWithSerie = "Script/gzip/gzipBaiduIR"; private static String dealWithSerie = "RecordScriptTestCase/gzip/gzipBaiduIR";
@Test @Test
public void testProcessResponse() throws Exception { public void testProcessResponse() throws Exception {
@ -53,7 +53,8 @@ public class TestCodeGenerator extends TestRecordBase {
public void testGetContentBodyWithoutCompress() throws IOException { public void testGetContentBodyWithoutCompress() throws IOException {
String contentString = new String( String contentString = new String(
new ResponseParser().parseReponseBody(FileUtils new ResponseParser().parseReponseBody(FileUtils
.readFileToString(new File("Script/simpleResponse.txt")))); .readFileToString(new File(
"RecordScriptTestCase/simpleResponse.txt"))));
assertTrue(contentString.indexOf("<html") == 0); assertTrue(contentString.indexOf("<html") == 0);
assertTrue(contentString.length() == 532); assertTrue(contentString.length() == 532);
} }