6
6
import com .meterware .httpunit .WebResponse ;
7
7
import com .meterware .servletunit .ServletRunner ;
8
8
import com .meterware .servletunit .ServletUnitClient ;
9
- import org .apache .http .localserver .EchoHandler ;
9
+ import org .apache .http .*;
10
+ import org .apache .http .entity .ByteArrayEntity ;
10
11
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 ;
11
15
import org .junit .After ;
12
16
import org .junit .Before ;
13
17
import org .junit .Test ;
14
18
import org .xml .sax .SAXException ;
15
19
16
- import java .io .IOException ;
20
+ import java .io .* ;
17
21
import java .util .Properties ;
18
22
19
23
import static org .junit .Assert .assertEquals ;
@@ -33,11 +37,13 @@ public class ProxyServletTest
33
37
34
38
/** From Meterware httpunit. */
35
39
private ServletRunner servletRunner ;
40
+ private ServletUnitClient sc ;
36
41
37
42
@ Before
38
43
public void setUp () throws Exception {
39
44
localTestServer = new LocalTestServer (null , null );
40
45
localTestServer .start ();
46
+ localTestServer .register ("/targetPath*" ,new RequestInfoHandler ());//matches /targetPath and /targetPath/blahblah
41
47
42
48
servletRunner = new ServletRunner ();
43
49
Properties params = new Properties ();
@@ -46,6 +52,7 @@ public void setUp() throws Exception {
46
52
params .setProperty (ProxyServlet .P_PROXY_PATH , "/targetPath" );//dummy
47
53
params .setProperty (ProxyServlet .P_LOG , "true" );
48
54
servletRunner .registerServlet ("/proxyMe/*" , ProxyServlet .class .getName (), params );//also matches /proxyMe (no path info)
55
+ sc = servletRunner .newClient ();
49
56
}
50
57
51
58
@ After
@@ -55,23 +62,76 @@ public void tearDown() throws Exception {
55
62
}
56
63
57
64
@ 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/" ));
60
68
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
+ }
62
74
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
+ }
65
83
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 ;
70
88
}
71
89
72
- private void testEchoRequest ( ServletUnitClient sc , WebRequest request ) throws IOException , SAXException {
90
+ private WebResponse execPostAndAssert ( PostMethodWebRequest request ) throws IOException , SAXException {
73
91
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
+ }
76
136
}
77
137
}
0 commit comments