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

Last change on this file since 1614 was 1614, checked in by pharms, 10 years ago
  • bugfix and test for correct query handling
File size: 22.3 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 dummyservice.DummyService;
51import dummyservice.DummyServicePortType;
52
53/**
54 * @author Patrick Harms
55 */
56public class HttpMonitoringProxyTest extends AbstractTC {
57
58    /**
59     *
60     */
61    private HttpMonitoringProxy proxy;
62
63    /**
64     * the jetty web server used for receiving messages copied by the proxy
65     */
66    private Server dummyMonitor;
67   
68    /**
69     * the messages received by the dummy monitor
70     */
71    private List<String> messages;
72
73    /**
74     *
75     */
76    protected void setUpHook() throws Exception {
77        // setup a simple HTTP server
78        messages = new LinkedList<String>();
79       
80        dummyMonitor = new Server(MONITOR_PORT);
81        ServletContextHandler root =
82            new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS);
83
84        root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*");
85       
86        dummyMonitor.start();
87    }
88   
89    /**
90     *
91     */
92    protected void tearDownHook() throws Exception {
93        if (proxy != null) {
94            try {
95                proxy.stop();
96            }
97            finally {
98                proxy = null;
99            }
100        }
101        if (dummyMonitor != null) {
102            try {
103                dummyMonitor.stop();
104            }
105            finally {
106                dummyMonitor = null;
107            }
108        }
109        messages = null;
110    }
111   
112    /**
113     *
114     */
115    @Test(expected=java.lang.IllegalArgumentException.class)
116    public void test_InvalidInitialization_01() throws Exception {
117        proxy = new HttpMonitoringProxy(new String[] {  });
118    }
119   
120    /**
121     *
122     */
123    @Test
124    public void test_SimpleText_Local() throws Exception {
125        proxy = new HttpMonitoringProxy
126            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
127                            "local" });
128
129        proxy.init();
130        proxy.start();
131       
132        String message = "dummy message";
133        String expectedResponse = "response content";
134        String response = sendDummyMessage("POST", null, message, expectedResponse);
135       
136        assertEquals(expectedResponse, response);
137       
138        proxy.stop();
139        proxy = null;
140
141        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
142       
143        assertTrue(logFile.exists());
144       
145        HTTPLogParser parser = new HTTPLogParser();
146
147        parser.parseFile(logFile);
148
149        // check the sequences
150        Collection<List<Event>> sequences = parser.getSequences();
151
152        assertNotNull(sequences);
153
154        Iterator<List<Event>> iterator = sequences.iterator();
155        assertTrue(iterator.hasNext());
156
157        List<Event> sequence = iterator.next();
158        assertFalse(iterator.hasNext());
159
160        assertNotNull(sequence);
161        assertEquals(1, sequence.size());
162
163        assertEvent(sequence.get(0), "POST", null, message, expectedResponse);
164    }
165   
166    /**
167     *
168     */
169    @Test
170    public void test_SimpleText_Remote() throws Exception {
171        proxy = new HttpMonitoringProxy
172            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
173                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
174
175        proxy.init();
176        proxy.start();
177       
178        String message = "dummy message";
179        String expectedResponse = "response content";
180        String response = sendDummyMessage("POST", null, message, expectedResponse);
181       
182        assertEquals(expectedResponse, response);
183       
184        // give the proxy some time to send the copy of the message
185        while(messages.size() < 1) {
186            System.out.println("waiting for message to be send by the proxy");
187            Thread.sleep(1000);
188        }
189       
190        assertEquals(1, messages.size());
191       
192        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
193        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
194       
195        @SuppressWarnings("unchecked")
196        JAXBElement<HttpExchange> jaxObject =
197            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
198       
199        assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse);
200    }
201   
202    /**
203     *
204     */
205    @Test
206    public void test_XMLMessage_Local() throws Exception {
207        proxy = new HttpMonitoringProxy
208            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
209                            "local" });
210
211        proxy.init();
212        proxy.start();
213           
214        String message =
215            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
216            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
217            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
218            "</xsd:schema>";
219       
220        String expectedResponse =
221            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
222            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
223            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
224            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
225            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
226            "    <headers>" +
227            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
228            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
229            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
230            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
231            "      <header key=\"Content-Length\" value=\"13\"/>" +
232            "    </headers>" +
233            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
234            "             length=\"13\">" +
235            "      <data>dummy message</data>" +
236            "    </content>" +
237            "  </request>" +
238            "  <response status=\"200\">" +
239            "    <headers>" +
240            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
241            "      <header key=\"Content-Length\" value=\"16\"/>" +
242            "    </headers>" +
243            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
244            "      <data>response content</data>" +
245            "    </content>" +
246            "  </response>" +
247            "</httpEvent>";
248
249        String response = sendDummyMessage("POST", null, message, expectedResponse);
250       
251        assertEquals(expectedResponse, response);
252       
253        proxy.stop();
254        proxy = null;
255
256        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
257       
258        assertTrue(logFile.exists());
259       
260        HTTPLogParser parser = new HTTPLogParser();
261
262        parser.parseFile(logFile);
263
264        // check the sequences
265        Collection<List<Event>> sequences = parser.getSequences();
266
267        assertNotNull(sequences);
268
269        Iterator<List<Event>> iterator = sequences.iterator();
270        assertTrue(iterator.hasNext());
271
272        List<Event> sequence = iterator.next();
273        assertFalse(iterator.hasNext());
274
275        assertNotNull(sequence);
276        assertEquals(1, sequence.size());
277
278        assertEvent(sequence.get(0), "POST", null, message, expectedResponse);
279    }
280   
281    /**
282     *
283     */
284    @Test
285    public void test_XMLMessage_Remote() throws Exception {
286        proxy = new HttpMonitoringProxy
287            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
288                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
289
290        proxy.init();
291        proxy.start();
292           
293        String message =
294            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
295            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
296            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
297            "</xsd:schema>";
298       
299        String expectedResponse =
300            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
301            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
302            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
303            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
304            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
305            "    <headers>" +
306            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
307            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
308            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
309            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
310            "      <header key=\"Content-Length\" value=\"13\"/>" +
311            "    </headers>" +
312            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
313            "             length=\"13\">" +
314            "      <data>dummy message</data>" +
315            "    </content>" +
316            "  </request>" +
317            "  <response status=\"200\">" +
318            "    <headers>" +
319            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
320            "      <header key=\"Content-Length\" value=\"16\"/>" +
321            "    </headers>" +
322            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
323            "      <data>response content</data>" +
324            "    </content>" +
325            "  </response>" +
326            "</httpEvent>";
327
328        String response = sendDummyMessage("POST", null, message, expectedResponse);
329       
330        assertEquals(expectedResponse, response);
331       
332        // give the proxy some time to send the copy of the message
333        while(messages.size() < 1) {
334            System.out.println("waiting for message to be send by the proxy");
335            Thread.sleep(1000);
336        }
337       
338        assertEquals(1, messages.size());
339       
340        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
341        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
342       
343        @SuppressWarnings("unchecked")
344        JAXBElement<HttpExchange> jaxObject =
345            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
346       
347        assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse);
348    }
349   
350    /**
351     *
352     */
353    @Test
354    public void test_SOAP_Local() throws Exception {
355        proxy = new HttpMonitoringProxy
356            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
357                            "local" });
358
359        proxy.init();
360        proxy.start();
361
362        DummyService service = new DummyService
363            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
364             new QName("DummyService", "DummyService"));
365
366        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
367       
368        ObjectFactory factory = new ObjectFactory();
369        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
370        VerifyUserOutMsgType response = dummyService.verifyUser(request);
371
372        assertNotNull(response);
373       
374        proxy.stop();
375        proxy = null;
376
377        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
378       
379        assertTrue(logFile.exists());
380       
381        HTTPLogParser parser = new HTTPLogParser();
382
383        parser.parseFile(logFile);
384
385        // check the sequences
386        Collection<List<Event>> sequences = parser.getSequences();
387
388        assertNotNull(sequences);
389
390        Iterator<List<Event>> iterator = sequences.iterator();
391        assertTrue(iterator.hasNext());
392
393        List<Event> sequence = iterator.next();
394        assertFalse(iterator.hasNext());
395
396        assertNotNull(sequence);
397        assertEquals(2, sequence.size());
398
399        assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL
400        assertEvent(sequence.get(1), "POST", null, null, null); // send request
401    }
402   
403    /**
404     *
405     */
406    @SuppressWarnings("unchecked")
407    @Test
408    public void test_SOAP_Remote() throws Exception {
409        proxy = new HttpMonitoringProxy
410            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
411                            "localhost:" + MONITOR_PORT  });
412
413        proxy.init();
414        proxy.start();
415
416        DummyService service = new DummyService
417            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
418             new QName("DummyService", "DummyService"));
419
420        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
421       
422        ObjectFactory factory = new ObjectFactory();
423        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
424        VerifyUserOutMsgType response = dummyService.verifyUser(request);
425
426        assertNotNull(response);
427       
428        proxy.stop();
429        proxy = null;
430
431        // give the proxy some time to send the copy of the message
432        while(messages.size() < 1) {
433            System.out.println("waiting for message to be send by the proxy");
434            Thread.sleep(1000);
435        }
436       
437        assertEquals(2, messages.size());
438       
439        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
440        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
441       
442        JAXBElement<HttpExchange> jaxObject =
443            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
444       
445        assertExchange(jaxObject.getValue(), "GET", "wsdl", null, null);
446
447        jaxObject =
448            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(1)));
449           
450        assertExchange(jaxObject.getValue(), "POST", null, null, null);
451    }
452   
453    /**
454     *
455     */
456    @Test
457    public void test_LargeRequest_Local() throws Exception {
458        proxy = new HttpMonitoringProxy
459            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
460                            "local" });
461
462        proxy.init();
463        proxy.start();
464       
465        StringBuffer message = new StringBuffer();
466        StringBuffer expectedResponse = new StringBuffer();
467       
468        for (int i = 0; i < 1000; i++) {
469            message.append(" # " + i + " test request data");
470            expectedResponse.append(" # " + i + " test response data");
471        }
472       
473        String response =
474            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
475       
476        assertEquals(expectedResponse.toString(), response);
477       
478        proxy.stop();
479        proxy = null;
480
481        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
482       
483        assertTrue(logFile.exists());
484       
485        HTTPLogParser parser = new HTTPLogParser();
486
487        parser.parseFile(logFile);
488
489        // check the sequences
490        Collection<List<Event>> sequences = parser.getSequences();
491
492        assertNotNull(sequences);
493
494        Iterator<List<Event>> iterator = sequences.iterator();
495        assertTrue(iterator.hasNext());
496
497        List<Event> sequence = iterator.next();
498        assertFalse(iterator.hasNext());
499
500        assertNotNull(sequence);
501        assertEquals(1, sequence.size());
502
503        assertEvent(sequence.get(0), "POST", null, message.toString(), expectedResponse.toString());
504    }
505   
506    /**
507     *
508     */
509    @Test
510    public void test_LargeRequest_Remote() throws Exception {
511        proxy = new HttpMonitoringProxy
512            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
513                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
514
515        proxy.init();
516        proxy.start();
517       
518       
519        StringBuffer message = new StringBuffer();
520        StringBuffer expectedResponse = new StringBuffer();
521       
522        for (int i = 0; i < 1000; i++) {
523            message.append(" # " + i + " test request data");
524            expectedResponse.append(" # " + i + " test response data");
525        }
526       
527        String response =
528            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
529       
530        assertEquals(expectedResponse.toString(), response);
531       
532        // give the proxy some time to send the copy of the message
533        while(messages.size() < 1) {
534            System.out.println("waiting for message to be send by the proxy");
535            Thread.sleep(1000);
536        }
537       
538        assertEquals(1, messages.size());
539       
540        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
541        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
542       
543        @SuppressWarnings("unchecked")
544        JAXBElement<HttpExchange> jaxObject =
545            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
546       
547        assertExchange(jaxObject.getValue(), "POST", null,
548                       message.toString(), expectedResponse.toString());
549    }
550   
551    /**
552     *
553     */
554    @Test
555    public void test_Query_Local() throws Exception {
556        proxy = new HttpMonitoringProxy
557            (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT,
558                            "local" });
559
560        proxy.init();
561        proxy.start();
562       
563        String message = "dummy message";
564        String expectedResponse = "response content";
565        String query = "key=value&key2=value2&key3=%3E%3CXXX";
566        String response = sendDummyMessage("POST", query, message, expectedResponse);
567       
568        assertEquals(expectedResponse, response);
569       
570        proxy.stop();
571        proxy = null;
572
573        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
574       
575        assertTrue(logFile.exists());
576       
577        HTTPLogParser parser = new HTTPLogParser();
578
579        parser.parseFile(logFile);
580
581        // check the sequences
582        Collection<List<Event>> sequences = parser.getSequences();
583
584        assertNotNull(sequences);
585
586        Iterator<List<Event>> iterator = sequences.iterator();
587        assertTrue(iterator.hasNext());
588
589        List<Event> sequence = iterator.next();
590        assertFalse(iterator.hasNext());
591
592        assertNotNull(sequence);
593        assertEquals(1, sequence.size());
594
595        assertEvent(sequence.get(0), "POST", query, message, expectedResponse);
596    }
597   
598    /**
599     *
600     */
601    @Test
602    public void test_Query_Remote() throws Exception {
603        proxy = new HttpMonitoringProxy
604            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
605                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
606
607        proxy.init();
608        proxy.start();
609       
610        String message = "dummy message";
611        String expectedResponse = "response content";
612        String query = "key=value&key2=value2&key3=%3E%3CXXX";
613        String response = sendDummyMessage("POST", query, message, expectedResponse);
614       
615        assertEquals(expectedResponse, response);
616       
617        // give the proxy some time to send the copy of the message
618        while(messages.size() < 1) {
619            System.out.println("waiting for message to be send by the proxy");
620            Thread.sleep(1000);
621        }
622       
623        assertEquals(1, messages.size());
624       
625        JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
626        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
627       
628        @SuppressWarnings("unchecked")
629        JAXBElement<HttpExchange> jaxObject =
630            (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0)));
631       
632        assertExchange(jaxObject.getValue(), "POST", query, message, expectedResponse);
633    }
634
635    /**
636     * @author Patrick Harms
637     */
638    private class DummyMonitorServlet extends HttpServlet {
639
640        /**  */
641        private static final long serialVersionUID = 1L;
642       
643        /* (non-Javadoc)
644         * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
645         */
646        @Override
647        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
648            throws ServletException, IOException
649        {
650            messages.add(readToString(req.getReader()));
651           
652            resp.setStatus(HttpServletResponse.SC_OK);
653            System.out.println("send ok response");
654        }
655       
656       
657    }
658
659}
Note: See TracBrowser for help on using the repository browser.