Skip to content

Commit

Permalink
Test sending a file. Turns out we need commons-io in this case.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmiley committed Oct 5, 2011
1 parent cedc33a commit bc4aa97
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
1 change: 1 addition & 0 deletions httpproxy.iml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.4" level="project" />
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.2.2" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:1.3.2" level="project" />
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.2" level="project" />
Expand Down
12 changes: 12 additions & 0 deletions httpproxy.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,17 @@
<root url="jar://$M2_REPO$/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: commons-io:commons-io:1.3.2">
<CLASSES>
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: commons-logging:commons-logging:1.0.4">
<CLASSES>
<root url="jar://$M2_REPO$/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar!/" />
Expand Down Expand Up @@ -972,6 +983,7 @@
</JAVADOC>
<SOURCES>
<root url="jar://$M2_REPO$/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2-test-sources.jar!/" />
<root url="intellijad://intellijad" />
</SOURCES>
</library>
<library name="Maven: org.apache.httpcomponents:httpcore:4.1.2">
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Only when sending a file -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
Expand Down
88 changes: 74 additions & 14 deletions src/test/java/org/mitre/dsmiley/httpproxy/ProxyServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import com.meterware.httpunit.WebResponse;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import org.apache.http.localserver.EchoHandler;
import org.apache.http.*;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.localserver.LocalTestServer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.*;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
Expand All @@ -33,11 +37,13 @@ public class ProxyServletTest

/** From Meterware httpunit. */
private ServletRunner servletRunner;
private ServletUnitClient sc;

@Before
public void setUp() throws Exception {
localTestServer = new LocalTestServer(null, null);
localTestServer.start();
localTestServer.register("/targetPath*",new RequestInfoHandler());//matches /targetPath and /targetPath/blahblah

servletRunner = new ServletRunner();
Properties params = new Properties();
Expand All @@ -46,6 +52,7 @@ public void setUp() throws Exception {
params.setProperty(ProxyServlet.P_PROXY_PATH, "/targetPath");//dummy
params.setProperty(ProxyServlet.P_LOG, "true");
servletRunner.registerServlet("/proxyMe/*", ProxyServlet.class.getName(), params);//also matches /proxyMe (no path info)
sc = servletRunner.newClient();
}

@After
Expand All @@ -55,23 +62,76 @@ public void tearDown() throws Exception {
}

@Test
public void testDoGet() throws Exception {
localTestServer.register("/targetPath*",new EchoHandler());//matches /targetPath and /targetPath/blahblah
public void test() throws Exception {
execGetAndAssert(new GetMethodWebRequest("http://localhost/proxyMe"));
execGetAndAssert(new GetMethodWebRequest("http://localhost/proxyMe/"));

ServletUnitClient sc = servletRunner.newClient();
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe"));
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe/pathInfo"));
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe?def=DEF"));
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe/pathInfo?def=DEF"));
}

assertEquals("", sc.getResponse( new GetMethodWebRequest( "http://localhost/proxyMe" ) ).getText());
assertEquals("", sc.getResponse( new GetMethodWebRequest( "http://localhost/proxyMe/" ) ).getText());
@Test
public void testSendFile() throws Exception {
final PostMethodWebRequest request = new PostMethodWebRequest("http://localhost/proxyMe",true);//true: mime encoded
InputStream data = new ByteArrayInputStream("testFileData".getBytes("UTF-8"));
request.selectFile("fileNameParam", "fileName", data, "text/plain");
WebResponse rsp = execPostAndAssert(request);
assertTrue(rsp.getText().contains("Content-Type: multipart/form-data; boundary="));
}

testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe" ));
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe/pathInfo" ));
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe?def=DEF" ));
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe/pathInfo?def=DEF" ));
private WebResponse execGetAndAssert(GetMethodWebRequest request) throws IOException, SAXException {
WebResponse rsp = execAndAssert(request);
//TODO //no assertions for GET but a failure should throw
return rsp;
}

private void testEchoRequest(ServletUnitClient sc, WebRequest request) throws IOException, SAXException {
private WebResponse execPostAndAssert(PostMethodWebRequest request) throws IOException, SAXException {
request.setParameter("abc","ABC");
WebResponse response = sc.getResponse( request );
assertTrue(response.getText().contains("ABC"));

WebResponse rsp = execAndAssert(request);

assertTrue(rsp.getText().contains("ABC"));
return rsp;
}

private WebResponse execAndAssert(WebRequest request) throws IOException, SAXException {
WebResponse rsp = sc.getResponse( request );
assertEquals(200,rsp.getResponseCode());
//assertEquals("TESTREASON",rsp.getResponseMessage());
assertTrue(rsp.getText().startsWith("REQUESTLINE:"));
return rsp;
}


/**
* Writes the input
*/
private static class RequestInfoHandler implements HttpRequestHandler
{

public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos,false);
final RequestLine rl = request.getRequestLine();
pw.println("REQUESTLINE: " + rl.getProtocolVersion() + " " + rl.getMethod() + " " + rl.getUri());
for (Header header : request.getAllHeaders()) {
pw.println(header.getName() + ": " + header.getValue());
}
pw.println("BODY: (below)");
pw.flush();//done with pw now

if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity = enclosingRequest.getEntity();
byte[] body = EntityUtils.toByteArray(entity);
baos.write(body);
}

response.setStatusCode(200);
response.setReasonPhrase("TESTREASON");
response.setEntity(new ByteArrayEntity(baos.toByteArray()));
}
}
}

0 comments on commit bc4aa97

Please sign in to comment.