we can do the unit test like this for the proxy server

we can do the unit test like this for the proxy server
This commit is contained in:
coderfengyun 2014-08-27 14:28:18 +08:00
parent 243d73bb21
commit cf9219b73b
2 changed files with 69 additions and 7 deletions

View File

@ -41,8 +41,9 @@ public class RequestHandler implements Runnable {
try {
this.header = new HttpRequestHeader(this.clientIn);
} catch (IOException e) {
if(this.header != null){
log.error("truncated request from browser: " + this.header.url, e);
if (this.header != null) {
log.error("truncated request from browser: " + this.header.url,
e);
System.out.println(this.header.url);
}
throw new Utils.SilentException();
@ -51,7 +52,7 @@ public class RequestHandler implements Runnable {
throw new Utils.UserException(
"Bench4Q only supports the HTTP protocol.");
URL url = new URL(this.header.url);
System.out.println(this.header.url);
Config.ProxySettings proxy = this.config.getProxySettings();
int port;
String host;
@ -161,18 +162,19 @@ public class RequestHandler implements Runnable {
int len;
while ((len = this.serverIn.read(buf, 0, buf.length)) >= 0) {
log.trace("read " + Integer.toString(len));
this.buffer.write(buf, 0, len);
}
log.trace("transferred response len:" + len);
ResponseModel responseModel = new ResponseModel(this.buffer.toByteArray());
ResponseModel responseModel = new ResponseModel(
this.buffer.toByteArray());
this.proxyServer.processResponse(this.header, requestBody,
responseModel);
try {
this.clientOut.write(responseModel.getResponse());
} catch (SocketException e) {
log.info("browser stopped listening: "
+ e.getMessage() + this.buffer.toString());
log.info("browser stopped listening: " + e.getMessage()
+ this.buffer.toString());
throw new Utils.SilentException();
}
log.trace("processed response");

View File

@ -0,0 +1,60 @@
package org.bench4q.recorder;
import java.io.IOException;
import java.net.ServerSocket;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
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.ProxyServer;
import org.bench4q.recorder.httpcapture.RequestHandler;
import org.junit.Test;
public class Test_RequestHandler {
private static final int PORT = 8910;
@Test
public void test() {
try {
ProxyServer proxyServer = new ProxyServer(PORT);
RequestHandler requestHandler = new RequestHandler(proxyServer,
proxyServer.getServerSocket().accept());
requestHandler.run();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test2() {
new SendRequest().run();
}
private static class SendRequest implements Runnable {
private HttpClient httpClient = new HttpClient();
public void run() {
HostConfiguration hostConfiguration = new HostConfiguration();
hostConfiguration.setProxy("127.0.0.1", PORT);
httpClient.setHostConfiguration(hostConfiguration);
GetMethod get = new GetMethod();
try {
get.setURI(new URI("http://www.baidu.com"));
int statusCode = this.httpClient.executeMethod(get);
System.out.println(statusCode);
} catch (URIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}