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