Skip to content

Commit bc4aa97

Browse files
committed
Test sending a file. Turns out we need commons-io in this case.
1 parent cedc33a commit bc4aa97

File tree

4 files changed

+93
-14
lines changed

4 files changed

+93
-14
lines changed

httpproxy.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<orderEntry type="sourceFolder" forTests="false" />
1515
<orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.4" level="project" />
1616
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.2.2" level="project" />
17+
<orderEntry type="library" name="Maven: commons-io:commons-io:1.3.2" level="project" />
1718
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" />
1819
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
1920
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.2" level="project" />

httpproxy.ipr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,17 @@
908908
<root url="jar://$M2_REPO$/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1-sources.jar!/" />
909909
</SOURCES>
910910
</library>
911+
<library name="Maven: commons-io:commons-io:1.3.2">
912+
<CLASSES>
913+
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2.jar!/" />
914+
</CLASSES>
915+
<JAVADOC>
916+
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2-javadoc.jar!/" />
917+
</JAVADOC>
918+
<SOURCES>
919+
<root url="jar://$M2_REPO$/commons-io/commons-io/1.3.2/commons-io-1.3.2-sources.jar!/" />
920+
</SOURCES>
921+
</library>
911922
<library name="Maven: commons-logging:commons-logging:1.0.4">
912923
<CLASSES>
913924
<root url="jar://$M2_REPO$/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar!/" />
@@ -972,6 +983,7 @@
972983
</JAVADOC>
973984
<SOURCES>
974985
<root url="jar://$M2_REPO$/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2-test-sources.jar!/" />
986+
<root url="intellijad://intellijad" />
975987
</SOURCES>
976988
</library>
977989
<library name="Maven: org.apache.httpcomponents:httpcore:4.1.2">

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<artifactId>commons-fileupload</artifactId>
2727
<version>1.2.2</version>
2828
</dependency>
29+
<!-- Only when sending a file -->
30+
<dependency>
31+
<groupId>commons-io</groupId>
32+
<artifactId>commons-io</artifactId>
33+
<version>1.3.2</version>
34+
</dependency>
2935
<dependency>
3036
<groupId>commons-httpclient</groupId>
3137
<artifactId>commons-httpclient</artifactId>

src/test/java/org/mitre/dsmiley/httpproxy/ProxyServletTest.java

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
import com.meterware.httpunit.WebResponse;
77
import com.meterware.servletunit.ServletRunner;
88
import com.meterware.servletunit.ServletUnitClient;
9-
import org.apache.http.localserver.EchoHandler;
9+
import org.apache.http.*;
10+
import org.apache.http.entity.ByteArrayEntity;
1011
import org.apache.http.localserver.LocalTestServer;
12+
import org.apache.http.protocol.HttpContext;
13+
import org.apache.http.protocol.HttpRequestHandler;
14+
import org.apache.http.util.EntityUtils;
1115
import org.junit.After;
1216
import org.junit.Before;
1317
import org.junit.Test;
1418
import org.xml.sax.SAXException;
1519

16-
import java.io.IOException;
20+
import java.io.*;
1721
import java.util.Properties;
1822

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

3438
/** From Meterware httpunit. */
3539
private ServletRunner servletRunner;
40+
private ServletUnitClient sc;
3641

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

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

5158
@After
@@ -55,23 +62,76 @@ public void tearDown() throws Exception {
5562
}
5663

5764
@Test
58-
public void testDoGet() throws Exception {
59-
localTestServer.register("/targetPath*",new EchoHandler());//matches /targetPath and /targetPath/blahblah
65+
public void test() throws Exception {
66+
execGetAndAssert(new GetMethodWebRequest("http://localhost/proxyMe"));
67+
execGetAndAssert(new GetMethodWebRequest("http://localhost/proxyMe/"));
6068

61-
ServletUnitClient sc = servletRunner.newClient();
69+
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe"));
70+
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe/pathInfo"));
71+
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe?def=DEF"));
72+
execPostAndAssert(new PostMethodWebRequest("http://localhost/proxyMe/pathInfo?def=DEF"));
73+
}
6274

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

66-
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe" ));
67-
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe/pathInfo" ));
68-
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe?def=DEF" ));
69-
testEchoRequest(sc, new PostMethodWebRequest( "http://localhost/proxyMe/pathInfo?def=DEF" ));
84+
private WebResponse execGetAndAssert(GetMethodWebRequest request) throws IOException, SAXException {
85+
WebResponse rsp = execAndAssert(request);
86+
//TODO //no assertions for GET but a failure should throw
87+
return rsp;
7088
}
7189

72-
private void testEchoRequest(ServletUnitClient sc, WebRequest request) throws IOException, SAXException {
90+
private WebResponse execPostAndAssert(PostMethodWebRequest request) throws IOException, SAXException {
7391
request.setParameter("abc","ABC");
74-
WebResponse response = sc.getResponse( request );
75-
assertTrue(response.getText().contains("ABC"));
92+
93+
WebResponse rsp = execAndAssert(request);
94+
95+
assertTrue(rsp.getText().contains("ABC"));
96+
return rsp;
97+
}
98+
99+
private WebResponse execAndAssert(WebRequest request) throws IOException, SAXException {
100+
WebResponse rsp = sc.getResponse( request );
101+
assertEquals(200,rsp.getResponseCode());
102+
//assertEquals("TESTREASON",rsp.getResponseMessage());
103+
assertTrue(rsp.getText().startsWith("REQUESTLINE:"));
104+
return rsp;
105+
}
106+
107+
108+
/**
109+
* Writes the input
110+
*/
111+
private static class RequestInfoHandler implements HttpRequestHandler
112+
{
113+
114+
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
115+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
116+
PrintWriter pw = new PrintWriter(baos,false);
117+
final RequestLine rl = request.getRequestLine();
118+
pw.println("REQUESTLINE: " + rl.getProtocolVersion() + " " + rl.getMethod() + " " + rl.getUri());
119+
for (Header header : request.getAllHeaders()) {
120+
pw.println(header.getName() + ": " + header.getValue());
121+
}
122+
pw.println("BODY: (below)");
123+
pw.flush();//done with pw now
124+
125+
if (request instanceof HttpEntityEnclosingRequest) {
126+
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
127+
HttpEntity entity = enclosingRequest.getEntity();
128+
byte[] body = EntityUtils.toByteArray(entity);
129+
baos.write(body);
130+
}
131+
132+
response.setStatusCode(200);
133+
response.setReasonPhrase("TESTREASON");
134+
response.setEntity(new ByteArrayEntity(baos.toByteArray()));
135+
}
76136
}
77137
}

0 commit comments

Comments
 (0)