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; |
---|
22 | import java.util.Collection; |
---|
23 | import java.util.Iterator; |
---|
24 | import java.util.LinkedList; |
---|
25 | import java.util.List; |
---|
26 | |
---|
27 | import javax.servlet.ServletException; |
---|
28 | import javax.servlet.http.HttpServlet; |
---|
29 | import javax.servlet.http.HttpServletRequest; |
---|
30 | import javax.servlet.http.HttpServletResponse; |
---|
31 | import javax.xml.bind.JAXBContext; |
---|
32 | import javax.xml.bind.JAXBElement; |
---|
33 | import javax.xml.bind.Unmarshaller; |
---|
34 | |
---|
35 | import org.eclipse.jetty.server.Server; |
---|
36 | import org.eclipse.jetty.servlet.ServletContextHandler; |
---|
37 | import org.eclipse.jetty.servlet.ServletHolder; |
---|
38 | import org.junit.Test; |
---|
39 | |
---|
40 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
41 | import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; |
---|
42 | import de.ugoe.cs.autoquest.httpmonitor.exchange.Method; |
---|
43 | import de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitoringProxy; |
---|
44 | import de.ugoe.cs.autoquest.plugin.http.HTTPLogParser; |
---|
45 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType; |
---|
46 | import de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPTarget; |
---|
47 | |
---|
48 | /** |
---|
49 | * @author Patrick Harms |
---|
50 | */ |
---|
51 | public class HttpMonitoringProxyTest extends AbstractTC { |
---|
52 | |
---|
53 | /** |
---|
54 | * |
---|
55 | */ |
---|
56 | private HttpMonitoringProxy proxy; |
---|
57 | |
---|
58 | /** |
---|
59 | * the jetty web server used for receiving messages copied by the proxy |
---|
60 | */ |
---|
61 | private Server dummyMonitor; |
---|
62 | |
---|
63 | /** |
---|
64 | * the messages received by the dummy monitor |
---|
65 | */ |
---|
66 | private List<String> messages; |
---|
67 | |
---|
68 | /** |
---|
69 | * |
---|
70 | */ |
---|
71 | protected void setUpHook() throws Exception { |
---|
72 | // setup a simple HTTP server |
---|
73 | messages = new LinkedList<String>(); |
---|
74 | |
---|
75 | dummyMonitor = new Server(PORT + 2); |
---|
76 | ServletContextHandler root = |
---|
77 | new ServletContextHandler(dummyMonitor, "/", ServletContextHandler.SESSIONS); |
---|
78 | |
---|
79 | root.addServlet(new ServletHolder(new DummyMonitorServlet()), "/*"); |
---|
80 | |
---|
81 | dummyMonitor.start(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * |
---|
86 | */ |
---|
87 | protected void tearDownHook() throws Exception { |
---|
88 | if (proxy != null) { |
---|
89 | try { |
---|
90 | proxy.stop(); |
---|
91 | } |
---|
92 | finally { |
---|
93 | proxy = null; |
---|
94 | } |
---|
95 | } |
---|
96 | if (dummyMonitor != null) { |
---|
97 | try { |
---|
98 | dummyMonitor.stop(); |
---|
99 | } |
---|
100 | finally { |
---|
101 | dummyMonitor = null; |
---|
102 | } |
---|
103 | } |
---|
104 | messages = null; |
---|
105 | } |
---|
106 | |
---|
107 | @Test(expected=java.lang.IllegalArgumentException.class) |
---|
108 | public void test_InvalidInitialization_01() throws Exception { |
---|
109 | proxy = new HttpMonitoringProxy(new String[] { }); |
---|
110 | } |
---|
111 | |
---|
112 | @Test |
---|
113 | public void test_SimpleText_Local() throws Exception { |
---|
114 | proxy = new HttpMonitoringProxy |
---|
115 | (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "local" }); |
---|
116 | |
---|
117 | proxy.init(); |
---|
118 | proxy.start(); |
---|
119 | |
---|
120 | String message = "dummy message"; |
---|
121 | String expectedResponse = "response content"; |
---|
122 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
123 | |
---|
124 | assertEquals(expectedResponse, response); |
---|
125 | |
---|
126 | proxy.stop(); |
---|
127 | proxy = null; |
---|
128 | |
---|
129 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
130 | |
---|
131 | assertTrue(logFile.exists()); |
---|
132 | |
---|
133 | HTTPLogParser parser = new HTTPLogParser(); |
---|
134 | |
---|
135 | parser.parseFile(logFile); |
---|
136 | |
---|
137 | // check the sequences |
---|
138 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
139 | |
---|
140 | assertNotNull(sequences); |
---|
141 | |
---|
142 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
143 | assertTrue(iterator.hasNext()); |
---|
144 | |
---|
145 | List<Event> sequence = iterator.next(); |
---|
146 | assertFalse(iterator.hasNext()); |
---|
147 | |
---|
148 | assertNotNull(sequence); |
---|
149 | assertEquals(1, sequence.size()); |
---|
150 | |
---|
151 | assertEvent(sequence.get(0), "POST", message, expectedResponse); |
---|
152 | } |
---|
153 | |
---|
154 | @Test |
---|
155 | public void test_SimpleText_Remote() throws Exception { |
---|
156 | proxy = new HttpMonitoringProxy |
---|
157 | (new String[] { LOG_FILE_DIR, PORT +"", |
---|
158 | "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); |
---|
159 | |
---|
160 | proxy.init(); |
---|
161 | proxy.start(); |
---|
162 | |
---|
163 | String message = "dummy message"; |
---|
164 | String expectedResponse = "response content"; |
---|
165 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
166 | |
---|
167 | assertEquals(expectedResponse, response); |
---|
168 | |
---|
169 | // give the proxy some time to send the copy of the message |
---|
170 | while(messages.size() < 1) { |
---|
171 | Thread.sleep(1000); |
---|
172 | } |
---|
173 | |
---|
174 | assertEquals(1, messages.size()); |
---|
175 | |
---|
176 | JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
177 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
178 | |
---|
179 | @SuppressWarnings("unchecked") |
---|
180 | JAXBElement<HttpExchange> jaxObject = |
---|
181 | (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0))); |
---|
182 | |
---|
183 | HttpExchange exchange = jaxObject.getValue(); |
---|
184 | |
---|
185 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
186 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
187 | } |
---|
188 | |
---|
189 | @Test |
---|
190 | public void test_XMLMessage_Local() throws Exception { |
---|
191 | proxy = new HttpMonitoringProxy |
---|
192 | (new String[] { LOG_FILE_DIR, PORT +"", "localhost:" + (PORT + 1), "local" }); |
---|
193 | |
---|
194 | proxy.init(); |
---|
195 | proxy.start(); |
---|
196 | |
---|
197 | String message = |
---|
198 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
199 | "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
200 | " <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" + |
---|
201 | "</xsd:schema>"; |
---|
202 | |
---|
203 | String expectedResponse = |
---|
204 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
205 | "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
206 | " <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" + |
---|
207 | " <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" + |
---|
208 | " <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" + |
---|
209 | " <headers>" + |
---|
210 | " <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" + |
---|
211 | " <header key=\"Connection\" value=\"keep-alive\"/>" + |
---|
212 | " <header key=\"Host\" value=\"localhost:19098\"/>" + |
---|
213 | " <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" + |
---|
214 | " <header key=\"Content-Length\" value=\"13\"/>" + |
---|
215 | " </headers>" + |
---|
216 | " <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" + |
---|
217 | " length=\"13\">" + |
---|
218 | " <data>dummy message</data>" + |
---|
219 | " </content>" + |
---|
220 | " </request>" + |
---|
221 | " <response status=\"200\">" + |
---|
222 | " <headers>" + |
---|
223 | " <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" + |
---|
224 | " <header key=\"Content-Length\" value=\"16\"/>" + |
---|
225 | " </headers>" + |
---|
226 | " <content encoding=\"ISO-8859-1\" length=\"16\">" + |
---|
227 | " <data>response content</data>" + |
---|
228 | " </content>" + |
---|
229 | " </response>" + |
---|
230 | "</httpEvent>"; |
---|
231 | |
---|
232 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
233 | |
---|
234 | assertEquals(expectedResponse, response); |
---|
235 | |
---|
236 | proxy.stop(); |
---|
237 | proxy = null; |
---|
238 | |
---|
239 | File logFile = new File(LOG_FILE_DIR + File.separator + "httpmonitor_000.log"); |
---|
240 | |
---|
241 | assertTrue(logFile.exists()); |
---|
242 | |
---|
243 | HTTPLogParser parser = new HTTPLogParser(); |
---|
244 | |
---|
245 | parser.parseFile(logFile); |
---|
246 | |
---|
247 | // check the sequences |
---|
248 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
249 | |
---|
250 | assertNotNull(sequences); |
---|
251 | |
---|
252 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
253 | assertTrue(iterator.hasNext()); |
---|
254 | |
---|
255 | List<Event> sequence = iterator.next(); |
---|
256 | assertFalse(iterator.hasNext()); |
---|
257 | |
---|
258 | assertNotNull(sequence); |
---|
259 | assertEquals(1, sequence.size()); |
---|
260 | |
---|
261 | assertEvent(sequence.get(0), "POST", message, expectedResponse); |
---|
262 | } |
---|
263 | |
---|
264 | @Test |
---|
265 | public void test_XMLMessage_Remote() throws Exception { |
---|
266 | proxy = new HttpMonitoringProxy |
---|
267 | (new String[] { LOG_FILE_DIR, PORT +"", |
---|
268 | "localhost:" + (PORT + 1), "localhost:" + (PORT + 2) }); |
---|
269 | |
---|
270 | proxy.init(); |
---|
271 | proxy.start(); |
---|
272 | |
---|
273 | String message = |
---|
274 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
275 | "<xsd:schema targetNamespace=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
276 | " <xsd:element name=\"httpEvent\" type=\"tns:HttpEvent\" />" + |
---|
277 | "</xsd:schema>"; |
---|
278 | |
---|
279 | String expectedResponse = |
---|
280 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
---|
281 | "<httpEvent status=\"success\" xmlns=\"http://autoquest.informatik.uni-goettingen.de\">" + |
---|
282 | " <sender><ip>127.0.0.1</ip><host>127.0.0.1</host><port>42688</port></sender>" + |
---|
283 | " <receiver><ip>127.0.0.1</ip><host>127.0.0.1</host><port>19098</port></receiver>" + |
---|
284 | " <request method=\"POST\" protocol=\"HTTP/1.1\" url=\"http://localhost:19098/\">" + |
---|
285 | " <headers>" + |
---|
286 | " <header key=\"User-Agent\" value=\"Apache-HttpClient/4.2.1 (java 1.5)\"/>" + |
---|
287 | " <header key=\"Connection\" value=\"keep-alive\"/>" + |
---|
288 | " <header key=\"Host\" value=\"localhost:19098\"/>" + |
---|
289 | " <header key=\"Content-Type\" value=\"text/plain; charset=ISO-8859-1\"/>" + |
---|
290 | " <header key=\"Content-Length\" value=\"13\"/>" + |
---|
291 | " </headers>" + |
---|
292 | " <content encoding=\"ISO-8859-1\" type=\"text/plain; charset=ISO-8859-1\"" + |
---|
293 | " length=\"13\">" + |
---|
294 | " <data>dummy message</data>" + |
---|
295 | " </content>" + |
---|
296 | " </request>" + |
---|
297 | " <response status=\"200\">" + |
---|
298 | " <headers>" + |
---|
299 | " <header key=\"Server\" value=\"Jetty(9.1.0.M0)\"/>" + |
---|
300 | " <header key=\"Content-Length\" value=\"16\"/>" + |
---|
301 | " </headers>" + |
---|
302 | " <content encoding=\"ISO-8859-1\" length=\"16\">" + |
---|
303 | " <data>response content</data>" + |
---|
304 | " </content>" + |
---|
305 | " </response>" + |
---|
306 | "</httpEvent>"; |
---|
307 | |
---|
308 | String response = sendDummyMessage("POST", message, expectedResponse); |
---|
309 | |
---|
310 | assertEquals(expectedResponse, response); |
---|
311 | |
---|
312 | // give the proxy some time to send the copy of the message |
---|
313 | while(messages.size() < 1) { |
---|
314 | Thread.sleep(1000); |
---|
315 | } |
---|
316 | |
---|
317 | assertEquals(1, messages.size()); |
---|
318 | |
---|
319 | JAXBContext jaxbContext = JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
320 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
321 | |
---|
322 | @SuppressWarnings("unchecked") |
---|
323 | JAXBElement<HttpExchange> jaxObject = |
---|
324 | (JAXBElement<HttpExchange>) unmarshaller.unmarshal(new StringReader(messages.get(0))); |
---|
325 | |
---|
326 | HttpExchange exchange = jaxObject.getValue(); |
---|
327 | |
---|
328 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
329 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * |
---|
334 | */ |
---|
335 | private void assertEvent(Event event, String method, String message, String response) { |
---|
336 | assertNotNull(event); |
---|
337 | assertNotNull(event.getType()); |
---|
338 | assertNotNull(event.getTarget()); |
---|
339 | |
---|
340 | assertTrue(event.getType() instanceof HTTPEventType); |
---|
341 | assertTrue(event.getTarget() instanceof HTTPTarget); |
---|
342 | |
---|
343 | HttpExchange exchange = ((HTTPEventType) event.getType()).getExchange(); |
---|
344 | |
---|
345 | assertEquals(Method.fromValue(method), exchange.getRequest().getMethod()); |
---|
346 | assertEquals(message, exchange.getRequest().getContent().getData()); |
---|
347 | assertEquals(response, exchange.getResponse().getContent().getData()); |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * @author Patrick Harms |
---|
352 | */ |
---|
353 | private class DummyMonitorServlet extends HttpServlet { |
---|
354 | |
---|
355 | /** */ |
---|
356 | private static final long serialVersionUID = 1L; |
---|
357 | |
---|
358 | /* (non-Javadoc) |
---|
359 | * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) |
---|
360 | */ |
---|
361 | @Override |
---|
362 | protected void doPost(HttpServletRequest req, HttpServletResponse resp) |
---|
363 | throws ServletException, IOException |
---|
364 | { |
---|
365 | messages.add(readToString(req.getReader())); |
---|
366 | |
---|
367 | resp.setStatus(HttpServletResponse.SC_OK); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | } |
---|
372 | |
---|
373 | } |
---|