1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
44
45
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 }