// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.httpmonitor; import static org.junit.Assert.*; import java.io.File; import java.io.IOException; import java.io.StringReader; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.junit.Test; import de.ugoe.cs.autoquest.eventcore.Event; import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; import de.ugoe.cs.autoquest.httpmonitor.exchange.Method; import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; /** * @author Patrick Harms */ public class HttpMonitoringProxyTest extends AbstractTC { /** * */ private HttpMonitoringProxy proxy; /** * the jetty web server used for receiving messages copied by the proxy */ private Server dummyMonitor; /** * the messages received by the dummy monitor */ private List messages; /** * */ protected void setUpHook() throws Exception { // setup a simple HTTP server messages = new LinkedList(); dummyMonitor = new Server(PORT + 2); ServletContextHandler root = new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS); root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*"); dummyMonitor.start(); } /** * */ protected void tearDownHook() throws Exception { if (proxy != null) { try { proxy.stop(); } finally { proxy = null; } } if (dummyMonitor != null) { try { dummyMonitor.stop(); } finally { dummyMonitor = null; } } messages = null; } @Test(expected=java.lang.IllegalArgumentException.class) public void test_InvalidInitialization_01() throws Exception { proxy = new HttpMonitoringProxy(new String[] { }); } @Test public void test_SimpleText_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "local" }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String response = sendDummyMessage("POST", message, expectedResponse); assertEquals(expectedResponse, response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", message, expectedResponse); } @Test public void test_SimpleText_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); proxy.init(); proxy.start(); String message = "dummy message"; String expectedResponse = "response content"; String response = sendDummyMessage("POST", message, expectedResponse); assertEquals(expectedResponse, response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); HttpExchange exchange = jaxObject.getValue(); assertEquals(message, exchange.getRequest().getContent().getData()); assertEquals(response, exchange.getResponse().getContent().getData()); } @Test public void test_XMLMessage_Local() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "local" }); proxy.init(); proxy.start(); String message = "" + "" + " " + ""; String expectedResponse = "" + "" + " 127.0.0.1127.0.0.142688" + " 127.0.0.1127.0.0.119098" + " " + " " + "
" + "
" + "
" + "
" + "
" + " " + " " + " dummy message" + " " + " " + " " + " " + "
" + "
" + " " + " " + " response content" + " " + " " + ""; String response = sendDummyMessage("POST", message, expectedResponse); assertEquals(expectedResponse, response); proxy.stop(); proxy = null; File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); assertTrue(logFile.exists()); HTTPLogParser parser = new HTTPLogParser(); parser.parseFile(logFile); // check the sequences Collection> sequences = parser.getSequences(); assertNotNull(sequences); Iterator> iterator = sequences.iterator(); assertTrue(iterator.hasNext()); List sequence = iterator.next(); assertFalse(iterator.hasNext()); assertNotNull(sequence); assertEquals(1, sequence.size()); assertEvent(sequence.get(0), "POST", message, expectedResponse); } @Test public void test_XMLMessage_Remote() throws Exception { proxy = new HttpMonitoringProxy (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); proxy.init(); proxy.start(); String message = "" + "" + " " + ""; String expectedResponse = "" + "" + " 127.0.0.1127.0.0.142688" + " 127.0.0.1127.0.0.119098" + " " + " " + "
" + "
" + "
" + "
" + "
" + " " + " " + " dummy message" + " " + " " + " " + " " + "
" + "
" + " " + " " + " response content" + " " + " " + ""; String response = sendDummyMessage("POST", message, expectedResponse); assertEquals(expectedResponse, response); // give the proxy some time to send the copy of the message while(messages.size() < 1) { Thread.sleep(1000); } assertEquals(1, messages.size()); JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") JAXBElement jaxObject = (JAXBElement) unmarshaller.unmarshal(new StringReader(messages.get(0))); HttpExchange exchange = jaxObject.getValue(); assertEquals(message, exchange.getRequest().getContent().getData()); assertEquals(response, exchange.getResponse().getContent().getData()); } /** * */ private void assertEvent(Event event, String method, String message, String response) { assertNotNull(event); assertNotNull(event.getType()); assertNotNull(event.getTarget()); assertTrue(event.getType() instanceof HTTPEventType); assertTrue(event.getTarget() instanceof HTTPTarget); HttpExchange exchange = ((HTTPEventType) event.getType()).getExchange(); assertEquals(Method.fromValue(method), exchange.getRequest().getMethod()); assertEquals(message, exchange.getRequest().getContent().getData()); assertEquals(response, exchange.getResponse().getContent().getData()); } /** * @author Patrick Harms */ private class DummyMonitorServlet extends HttpServlet { /** */ private static final long serialVersionUID = 1L; /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { messages.add(readToString(req.getReader())); resp.setStatus(HttpServletResponse.SC_OK); } } }