View Javadoc

1   // Copyright (C) 2007 - 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 org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNotSame;
26  import static org.junit.Assert.assertSame;
27  import net.grinder.common.SSLContextFactory;
28  import net.grinder.plugininterface.PluginThreadContext;
29  import net.grinder.util.InsecureSSLContextFactory;
30  import net.grinder.util.Sleeper;
31  
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.mockito.Mock;
35  import org.mockito.MockitoAnnotations;
36  
37  import HTTPClient.HTTPConnection;
38  import HTTPClient.HTTPResponse;
39  import HTTPClient.URI;
40  
41  
42  /**
43   * Unit tests for {@link HTTPPluginThreadState}.
44   *
45   * @author Philip Aston
46   */
47  public class TestHTTPPluginThreadState {
48  
49    private final SSLContextFactory m_sslContextFactory =
50        new InsecureSSLContextFactory();
51  
52    @Mock private PluginThreadContext m_threadContext;
53  
54    @Mock private Sleeper m_sleeper;
55  
56    @Before
57    public void setUp() throws Exception {
58      MockitoAnnotations.initMocks(this);
59    }
60  
61    @Test
62    public void testHTTPPluginThreadState() throws Exception {
63      final HTTPPluginThreadState pluginThreadState =
64        new HTTPPluginThreadState(m_threadContext,
65                                  m_sslContextFactory,
66                                  m_sleeper,
67                                  null);
68  
69      assertSame(m_threadContext, pluginThreadState.getThreadContext());
70  
71      pluginThreadState.beginThread();
72  
73      pluginThreadState.beginRun();
74  
75      pluginThreadState.endRun();
76  
77      pluginThreadState.beginRun();
78  
79      final HTTPConnectionWrapper wrapper1 =
80        pluginThreadState.getConnectionWrapper(new URI("http://blah.com"));
81  
82      assertNotNull(wrapper1);
83      assertNotNull(m_sslContextFactory.getSSLContext().getSocketFactory());
84  
85      final HTTPConnectionWrapper wrapper2 =
86        pluginThreadState.getConnectionWrapper(new URI("https://secure.com"));
87  
88      assertNotNull(wrapper2);
89  
90      final HTTPConnectionWrapper wrapper3 =
91        pluginThreadState.getConnectionWrapper(new URI("http://blah.com/lah"));
92  
93      assertSame(wrapper1, wrapper3);
94      assertNotSame(wrapper2, wrapper3);
95  
96      pluginThreadState.endRun();
97  
98      pluginThreadState.beginRun();
99  
100     final HTTPConnectionWrapper wrapper4 =
101       pluginThreadState.getConnectionWrapper(new URI("http://blah.com/lah"));
102 
103     assertNotSame(wrapper1, wrapper4);
104 
105     pluginThreadState.endRun();
106 
107     pluginThreadState.endThread();
108 
109     pluginThreadState.beginShutdown();
110   }
111 
112   @Test public void testSetResponse() throws Exception {
113     final HTTPRequestHandler handler = new HTTPRequestHandler();
114     handler.start();
115 
116     try {
117       final HTTPConnection connection =
118         new HTTPConnection(new URI(handler.getURL()));
119 
120       final HTTPPluginThreadState pluginThreadState =
121         new HTTPPluginThreadState(m_threadContext,
122                                   m_sslContextFactory,
123                                   m_sleeper,
124                                   null);
125 
126       final HTTPResponse response = connection.Get("foo");
127 
128       pluginThreadState.setLastResponse(response);
129 
130       assertSame(response, pluginThreadState.getLastResponse());
131     }
132     finally {
133       handler.shutdown();
134     }
135   }
136 }