source: trunk/autoquest-httpmonitor-test/src/test/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorTest.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: 16.4 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.assertEquals;
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertTrue;
21
22import java.io.File;
23import java.io.StringWriter;
24import java.net.URL;
25import java.util.Collection;
26import java.util.Iterator;
27import java.util.List;
28
29import javax.xml.bind.JAXBContext;
30import javax.xml.bind.Marshaller;
31import javax.xml.namespace.QName;
32
33import org.apache.http.HttpEntity;
34import org.apache.http.client.methods.HttpPost;
35import org.apache.http.entity.ContentType;
36import org.apache.http.entity.StringEntity;
37import org.apache.http.impl.client.DefaultHttpClient;
38import org.junit.Test;
39
40import de.ugoe.cs.autoquest.eventcore.Event;
41import de.ugoe.cs.autoquest.http.HTTPTestUtils;
42import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy;
43import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser;
44import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
45import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType;
46import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget;
47import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
48import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory;
49import de.ugoe.cs.autoquest.plugin.http.logdata.Session;
50import dummyservice.DummyService;
51import dummyservice.DummyServicePortType;
52import dummyservice.VerifyUserInMsgType;
53import dummyservice.VerifyUserOutMsgType;
54
55/**
56 * @author Patrick Harms
57 */
58public class HttpMonitorTest extends AbstractTC {
59   
60    /**
61     *
62     */
63    private HttpMonitoringProxy proxy;
64
65    /**
66     *
67     */
68    private HttpMonitor monitor;
69
70    /* (non-Javadoc)
71     * @see de.ugoe.cs.autoquest.httpmonitor.AbstractTC#setUpHook()
72     */
73    @Override
74    protected void setUpHook() throws Exception {
75        // nothing to do
76    }
77
78    /**
79     *
80     */
81    public void tearDownHook() throws Exception {
82        if (proxy != null) {
83            try {
84                proxy.stop();
85            }
86            finally {
87                proxy = null;
88            }
89        }
90        if (monitor != null) {
91            try {
92                monitor.stop();
93            }
94            finally {
95                monitor = null;
96            }
97        }
98    }
99   
100    /**
101     *
102     */
103    @Test
104    public void test_SimulatedSession_MonitorOnly() throws Exception {
105        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
106
107        monitor.init();
108        monitor.start();
109       
110        Session simulatedSession = HTTPTestUtils.createRandomSession(50);
111        sendExchanges(simulatedSession.getHttpExchange());
112       
113        monitor.stop();
114        monitor = null;
115
116        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
117       
118        assertTrue(logFile.exists());
119       
120        HTTPLogParser parser = new HTTPLogParser();
121
122        parser.parseFile(logFile);
123
124        // check the sequences
125        Collection<List<Event>> sequences = parser.getSequences();
126
127        assertNotNull(sequences);
128
129        Iterator<List<Event>> iterator = sequences.iterator();
130        assertTrue(iterator.hasNext());
131
132        List<Event> sequence = iterator.next();
133        assertFalse(iterator.hasNext());
134
135        assertNotNull(sequence);
136        assertEquals(simulatedSession.getHttpExchange().size(), sequence.size());
137
138        System.out.println("{");
139        System.out.println("  {");
140        for (int j = 0; j < sequence.size(); j++) {
141            System.out.print("    ");
142            System.out.print(sequence.get(j));
143            System.out.println(",");
144
145            assertNotNull(sequence.get(j));
146            assertNotNull(sequence.get(j).getType());
147            assertTrue(sequence.get(j).getType() instanceof HTTPEventType);
148
149            assertNotNull(sequence.get(j).getTarget());
150            assertTrue(sequence.get(j).getTarget() instanceof HTTPTarget);
151
152            HTTPTestUtils.assertExchangeEquals
153                (simulatedSession.getHttpExchange().get(j),
154                 ((HTTPEventType) sequence.get(j).getType()).getExchange());
155
156            assertEquals(HTTPUtils.toString(simulatedSession.getHttpExchange().get(j).getReceiver()),
157                         ((HTTPTarget) sequence.get(j).getTarget()).getStringIdentifier());
158        }
159        System.out.println("  }");
160        System.out.println("}");
161        System.out.println("\n\n");
162    }
163   
164    /**
165     *
166     */
167    @Test
168    public void test_SimpleText_ProxyAndMonitor() throws Exception {
169        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
170
171        monitor.init();
172        monitor.start();
173
174        proxy = new HttpMonitoringProxy
175            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
176                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
177
178        proxy.init();
179        proxy.start();
180       
181        String message = "dummy message";
182        String expectedResponse = "response content";
183        String response = sendDummyMessage("POST", null, message, expectedResponse);
184       
185        assertEquals(expectedResponse, response);
186       
187        // the monitor needs some time to receive the exchange --> give it
188        Thread.sleep(1000);
189       
190        monitor.stop();
191        monitor = null;
192
193        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
194       
195        assertTrue(logFile.exists());
196       
197        HTTPLogParser parser = new HTTPLogParser();
198
199        parser.parseFile(logFile);
200
201        // check the sequences
202        Collection<List<Event>> sequences = parser.getSequences();
203
204        assertNotNull(sequences);
205
206        Iterator<List<Event>> iterator = sequences.iterator();
207        assertTrue(iterator.hasNext());
208
209        List<Event> sequence = iterator.next();
210        assertFalse(iterator.hasNext());
211
212        assertNotNull(sequence);
213        assertEquals(1, sequence.size());
214
215        assertEvent(sequence.get(0), "POST", null, message, response);
216    }
217   
218    /**
219     *
220     */
221    @Test
222    public void test_XMLMessage_ProxyAndMonitor() throws Exception {
223        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
224
225        monitor.init();
226        monitor.start();
227
228        proxy = new HttpMonitoringProxy
229            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
230                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
231
232        proxy.init();
233        proxy.start();
234       
235        String message =
236            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
237            "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" +
238            "  <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" +
239            "</xsd:schema>";
240   
241        String expectedResponse =
242            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
243            "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" +
244            "  <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" +
245            "  <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" +
246            "  <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" +
247            "    <headers>" +
248            "      <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" +
249            "      <header key=\"Connection\" value=\"keep-alive\"/>" +
250            "      <header key=\"Host\" value=\"localhost:19098\"/>" +
251            "      <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" +
252            "      <header key=\"Content-Length\" value=\"13\"/>" +
253            "    </headers>" +
254            "    <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" +
255            "             length=\"13\">" +
256            "      <data>dummy message</data>" +
257            "    </content>" +
258            "  </request>" +
259            "  <response status=\"200\">" +
260            "    <headers>" +
261            "      <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" +
262            "      <header key=\"Content-Length\" value=\"16\"/>" +
263            "    </headers>" +
264            "    <content encoding=\"ISO-8859-1\" length=\"16\">" +
265            "      <data>response content</data>" +
266            "    </content>" +
267            "  </response>" +
268            "</httpEvent>";
269
270        String response = sendDummyMessage("POST", null, message, expectedResponse);
271       
272        assertEquals(expectedResponse, response);
273       
274        // the monitor needs some time to receive the exchange --> give it
275        Thread.sleep(1000);
276       
277        monitor.stop();
278        monitor = null;
279
280        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
281       
282        assertTrue(logFile.exists());
283       
284        HTTPLogParser parser = new HTTPLogParser();
285
286        parser.parseFile(logFile);
287
288        // check the sequences
289        Collection<List<Event>> sequences = parser.getSequences();
290
291        assertNotNull(sequences);
292
293        Iterator<List<Event>> iterator = sequences.iterator();
294        assertTrue(iterator.hasNext());
295
296        List<Event> sequence = iterator.next();
297        assertFalse(iterator.hasNext());
298
299        assertNotNull(sequence);
300        assertEquals(1, sequence.size());
301
302        assertEvent(sequence.get(0), "POST", null, message, response);
303    }
304   
305    /**
306     *
307     */
308    @Test
309    public void test_SOAP_ProxyAndMonitor() throws Exception {
310        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
311
312        monitor.init();
313        monitor.start();
314
315        proxy = new HttpMonitoringProxy
316            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
317                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
318
319        proxy.init();
320        proxy.start();
321
322        DummyService service = new DummyService
323            (new URL("http://localhost:" + PROXY_PORT + "/dummyWebapp/DummyServiceSOAPPort?wsdl"),
324             new QName("DummyService", "DummyService"));
325
326        DummyServicePortType dummyService = service.getDummyServiceSOAPPort();
327       
328        dummyservice.ObjectFactory factory = new dummyservice.ObjectFactory();
329        VerifyUserInMsgType request = factory.createVerifyUserInMsgType();
330        VerifyUserOutMsgType response = dummyService.verifyUser(request);
331
332        assertNotNull(response);
333       
334        // the monitor needs some time to receive the exchange --> give it
335        Thread.sleep(1000);
336       
337        monitor.stop();
338        monitor = null;
339
340        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
341       
342        assertTrue(logFile.exists());
343       
344        HTTPLogParser parser = new HTTPLogParser();
345
346        parser.parseFile(logFile);
347
348        // check the sequences
349        Collection<List<Event>> sequences = parser.getSequences();
350
351        assertNotNull(sequences);
352
353        Iterator<List<Event>> iterator = sequences.iterator();
354        assertTrue(iterator.hasNext());
355
356        List<Event> sequence = iterator.next();
357        assertFalse(iterator.hasNext());
358
359        assertNotNull(sequence);
360        assertEquals(2, sequence.size());
361
362        assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL
363        assertEvent(sequence.get(1), "POST", null, null, null); // send request
364    }
365   
366    /**
367     *
368     */
369    @Test
370    public void test_LargeRequest_ProxyAndMonitor() throws Exception {
371        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
372
373        monitor.init();
374        monitor.start();
375
376        proxy = new HttpMonitoringProxy
377            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
378                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
379
380        proxy.init();
381        proxy.start();
382       
383        StringBuffer message = new StringBuffer();
384        StringBuffer expectedResponse = new StringBuffer();
385       
386        for (int i = 0; i < 1000; i++) {
387            message.append(" # " + i + " test request data");
388            expectedResponse.append(" # " + i + " test response data");
389        }
390       
391        String response =
392            sendDummyMessage("POST", null, message.toString(), expectedResponse.toString());
393       
394        assertEquals(expectedResponse.toString(), response);
395       
396        // the monitor needs some time to receive the exchange --> give it
397        Thread.sleep(1000);
398       
399        monitor.stop();
400        monitor = null;
401
402        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
403       
404        assertTrue(logFile.exists());
405       
406        HTTPLogParser parser = new HTTPLogParser();
407
408        parser.parseFile(logFile);
409
410        // check the sequences
411        Collection<List<Event>> sequences = parser.getSequences();
412
413        assertNotNull(sequences);
414
415        Iterator<List<Event>> iterator = sequences.iterator();
416        assertTrue(iterator.hasNext());
417
418        List<Event> sequence = iterator.next();
419        assertFalse(iterator.hasNext());
420
421        assertNotNull(sequence);
422        assertEquals(1, sequence.size());
423
424        assertEvent(sequence.get(0), "POST", null, message.toString(), response);
425    }
426   
427    /**
428     *
429     */
430    @Test
431    public void test_Query_ProxyAndMonitor() throws Exception {
432        monitor = new HttpMonitor(new String[] { LOG_FILE_DIR, MONITOR_PORT + "" });
433
434        monitor.init();
435        monitor.start();
436
437        proxy = new HttpMonitoringProxy
438            (new String[] { LOG_FILE_DIR, PROXY_PORT + "",
439                            "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT });
440
441        proxy.init();
442        proxy.start();
443       
444        String message = "dummy message";
445        String query = "key=value&key2=value2&key3=%3E%3CXXX";
446        String expectedResponse = "response content";
447        String response = sendDummyMessage("POST", query, message, expectedResponse);
448       
449        assertEquals(expectedResponse, response);
450       
451        // the monitor needs some time to receive the exchange --> give it
452        Thread.sleep(1000);
453       
454        monitor.stop();
455        monitor = null;
456
457        File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log");
458       
459        assertTrue(logFile.exists());
460       
461        HTTPLogParser parser = new HTTPLogParser();
462
463        parser.parseFile(logFile);
464
465        // check the sequences
466        Collection<List<Event>> sequences = parser.getSequences();
467
468        assertNotNull(sequences);
469
470        Iterator<List<Event>> iterator = sequences.iterator();
471        assertTrue(iterator.hasNext());
472
473        List<Event> sequence = iterator.next();
474        assertFalse(iterator.hasNext());
475
476        assertNotNull(sequence);
477        assertEquals(1, sequence.size());
478
479        assertEvent(sequence.get(0), "POST", query, message, response);
480    }
481
482    /**
483     *
484     */
485    private void sendExchanges(List<HttpExchange> exchanges) throws Exception {
486        DefaultHttpClient httpclient = new DefaultHttpClient();
487       
488        for (HttpExchange exchange : exchanges) {
489            HttpPost httpRequest = new HttpPost("http://localhost:" + MONITOR_PORT + "/");
490           
491            JAXBContext jaxbContext =
492                    JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
493            Marshaller marshaller = jaxbContext.createMarshaller();
494
495            StringWriter out = new StringWriter();
496            marshaller.marshal(new ObjectFactory().createHttpExchange(exchange), out);
497
498            HttpEntity entity = new StringEntity
499                (out.toString(), ContentType.create("text/plain", "UTF-8"));
500            ((HttpPost) httpRequest).setEntity(entity);
501       
502            try {
503                httpclient.execute(httpRequest);
504            }
505            finally {
506                httpRequest.releaseConnection();
507                out.close();
508            }
509        }
510    }
511
512}
Note: See TracBrowser for help on using the repository browser.