View Javadoc

1   // Copyright (C) 2006 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.plugin.http;
23  
24  import static java.util.Arrays.asList;
25  import static java.util.Collections.emptyList;
26  import static java.util.Collections.singletonList;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertSame;
29  import static org.mockito.Answers.RETURNS_MOCKS;
30  import static org.mockito.Mockito.when;
31  import net.grinder.common.GrinderException;
32  import net.grinder.common.SSLContextFactory;
33  import net.grinder.plugininterface.GrinderPlugin;
34  import net.grinder.plugininterface.PluginProcessContext;
35  import net.grinder.plugininterface.PluginRegistry;
36  import net.grinder.plugininterface.PluginThreadContext;
37  import net.grinder.script.Grinder.ScriptContext;
38  import net.grinder.script.Statistics;
39  import net.grinder.statistics.StatisticsServicesImplementation;
40  import net.grinder.util.InsecureSSLContextFactory;
41  import net.grinder.util.StandardTimeAuthority;
42  import net.grinder.util.TimeAuthority;
43  
44  import org.junit.Before;
45  import org.junit.Test;
46  import org.mockito.Mock;
47  import org.mockito.MockitoAnnotations;
48  
49  import HTTPClient.HTTPResponse;
50  import HTTPClient.NVPair;
51  
52  
53  /**
54   * Unit test case for <code>HTTPUtilitiesImplementation</code>.
55   *
56   * @author Philip Aston
57   */
58  public class TestHTTPUtilitiesImplementation {
59  
60    private final SSLContextFactory m_sslContextFactory =
61        new InsecureSSLContextFactory();
62  
63    @Mock private PluginProcessContext m_pluginProcessContext;
64    @Mock(answer = RETURNS_MOCKS) private ScriptContext m_scriptContext;
65    @Mock private Statistics m_statistics;
66    @Mock private PluginThreadContext m_threadContext;
67  
68    @Before public void setUp() throws Exception {
69      MockitoAnnotations.initMocks(this);
70  
71      final TimeAuthority timeAuthority = new StandardTimeAuthority();
72  
73      final HTTPPluginThreadState threadState =
74        new HTTPPluginThreadState(m_threadContext,
75                                  m_sslContextFactory,
76                                  null,
77                                  timeAuthority);
78  
79      when(m_scriptContext.getStatistics()).thenReturn(m_statistics);
80  
81      when(m_pluginProcessContext.getPluginThreadListener())
82        .thenReturn(threadState);
83  
84      when(m_pluginProcessContext.getScriptContext()).thenReturn(m_scriptContext);
85      when(m_pluginProcessContext.getStatisticsServices())
86        .thenReturn(StatisticsServicesImplementation.getInstance());
87  
88      when(m_pluginProcessContext.getTimeAuthority()).thenReturn(timeAuthority);
89  
90      new PluginRegistry() {
91        {
92          setInstance(this);
93        }
94  
95        @Override
96        public void register(final GrinderPlugin plugin) throws GrinderException {
97          plugin.initialize(m_pluginProcessContext);
98        }
99      };
100 
101     HTTPPlugin.getPlugin().initialize(m_pluginProcessContext);
102   }
103 
104   @Test public void testBasicAuthorizationHeader() throws Exception {
105     final HTTPUtilities httpUtilities =
106       new HTTPUtilitiesImplementation(m_pluginProcessContext);
107 
108     final NVPair pair =
109       httpUtilities.basicAuthorizationHeader("foo", "secret");
110     assertEquals("Authorization", pair.getName());
111     assertEquals("Basic Zm9vOnNlY3JldA==", pair.getValue());
112 
113     final NVPair pair2 =
114       httpUtilities.basicAuthorizationHeader("", "");
115     assertEquals("Authorization", pair2.getName());
116     assertEquals("Basic Og==", pair2.getValue());
117   }
118 
119   @Test public void testGetLastResponse() throws Exception {
120     final HTTPUtilities httpUtilities =
121       new HTTPUtilitiesImplementation(m_pluginProcessContext);
122 
123     assertEquals(null, httpUtilities.getLastResponse());
124 
125     // HTTPClient isn't hot on interfaces, so we can't stub these.
126     final HTTPRequestHandler handler = new HTTPRequestHandler();
127     handler.start();
128     final HTTPRequest request = new HTTPRequest();
129     final HTTPResponse httpResponse = request.GET(handler.getURL());
130 
131     assertSame(httpResponse, httpUtilities.getLastResponse());
132 
133     handler.shutdown();
134   }
135 
136   @Test public void testValueFromLocationHeader() throws Exception {
137     final HTTPRequest request = new HTTPRequest();
138 
139     final HTTPUtilities httpUtilities =
140       new HTTPUtilitiesImplementation(m_pluginProcessContext);
141     assertEquals("", httpUtilities.valueFromLocationURI("foo"));
142 
143     final HTTPRequestHandler handler = new HTTPRequestHandler();
144     handler.start();
145     request.GET(handler.getURL());
146     assertEquals("", httpUtilities.valueFromLocationURI("foo"));
147 
148     handler.addHeader("Location", "http://www.w3.org/pub/WWW/People.html");
149     request.GET(handler.getURL());
150     assertEquals("", httpUtilities.valueFromLocationURI("foo"));
151 
152     handler.clearHeaders();
153     handler.addHeader(
154       "Location", "http://www.w3.org/pub/WWW/People.html?foo=bah&lah=dah");
155     request.GET(handler.getURL());
156     assertEquals("bah", httpUtilities.valueFromLocationURI("foo"));
157     assertEquals("", httpUtilities.valueFromLocationURI("bah"));
158 
159     handler.clearHeaders();
160     handler.addHeader(
161       "Location", "http://www.w3.org/pub/WWW/People.html;foo=?foo=bah&lah=dah");
162     request.GET(handler.getURL());
163     assertEquals("", httpUtilities.valueFromLocationURI("foo"));
164     assertEquals("dah", httpUtilities.valueFromLocationURI("lah"));
165 
166     handler.clearHeaders();
167     handler.addHeader(
168       "Location", "http://www.w3.org/pub/WWW/People.html;JSESSIONID=1234");
169     request.GET(handler.getURL());
170     assertEquals("1234", httpUtilities.valueFromLocationURI("JSESSIONID"));
171     assertEquals("", httpUtilities.valueFromLocationURI("foo"));
172 
173     handler.shutdown();
174   }
175 
176   @Test public void testValueFromBodyURI() throws Exception {
177     final HTTPRequest request = new HTTPRequest();
178 
179     final HTTPUtilities httpUtilities =
180       new HTTPUtilitiesImplementation(m_pluginProcessContext);
181     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
182 
183     final HTTPRequestHandler handler = new HTTPRequestHandler();
184     handler.start();
185     request.GET(handler.getURL());
186     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
187 
188     handler.setBody(
189       "<body><a href='http://www.w3.org/pub/WWW/People.html'>foo</a></body>");
190     request.GET(handler.getURL());
191     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
192 
193     handler.setBody(
194       "<body><a href='http://www.w3.org/pub/WWW/People.html?foo=bah&lah=dah'>foo</a></body>");
195     request.GET(handler.getURL());
196     assertEquals("bah", httpUtilities.valueFromBodyURI("foo"));
197     assertEquals("", httpUtilities.valueFromBodyURI("bah"));
198 
199     handler.setBody(
200       "<body><a href='http://www.w3.org/pub/WWW/People.html;foo=?foo=bah&lah=dah'>foo</a></body>");
201     request.GET(handler.getURL());
202     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
203     assertEquals("dah", httpUtilities.valueFromBodyURI("lah"));
204 
205     handler.setBody(
206     "<body><a href='http://www.w3.org/pub/WWW/People.html;JSESSIONID=1234'>foo</a>" +
207     "<a href='http://www.w3.org/pub/WWW/People.html;JSESSIONID=5678'>foo</a></body>");
208     request.GET(handler.getURL());
209     assertEquals("1234", httpUtilities.valueFromBodyURI("JSESSIONID"));
210     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
211 
212     handler.addHeader("Content-type", "garbage");
213     request.GET(handler.getURL());
214     assertEquals("1234", httpUtilities.valueFromBodyURI("JSESSIONID"));
215     assertEquals("", httpUtilities.valueFromBodyURI("foo"));
216     assertEquals("1234", httpUtilities.valueFromBodyURI("JSESSIONID", "<body>"));
217     assertEquals("5678", httpUtilities.valueFromBodyURI("JSESSIONID", "</a>"));
218     assertEquals("", httpUtilities.valueFromBodyURI("JSESSIONID", "5"));
219     assertEquals("", httpUtilities.valueFromBodyURI("JSESSIONID", "999"));
220 
221     handler.shutdown();
222   }
223 
224   @Test public void testValuesFromBodyURI() throws Exception {
225     final HTTPRequest request = new HTTPRequest();
226 
227     final HTTPUtilities httpUtilities =
228       new HTTPUtilitiesImplementation(m_pluginProcessContext);
229     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("foo"));
230 
231     final HTTPRequestHandler handler = new HTTPRequestHandler();
232     handler.start();
233     request.GET(handler.getURL());
234     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("foo"));
235 
236     handler.setBody(
237       "<body><a href='http://www.w3.org/pub/WWW/People.html'>foo</a></body>");
238     request.GET(handler.getURL());
239     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("foo"));
240 
241     handler.setBody(
242       "<body><a href='http://www.w3.org/pub/WWW/People.html?foo=bah&lah=dah'>foo</a></body>");
243     request.GET(handler.getURL());
244     assertEquals(singletonList("bah"), httpUtilities.valuesFromBodyURI("foo"));
245     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("bah"));
246 
247     handler.setBody(
248       "<body><a href='http://www.w3.org/pub/WWW/People.html;foo=?foo=bah&lah=dah'>foo</a></body>");
249     request.GET(handler.getURL());
250     assertEquals(singletonList(""), httpUtilities.valuesFromBodyURI("foo"));
251     assertEquals(singletonList("dah"), httpUtilities.valuesFromBodyURI("lah"));
252 
253     handler.setBody(
254     "<body><a href='http://www.w3.org/pub/WWW/People.html;JSESSIONID=1234'>foo</a>" +
255     "<a href='http://www.w3.org/pub/WWW/People.html;JSESSIONID=5678'>foo</a></body>");
256     request.GET(handler.getURL());
257     assertEquals(asList("1234", "5678"),
258                  httpUtilities.valuesFromBodyURI("JSESSIONID"));
259     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("foo"));
260 
261     handler.addHeader("Content-Type", "garbage");
262     request.GET(handler.getURL());
263     assertEquals(asList("1234", "5678"),
264                  httpUtilities.valuesFromBodyURI("JSESSIONID"));
265     assertEquals(emptyList(), httpUtilities.valuesFromBodyURI("foo"));
266     assertEquals(asList("1234", "5678"),
267                  httpUtilities.valuesFromBodyURI("JSESSIONID", "<body>"));
268     assertEquals(asList("5678"),
269                  httpUtilities.valuesFromBodyURI("JSESSIONID", "</a>"));
270     assertEquals(emptyList(),
271                  httpUtilities.valuesFromBodyURI("JSESSIONID", "5"));
272     assertEquals(emptyList(),
273                  httpUtilities.valuesFromBodyURI("JSESSIONID", "999"));
274 
275     handler.shutdown();
276   }
277 
278   @Test public void testValueFromBodyInput() throws Exception {
279     final HTTPRequest request = new HTTPRequest();
280 
281     final HTTPUtilities httpUtilities =
282       new HTTPUtilitiesImplementation(m_pluginProcessContext);
283     assertEquals("", httpUtilities.valueFromBodyInput("foo"));
284 
285     final HTTPRequestHandler handler = new HTTPRequestHandler();
286     handler.start();
287     request.GET(handler.getURL());
288     assertEquals("", httpUtilities.valueFromBodyInput("foo"));
289 
290     handler.setBody("<body><input name='foo'>foo</input></body>");
291     request.GET(handler.getURL());
292     assertEquals("", httpUtilities.valueFromBodyInput("foo"));
293 
294     // input tags should be empty. The content has no meaning
295     handler.setBody("<body><input type='hidden' name='foo' value='bah'>foo</input>" +
296                     "<input name='foo' value='blah'>foo</input></body>");
297     request.GET(handler.getURL());
298     assertEquals("bah", httpUtilities.valueFromBodyInput("foo"));
299     assertEquals("", httpUtilities.valueFromBodyInput("bah"));
300     assertEquals("bah", httpUtilities.valueFromBodyInput("foo", "<body>"));
301     assertEquals("blah", httpUtilities.valueFromBodyInput("foo", "input"));
302     assertEquals("", httpUtilities.valueFromBodyInput("foo", "not there"));
303 
304     handler.shutdown();
305   }
306 
307   @Test public void testValuesFromBodyInput() throws Exception {
308     final HTTPRequest request = new HTTPRequest();
309 
310     final HTTPUtilities httpUtilities =
311       new HTTPUtilitiesImplementation(m_pluginProcessContext);
312     assertEquals(emptyList(), httpUtilities.valuesFromBodyInput("foo"));
313 
314     final HTTPRequestHandler handler = new HTTPRequestHandler();
315     handler.start();
316     request.GET(handler.getURL());
317     assertEquals(emptyList(), httpUtilities.valuesFromBodyInput("foo"));
318 
319     handler.setBody("<body><input name='foo'>foo</input></body>");
320     request.GET(handler.getURL());
321     assertEquals(emptyList(), httpUtilities.valuesFromBodyInput("foo"));
322 
323     // input tags should be empty. The content has no meaning
324     handler.setBody("<body><input name='foo' value='bah'>foo</input>" +
325                     "<input type='hidden' name='foo' value='blah'>foo</input></body>");
326     request.GET(handler.getURL());
327     assertEquals(asList("bah", "blah"),
328                  httpUtilities.valuesFromBodyInput("foo"));
329     assertEquals(emptyList(), httpUtilities.valuesFromBodyInput("bah"));
330     assertEquals(asList("blah"),
331                  httpUtilities.valuesFromBodyInput("foo", "bah"));
332     assertEquals(emptyList(),
333                  httpUtilities.valuesFromBodyInput("foo", "blah"));
334     assertEquals(emptyList(),
335                  httpUtilities.valuesFromBodyInput("foo", "not there"));
336 
337     handler.shutdown();
338   }
339 
340   @Test public void testValueFromHiddenInput() throws Exception {
341     final HTTPRequest request = new HTTPRequest();
342 
343     final HTTPUtilities httpUtilities =
344       new HTTPUtilitiesImplementation(m_pluginProcessContext);
345     assertEquals("", httpUtilities.valueFromHiddenInput("foo"));
346 
347     final HTTPRequestHandler handler = new HTTPRequestHandler();
348     handler.start();
349     request.GET(handler.getURL());
350     assertEquals("", httpUtilities.valueFromHiddenInput("foo"));
351 
352     handler.setBody("<body><input type='hidden' name='foo'>foo</input></body>");
353     request.GET(handler.getURL());
354     assertEquals("", httpUtilities.valueFromHiddenInput("foo"));
355 
356     // input tags should be empty. The content has no meaning
357     handler.setBody("<body><input name='foo' value='blah'>foo</input>" +
358                     "<input type='hidden' name='foo' value='bah'>foo</input></body>");
359     request.GET(handler.getURL());
360     assertEquals("bah", httpUtilities.valueFromHiddenInput("foo"));
361     assertEquals("", httpUtilities.valueFromHiddenInput("bah"));
362     assertEquals("bah", httpUtilities.valueFromHiddenInput("foo", "<body>"));
363     assertEquals("", httpUtilities.valueFromHiddenInput("foo", "input"));
364     assertEquals("", httpUtilities.valueFromHiddenInput("foo", "not there"));
365 
366     handler.shutdown();
367   }
368 
369   @Test public void testValuesFromHiddenInput() throws Exception {
370     final HTTPRequest request = new HTTPRequest();
371 
372     final HTTPUtilities httpUtilities =
373       new HTTPUtilitiesImplementation(m_pluginProcessContext);
374     assertEquals(emptyList(), httpUtilities.valuesFromHiddenInput("foo"));
375 
376     final HTTPRequestHandler handler = new HTTPRequestHandler();
377     handler.start();
378     request.GET(handler.getURL());
379     assertEquals(emptyList(), httpUtilities.valuesFromHiddenInput("foo"));
380 
381     handler.setBody("<body><input type='hidden' name='foo'>foo</input></body>");
382     request.GET(handler.getURL());
383     assertEquals(emptyList(), httpUtilities.valuesFromHiddenInput("foo"));
384 
385     // input tags should be empty. The content has no meaning
386     handler.setBody("<body><input type='hidden' name='foo' value='bah'>foo</input>" +
387                     "<input type='hidden' name='foo' value='blah'>foo</input></body>");
388     request.GET(handler.getURL());
389     assertEquals(asList("bah", "blah"),
390                  httpUtilities.valuesFromHiddenInput("foo"));
391     assertEquals(emptyList(), httpUtilities.valuesFromHiddenInput("bah"));
392     assertEquals(asList("blah"),
393                  httpUtilities.valuesFromHiddenInput("foo", "bah"));
394     assertEquals(emptyList(),
395                  httpUtilities.valuesFromHiddenInput("foo", "blah"));
396     assertEquals(emptyList(),
397                  httpUtilities.valuesFromHiddenInput("foo", "not there"));
398 
399     handler.shutdown();
400   }
401 }