add new test and new class

This commit is contained in:
coderfengyun 2013-12-18 20:17:17 +08:00
parent 51d0bab4d9
commit 0371a636b6
4 changed files with 86 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@ -169,6 +170,11 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
public void processRequest(HttpRequestHeader header, byte[] requestBody)
throws Exception {
// doParseRequest(header, requestBody);
}
private void doParseRequest(HttpRequestHeader header, byte[] requestBody)
throws UserException, UnsupportedEncodingException {
Param[] params;
int i;
String name;
@ -285,11 +291,10 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
method = method + "MultiPart";
data_str = ", data";
cont_len_str = ", " + header.contentLength;
doSetData(action.getMultiPartData());
}
this.requestCount++;
doCallUrl(action, method, data_str, cont_len_str);
this.requestCount++;
}
private String doForCharset(String contentType) throws UserException {
@ -329,8 +334,9 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
this.ignoreNextResponse = false;
return;
}
ResponseParser parser = new ResponseParser();
String respStr = new String(response).toLowerCase();
ResponseParser parser = new ResponseParser(response);
String respCode = parser.parseResponseCode(respStr);
String contentType = parser.parseContentType(respStr);
String charset = parser.parseCharset(respStr);
@ -367,6 +373,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
+ contentType);
}
doParseRequest(header, requestBody);
}
// TODO: edit this type
@ -385,9 +392,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator,
behaviorModel);
}
}
}
} catch (InterruptedException e) {
log.trace("Thread dead");
}

View File

@ -7,6 +7,43 @@ import org.apache.log4j.Logger;
public class ResponseParser {
private Logger logger = Logger.getLogger(ResponseParser.class);
private byte[] response;
private String responseHeader;
private String responseBody;
private byte[] getResponse() {
return response;
}
private void setResponse(byte[] response) {
this.response = response;
}
private String getResponseHeader() {
return responseHeader;
}
private void setResponseHeader(String responseHeader) {
this.responseHeader = responseHeader;
}
private String getResponseBody() {
return responseBody;
}
private void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}
public ResponseParser(byte[] response) {
this.setResponse(response);
init();
}
private void init() {
String responseHeader = new String(this.getResponse());
this.setResponseHeader(responseHeader);
}
private String preprocess(String respString) {
return respString.toLowerCase();

View File

@ -53,7 +53,7 @@ public class TestCodeGenerator extends TestRecordBase {
@Test
public void testGetContentBodyWithoutCompress() throws IOException {
String contentString = new String(
new ResponseParser().parseReponseBody(FileUtils
new ResponseParser(new byte[0]).parseReponseBody(FileUtils
.readFileToString(new File(
"RecordScriptTestCase/simpleResponse.txt"))));
assertTrue(contentString.indexOf("<html") == 0);
@ -62,7 +62,7 @@ public class TestCodeGenerator extends TestRecordBase {
@Test
public void testParseContentLength() throws IOException {
String contentLengthString = new ResponseParser()
String contentLengthString = new ResponseParser(new byte[0])
.parseContentLength(FileUtils.readFileToString(
new File(dealWithSerie + "Response.txt")).toLowerCase());
int contentLength = Integer.parseInt(contentLengthString);

View File

@ -0,0 +1,37 @@
package org.bench4q.master.test.recordscript;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.bench4q.master.scriptrecord.httpcapture.generator.ResponseParser;
import org.bench4q.share.helper.TestHelper;
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 {
this.setResponseParser(new ResponseParser(FileUtils
.readFileToByteArray(new File("RecordScriptTestCase"
+ FILE_SEPARATOR + "gzipBaiduResponse.txt"))));
}
@Test
public void testInit() throws Exception {
TestHelper.invokePrivateMethod(this.getResponseParser().getClass(),
this.getResponseParser(), "init", null, null);
assertTrue(this.getResponseParser() != null);
}
}