source: trunk/autoquest-httpmonitor-test/src/test/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitoringProxyTest.java @ 1563

Last change on this file since 1563 was 1563, checked in by pharms, 10 years ago
  • bugfix for large requests
File size: 19.7 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.httpmonitor;
16
17import static org.junit.Assert.*;
18
19import java.io.File;
20import java.io.IOException;
21import java.io.StringReader;
22import java.net.URL;
23import java.util.Collection;
24import java.util.Iterator;
25import java.util.LinkedList;
26import java.util.List;
27
28import javax.servlet.ServletException;
29import javax.servlet.http.HttpServlet;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32import javax.xml.bind.JAXBContext;
33import javax.xml.bind.JAXBElement;
34import javax.xml.bind.Unmarshaller;
35import javax.xml.namespace.QName;
36
37import org.eclipse.jetty.server.Server;
38import org.eclipse.jetty.servlet.ServletContextHandler;
39import org.eclipse.jetty.servlet.ServletHolder;
40import org.junit.Test;
41
42import dummyservice.ObjectFactory;
43import dummyservice.VerifyUserInMsgType;
44import dummyservice.VerifyUserOutMsgType;
45
46import de.ugoe.cs.autoquest.eventcore.Event;
47import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy;
48import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
49import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
50import de.ugoe.cs.autoquest.plugin.http.logdata.Method;
51import dummyservice.DummyService;
52import dummyservice.DummyServicePortType;
53
54/**
55 * @author Patrick Harms
56 */
57public class HttpMonitoringProxyTest extends AbstractTC {
58
59    /**
60     *
61     */
62    private HttpMonitoringProxy proxy;
63
64    /**
65     * the jetty web server used for receiving messages copied by the proxy
66     */
67    private Server dummyMonitor;
68   
69    /**
70     * the messages received by the dummy monitor
71     */
72    private List<String> messages;
73
74    /**
75     *
76     */
77    protected void setUpHook() throws Exception {
78        // setup a simple HTTP server
79        messages = new LinkedList<String>();
80       
81        dummyMonitor = new Server(MONITOR_PORT);
82        ServletContextHandler root =
83            new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS);
84
85        root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*");
86       
87        dummyMonitor.start();
88    }
89   
90    /**
91     *
92     */
93    protected void tearDownHook() throws Exception {
94        if (proxy != null) {
95            try {
96                proxy.stop();
97            }
98            finally {
99                proxy = null;
100            }
101        }
102        if (dummyMonitor != null) {
103            try {
104                dummyMonitor.stop();
105            }
106            finally {
107                dummyMonitor = null;
108            }
109        }
110        messages = null;
111    }
112   
113    @Test(expected=java.lang.IllegalArgumentException.class)
114    public void test_InvalidInitialization_01() throws Exception {
115        proxy = new HttpMonitoringProxy(new String[] {  });
116    }
117   
118    @Test
119    public void test_SimpleText_Local() throws Exception {
120        proxy = new HttpMonitoringProxy
121            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
122                            "local" });
123
124        proxy.init();
125        proxy.start();
126       
127        String message = "dummy message";
128        String expectedResponse = "response content";
129        String response = sendDummyMessage("POST", message, expectedResponse);
130       
131        assertEquals(expectedResponse, response);
132       
133        proxy.stop();
134        proxy = null;
135
136        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
137       
138        assertTrue(logFile.exists());
139       
140        HTTPLogParser parser = new HTTPLogParser();
141
142        parser.parseFile(logFile);
143
144        // check the sequences
145        Collection<List<Event>> sequences = parser.getSequences();
146
147        assertNotNull(sequences);
148
149        Iterator<List<Event>> iterator = sequences.iterator();
150        assertTrue(iterator.hasNext());
151
152        List<Event> sequence = iterator.next();
153        assertFalse(iterator.hasNext());
154
155        assertNotNull(sequence);
156        assertEquals(1, sequence.size());
157
158        assertEvent(sequence.get(0), "POST", message, expectedResponse);
159    }
160
161    @Test
162    public void test_SimpleText_Remote() throws Exception {
163        proxy = new HttpMonitoringProxy
164            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
165                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
166
167        proxy.init();
168        proxy.start();
169       
170        String message = "dummy message";
171        String expectedResponse = "response content";
172        String response = sendDummyMessage("POST", message, expectedResponse);
173       
174        assertEquals(expectedResponse, response);
175       
176        // give the proxy some time to send the copy of the message
177        while(messages.size() < 1) {
178            Thread.sleep(1000);
179        }
180       
181        assertEquals(1, messages.size());
182       
183        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
184        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
185       
186        @SuppressWarnings("unchecked")
187        JAXBElement<HttpExchange> jaxObject =
188            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
189       
190        HttpExchange exchange = jaxObject.getValue();
191       
192        assertEquals(message, exchange.getRequest().getContent().getData());
193        assertEquals(response, exchange.getResponse().getContent().getData());
194    }
195   
196    @Test
197    public void test_XMLMessage_Local() throws Exception {
198        proxy = new HttpMonitoringProxy
199            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
200                            "local" });
201
202        proxy.init();
203        proxy.start();
204           
205        String message =
206            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
207            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
208            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
209            "</xsd:schema>";
210       
211        String expectedResponse =
212            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
213            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
214            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
215            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
216            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
217            "    <headers>" +
218            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
219            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
220            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
221            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
222            "      <header key=\"Content-Length\" value=\"13\"/>" +
223            "    </headers>" +
224            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
225            "             length=\"13\">" +
226            "      <data>dummy message</data>" +
227            "    </content>" +
228            "  </request>" +
229            "  <response status=\"200\">" +
230            "    <headers>" +
231            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
232            "      <header key=\"Content-Length\" value=\"16\"/>" +
233            "    </headers>" +
234            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
235            "      <data>response content</data>" +
236            "    </content>" +
237            "  </response>" +
238            "</httpEvent>";
239
240        String response = sendDummyMessage("POST", message, expectedResponse);
241       
242        assertEquals(expectedResponse, response);
243       
244        proxy.stop();
245        proxy = null;
246
247        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
248       
249        assertTrue(logFile.exists());
250       
251        HTTPLogParser parser = new HTTPLogParser();
252
253        parser.parseFile(logFile);
254
255        // check the sequences
256        Collection<List<Event>> sequences = parser.getSequences();
257
258        assertNotNull(sequences);
259
260        Iterator<List<Event>> iterator = sequences.iterator();
261        assertTrue(iterator.hasNext());
262
263        List<Event> sequence = iterator.next();
264        assertFalse(iterator.hasNext());
265
266        assertNotNull(sequence);
267        assertEquals(1, sequence.size());
268
269        assertEvent(sequence.get(0), "POST", message, expectedResponse);
270    }
271   
272    @Test
273    public void test_XMLMessage_Remote() throws Exception {
274        proxy = new HttpMonitoringProxy
275            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
276                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
277
278        proxy.init();
279        proxy.start();
280           
281        String message =
282            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
283            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
284            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
285            "</xsd:schema>";
286       
287        String expectedResponse =
288            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
289            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
290            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
291            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
292            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
293            "    <headers>" +
294            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
295            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
296            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
297            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
298            "      <header key=\"Content-Length\" value=\"13\"/>" +
299            "    </headers>" +
300            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
301            "             length=\"13\">" +
302            "      <data>dummy message</data>" +
303            "    </content>" +
304            "  </request>" +
305            "  <response status=\"200\">" +
306            "    <headers>" +
307            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
308            "      <header key=\"Content-Length\" value=\"16\"/>" +
309            "    </headers>" +
310            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
311            "      <data>response content</data>" +
312            "    </content>" +
313            "  </response>" +
314            "</httpEvent>";
315
316        String response = sendDummyMessage("POST", message, expectedResponse);
317       
318        assertEquals(expectedResponse, response);
319       
320        // give the proxy some time to send the copy of the message
321        while(messages.size() < 1) {
322            Thread.sleep(1000);
323        }
324       
325        assertEquals(1, messages.size());
326       
327        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
328        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
329       
330        @SuppressWarnings("unchecked")
331        JAXBElement<HttpExchange> jaxObject =
332            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
333       
334        HttpExchange exchange = jaxObject.getValue();
335       
336        assertEquals(message, exchange.getRequest().getContent().getData());
337        assertEquals(response, exchange.getResponse().getContent().getData());
338    }
339   
340    @Test
341    public void test_SOAP_Local() throws Exception {
342        proxy = new HttpMonitoringProxy
343            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
344                            "local" });
345
346        proxy.init();
347        proxy.start();
348
349        DummyService service = new DummyService
350            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
351             new QName("DummyService", "DummyService"));
352
353        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
354       
355        ObjectFactory factory = new ObjectFactory();
356        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
357        VerifyUserOutMsgType response = dummyService.verifyUser(request);
358
359        assertNotNull(response);
360       
361        proxy.stop();
362        proxy = null;
363
364        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
365       
366        assertTrue(logFile.exists());
367       
368        HTTPLogParser parser = new HTTPLogParser();
369
370        parser.parseFile(logFile);
371
372        // check the sequences
373        Collection<List<Event>> sequences = parser.getSequences();
374
375        assertNotNull(sequences);
376
377        Iterator<List<Event>> iterator = sequences.iterator();
378        assertTrue(iterator.hasNext());
379
380        List<Event> sequence = iterator.next();
381        assertFalse(iterator.hasNext());
382
383        assertNotNull(sequence);
384        assertEquals(2, sequence.size());
385
386        assertEvent(sequence.get(0), "GET", null, null); // get WSDL
387        assertEvent(sequence.get(1), "POST", null, null); // send request
388    }
389   
390    @SuppressWarnings("unchecked")
391    @Test
392    public void test_SOAP_Remote() throws Exception {
393        proxy = new HttpMonitoringProxy
394            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
395                            "localhost:" + MONITOR_PORT  });
396
397        proxy.init();
398        proxy.start();
399
400        DummyService service = new DummyService
401            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
402             new QName("DummyService", "DummyService"));
403
404        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
405       
406        ObjectFactory factory = new ObjectFactory();
407        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
408        VerifyUserOutMsgType response = dummyService.verifyUser(request);
409
410        assertNotNull(response);
411       
412        proxy.stop();
413        proxy = null;
414
415        // give the proxy some time to send the copy of the message
416        while(messages.size() < 1) {
417            Thread.sleep(1000);
418        }
419       
420        assertEquals(2, messages.size());
421       
422        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
423        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
424       
425        JAXBElement<HttpExchange> jaxObject =
426            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
427       
428        HttpExchange exchange = jaxObject.getValue();
429       
430        assertEquals(Method.GET, exchange.getRequest().getMethod());
431
432        assertNull(exchange.getRequest().getContent());
433        assertNotNull(exchange.getResponse().getContent().getData());
434
435        jaxObject =
436            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(1)));
437           
438        exchange = jaxObject.getValue();
439           
440        assertEquals(Method.POST, exchange.getRequest().getMethod());
441
442        assertNotNull(exchange.getRequest().getContent().getData());
443        assertNotNull(exchange.getResponse().getContent().getData());
444    }
445   
446    @Test
447    public void test_LargeRequest_Local() throws Exception {
448        proxy = new HttpMonitoringProxy
449            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
450                            "local" });
451
452        proxy.init();
453        proxy.start();
454       
455        StringBuffer message = new StringBuffer();
456        StringBuffer expectedResponse = new StringBuffer();
457       
458        for (int i = 0; i < 1000; i++) {
459            message.append(" # " + i + " test request data");
460            expectedResponse.append(" # " + i + " test response data");
461        }
462       
463        String response = sendDummyMessage("POST", message.toString(), expectedResponse.toString());
464       
465        assertEquals(expectedResponse.toString(), response);
466       
467        proxy.stop();
468        proxy = null;
469
470        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
471       
472        assertTrue(logFile.exists());
473       
474        HTTPLogParser parser = new HTTPLogParser();
475
476        parser.parseFile(logFile);
477
478        // check the sequences
479        Collection<List<Event>> sequences = parser.getSequences();
480
481        assertNotNull(sequences);
482
483        Iterator<List<Event>> iterator = sequences.iterator();
484        assertTrue(iterator.hasNext());
485
486        List<Event> sequence = iterator.next();
487        assertFalse(iterator.hasNext());
488
489        assertNotNull(sequence);
490        assertEquals(1, sequence.size());
491
492        assertEvent(sequence.get(0), "POST", message.toString(), expectedResponse.toString());
493    }
494
495    @Test
496    public void test_LargeRequest_Remote() throws Exception {
497        proxy = new HttpMonitoringProxy
498            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
499                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
500
501        proxy.init();
502        proxy.start();
503       
504       
505        StringBuffer message = new StringBuffer();
506        StringBuffer expectedResponse = new StringBuffer();
507       
508        for (int i = 0; i < 1000; i++) {
509            message.append(" # " + i + " test request data");
510            expectedResponse.append(" # " + i + " test response data");
511        }
512       
513        String response = sendDummyMessage("POST", message.toString(), expectedResponse.toString());
514       
515        assertEquals(expectedResponse.toString(), response);
516       
517        // give the proxy some time to send the copy of the message
518        while(messages.size() < 1) {
519            Thread.sleep(1000);
520        }
521       
522        assertEquals(1, messages.size());
523       
524        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
525        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
526       
527        @SuppressWarnings("unchecked")
528        JAXBElement<HttpExchange> jaxObject =
529            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
530       
531        HttpExchange exchange = jaxObject.getValue();
532       
533        assertEquals(message.toString(), exchange.getRequest().getContent().getData());
534        assertEquals(response.toString(), exchange.getResponse().getContent().getData());
535    }
536
537    /**
538     * @author Patrick Harms
539     */
540    private class DummyMonitorServlet extends HttpServlet {
541
542        /**  */
543        private static final long serialVersionUID = 1L;
544       
545        /* (non-Javadoc)
546         * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
547         */
548        @Override
549        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
550            throws ServletException, IOException
551        {
552            messages.add(readToString(req.getReader()));
553           
554            resp.setStatus(HttpServletResponse.SC_OK);
555        }
556       
557       
558    }
559
560}
Note: See TracBrowser for help on using the repository browser.