[1376] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
| 16 | |
---|
| 17 | import static org.junit.Assert.*; |
---|
| 18 | |
---|
| 19 | import java.io.File; |
---|
| 20 | import java.io.IOException; |
---|
| 21 | import java.io.StringReader; |
---|
[1390] | 22 | import java.net.URL; |
---|
[1376] | 23 | import java.util.Collection; |
---|
| 24 | import java.util.Iterator; |
---|
| 25 | import java.util.LinkedList; |
---|
| 26 | import java.util.List; |
---|
| 27 | |
---|
| 28 | import javax.servlet.ServletException; |
---|
| 29 | import javax.servlet.http.HttpServlet; |
---|
| 30 | import javax.servlet.http.HttpServletRequest; |
---|
| 31 | import javax.servlet.http.HttpServletResponse; |
---|
| 32 | import javax.xml.bind.JAXBContext; |
---|
| 33 | import javax.xml.bind.JAXBElement; |
---|
| 34 | import javax.xml.bind.Unmarshaller; |
---|
[1390] | 35 | import javax.xml.namespace.QName; |
---|
[1376] | 36 | |
---|
| 37 | import org.eclipse.jetty.server.Server; |
---|
| 38 | import org.eclipse.jetty.servlet.ServletContextHandler; |
---|
| 39 | import org.eclipse.jetty.servlet.ServletHolder; |
---|
| 40 | import org.junit.Test; |
---|
| 41 | |
---|
[1390] | 42 | import dummyservice.ObjectFactory; |
---|
| 43 | import dummyservice.VerifyUserInMsgType; |
---|
| 44 | import dummyservice.VerifyUserOutMsgType; |
---|
| 45 | |
---|
[1376] | 46 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 47 | import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; |
---|
| 48 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
[1561] | 49 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
[1390] | 50 | import dummyservice.DummyService; |
---|
| 51 | import dummyservice.DummyServicePortType; |
---|
[1376] | 52 | |
---|
| 53 | /** |
---|
| 54 | * @author Patrick Harms |
---|
| 55 | */ |
---|
| 56 | public 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 | |
---|
[1390] | 80 | dummyMonitor = new Server(MONITOR_PORT); |
---|
[1376] | 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 | |
---|
[1614] | 112 | /** |
---|
| 113 | * |
---|
| 114 | */ |
---|
[1376] | 115 | @Test(expected=java.lang.IllegalArgumentException.class) |
---|
| 116 | public void test_InvalidInitialization_01() throws Exception { |
---|
| 117 | proxy = new HttpMonitoringProxy(new String[] { }); |
---|
| 118 | } |
---|
| 119 | |
---|
[1614] | 120 | /** |
---|
| 121 | * |
---|
| 122 | */ |
---|
[1376] | 123 | @Test |
---|
| 124 | public void test_SimpleText_Local() throws Exception { |
---|
| 125 | proxy = new HttpMonitoringProxy |
---|
[1390] | 126 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, |
---|
| 127 | "local" }); |
---|
[1376] | 128 | |
---|
| 129 | proxy.init(); |
---|
| 130 | proxy.start(); |
---|
| 131 | |
---|
| 132 | String message = "dummy message"; |
---|
| 133 | String expectedResponse = "response content"; |
---|
[1614] | 134 | String response = sendDummyMessage("POST", null, message, expectedResponse); |
---|
[1376] | 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 | |
---|
[1614] | 163 | assertEvent(sequence.get(0), "POST", null, message, expectedResponse); |
---|
[1376] | 164 | } |
---|
[1614] | 165 | |
---|
| 166 | /** |
---|
| 167 | * |
---|
| 168 | */ |
---|
[1376] | 169 | @Test |
---|
| 170 | public void test_SimpleText_Remote() throws Exception { |
---|
| 171 | proxy = new HttpMonitoringProxy |
---|
[1390] | 172 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", |
---|
| 173 | "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); |
---|
[1376] | 174 | |
---|
| 175 | proxy.init(); |
---|
| 176 | proxy.start(); |
---|
| 177 | |
---|
| 178 | String message = "dummy message"; |
---|
| 179 | String expectedResponse = "response content"; |
---|
[1614] | 180 | String response = sendDummyMessage("POST", null, message, expectedResponse); |
---|
[1376] | 181 | |
---|
| 182 | assertEquals(expectedResponse, response); |
---|
| 183 | |
---|
| 184 | // give the proxy some time to send the copy of the message |
---|
| 185 | while(messages.size() < 1) { |
---|
[1614] | 186 | System.out.println("waiting for message to be send by the proxy"); |
---|
[1376] | 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 | |
---|
[1614] | 199 | assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse); |
---|
[1376] | 200 | } |
---|
| 201 | |
---|
[1614] | 202 | /** |
---|
| 203 | * |
---|
| 204 | */ |
---|
[1376] | 205 | @Test |
---|
| 206 | public void test_XMLMessage_Local() throws Exception { |
---|
| 207 | proxy = new HttpMonitoringProxy |
---|
[1390] | 208 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", "localhost:" + DUMMY_SERVER_PORT, |
---|
| 209 | "local" }); |
---|
[1376] | 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 | |
---|
[1614] | 249 | String response = sendDummyMessage("POST", null, message, expectedResponse); |
---|
[1376] | 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 | |
---|
[1614] | 278 | assertEvent(sequence.get(0), "POST", null, message, expectedResponse); |
---|
[1376] | 279 | } |
---|
| 280 | |
---|
[1614] | 281 | /** |
---|
| 282 | * |
---|
| 283 | */ |
---|
[1376] | 284 | @Test |
---|
| 285 | public void test_XMLMessage_Remote() throws Exception { |
---|
| 286 | proxy = new HttpMonitoringProxy |
---|
[1390] | 287 | (new String[] { LOG_FILE_DIR, PROXY_PORT + "", |
---|
| 288 | "localhost:" + DUMMY_SERVER_PORT, "localhost:" + MONITOR_PORT }); |
---|
[1376] | 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 | |
---|
[1614] | 328 | String response = sendDummyMessage("POST", null, message, expectedResponse); |
---|
[1376] | 329 | |
---|
| 330 | assertEquals(expectedResponse, response); |
---|
| 331 | |
---|
| 332 | // give the proxy some time to send the copy of the message |
---|
| 333 | while(messages.size() < 1) { |
---|
[1614] | 334 | System.out.println("waiting for message to be send by the proxy"); |
---|
[1376] | 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 | |
---|
[1614] | 347 | assertExchange(jaxObject.getValue(), "POST", null, message, expectedResponse); |
---|
[1376] | 348 | } |
---|
| 349 | |
---|
[1614] | 350 | /** |
---|
| 351 | * |
---|
| 352 | */ |
---|
[1390] | 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 | |
---|
[1614] | 399 | assertEvent(sequence.get(0), "GET", "wsdl", null, null); // get WSDL |
---|
| 400 | assertEvent(sequence.get(1), "POST", null, null, null); // send request |
---|
[1390] | 401 | } |
---|
| 402 | |
---|
[1614] | 403 | /** |
---|
| 404 | * |
---|
| 405 | */ |
---|
[1390] | 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) { |
---|
[1614] | 433 | System.out.println("waiting for message to be send by the proxy"); |
---|
[1390] | 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 | |
---|
[1614] | 445 | assertExchange(jaxObject.getValue(), "GET", "wsdl", null, null); |
---|
[1390] | 446 | |
---|
| 447 | jaxObject = |
---|
| 448 | (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(1))); |
---|
| 449 | |
---|
[1614] | 450 | assertExchange(jaxObject.getValue(), "POST", null, null, null); |
---|
[1390] | 451 | } |
---|
[1563] | 452 | |
---|
[1614] | 453 | /** |
---|
| 454 | * |
---|
| 455 | */ |
---|
[1563] | 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" }); |
---|
[1376] | 461 | |
---|
[1563] | 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 | |
---|
[1614] | 473 | String response = |
---|
| 474 | sendDummyMessage("POST", null, message.toString(), expectedResponse.toString()); |
---|
[1563] | 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 | |
---|
[1614] | 503 | assertEvent(sequence.get(0), "POST", null, message.toString(), expectedResponse.toString()); |
---|
[1563] | 504 | } |
---|
[1614] | 505 | |
---|
| 506 | /** |
---|
| 507 | * |
---|
| 508 | */ |
---|
[1563] | 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 | |
---|
[1614] | 527 | String response = |
---|
| 528 | sendDummyMessage("POST", null, message.toString(), expectedResponse.toString()); |
---|
[1563] | 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) { |
---|
[1614] | 534 | System.out.println("waiting for message to be send by the proxy"); |
---|
[1563] | 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 | |
---|
[1614] | 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(); |
---|
[1563] | 562 | |
---|
[1614] | 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); |
---|
[1563] | 596 | } |
---|
[1614] | 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 }); |
---|
[1563] | 606 | |
---|
[1614] | 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 | |
---|
[1376] | 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); |
---|
[1614] | 653 | System.out.println("send ok response"); |
---|
[1376] | 654 | } |
---|
| 655 | |
---|
| 656 | |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | } |
---|