1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.testing.framework;
29
30 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY;
31 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS;
32 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD;
33 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH;
34 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS;
35
36 import java.io.IOException;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.concurrent.TimeUnit;
40
41 import org.apache.hc.core5.http.ClassicHttpRequest;
42 import org.apache.hc.core5.http.ClassicHttpResponse;
43 import org.apache.hc.core5.http.HttpException;
44 import org.apache.hc.core5.http.HttpHost;
45 import org.apache.hc.core5.http.Method;
46 import org.apache.hc.core5.http.io.SocketConfig;
47 import org.apache.hc.core5.http.protocol.HttpContext;
48 import org.apache.hc.core5.io.CloseMode;
49 import org.apache.hc.core5.testing.classic.ClassicTestServer;
50 import org.apache.hc.core5.testing.classic.EchoHandler;
51 import org.junit.After;
52 import org.junit.Assert;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.mockito.Mockito;
56
57 public class TestClassicTestClientTestingAdapter {
58 private static final String ECHO_PATH = "echo/something";
59 private static final String CUSTOM_PATH = "custom/something";
60
61 private ClassicTestServer server;
62
63 @Before
64 public void initServer() throws Exception {
65 this.server = new ClassicTestServer(SocketConfig.custom()
66 .setSoTimeout(5, TimeUnit.SECONDS).build());
67 }
68
69 @After
70 public void shutDownServer() throws Exception {
71 if (this.server != null) {
72 this.server.shutdown(CloseMode.IMMEDIATE);
73 }
74 }
75
76 @Test
77 public void nullDefaultURI() throws Exception {
78 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
79
80 final String defaultURI = null;
81 final Map<String, Object> request = new HashMap<>();
82 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
83 final Map<String, Object> responseExpectations = new HashMap<>();
84
85 try {
86 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
87 Assert.fail("WebServerTestingFrameworkException should have been thrown");
88 } catch (final TestingFrameworkException ex) {
89
90 }
91 }
92
93 @Test
94 public void nullRequest() throws Exception {
95 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
96
97 final String defaultURI = "";
98 final Map<String, Object> request = null;
99 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
100 final Map<String, Object> responseExpectations = new HashMap<>();
101
102 try {
103 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
104 Assert.fail("WebServerTestingFrameworkException should have been thrown");
105 } catch (final TestingFrameworkException ex) {
106
107 }
108 }
109
110 @Test
111 public void nullRequestHandler() throws Exception {
112 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
113
114 final String defaultURI = "";
115 final Map<String, Object> request = new HashMap<>();
116 final TestingFrameworkRequestHandler requestHandler = null;
117 final Map<String, Object> responseExpectations = new HashMap<>();
118
119 try {
120 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
121 Assert.fail("WebServerTestingFrameworkException should have been thrown");
122 } catch (final TestingFrameworkException ex) {
123
124 }
125 }
126
127 @Test
128 public void nullResponseExpectations() throws Exception {
129 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
130
131 final String defaultURI = "";
132 final Map<String, Object> request = new HashMap<>();
133 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
134 final Map<String, Object> responseExpectations = null;
135
136 try {
137 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
138 Assert.fail("WebServerTestingFrameworkException should have been thrown");
139 } catch (final TestingFrameworkException ex) {
140
141 }
142 }
143
144 @Test
145 public void noPath() throws Exception {
146 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
147
148 final String defaultURI = "";
149 final Map<String, Object> request = new HashMap<>();
150 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
151 final Map<String, Object> responseExpectations = new HashMap<>();
152
153 try {
154 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
155 Assert.fail("WebServerTestingFrameworkException should have been thrown");
156 } catch (final TestingFrameworkException ex) {
157
158 }
159 }
160
161 @Test
162 public void noMethod() throws Exception {
163 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
164
165 final String defaultURI = "";
166
167 final Map<String, Object> request = new HashMap<>();
168 request.put(PATH, ECHO_PATH);
169
170 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
171 final Map<String, Object> responseExpectations = new HashMap<>();
172
173 try {
174 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
175 Assert.fail("WebServerTestingFrameworkException should have been thrown");
176 } catch (final TestingFrameworkException ex) {
177
178 }
179 }
180
181 @Test
182 public void invalidMethod() throws Exception {
183 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
184
185 final String defaultURI = "";
186
187 final Map<String, Object> request = new HashMap<>();
188 request.put(PATH, ECHO_PATH);
189 request.put(METHOD, "JUNK");
190
191 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
192 final Map<String, Object> responseExpectations = new HashMap<>();
193
194 try {
195 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
196 Assert.fail("WebServerTestingFrameworkException should have been thrown");
197 } catch (final TestingFrameworkException ex) {
198
199 }
200 }
201
202 @Test
203 public void withLiveServerEcho() throws Exception {
204 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
205
206
207 server.registerHandler("/echo/*", new EchoHandler());
208
209
210 this.server.start();
211 final HttpHost target = new HttpHost("localhost", this.server.getPort());
212
213 final String defaultURI = target.toString();
214 final Map<String, Object> request = new HashMap<>();
215 request.put(PATH, ECHO_PATH);
216 request.put(METHOD, Method.POST.name());
217 final String body = "mybody";
218 request.put(BODY, body);
219
220 final Map<String, Object> responseExpectations = new HashMap<>();
221
222 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
223 final Map<String, Object> response = adapter.execute(defaultURI, request, requestHandler, responseExpectations);
224
225 Assert.assertNotNull("response should not be null", response);
226 Assert.assertEquals("status unexpected", 200, response.get(STATUS));
227
228 @SuppressWarnings("unchecked")
229 final Map<String, Object> headers = (Map<String, Object>) response.get(HEADERS);
230 Assert.assertNotNull("headers should be in the response", headers);
231 Assert.assertFalse(headers.isEmpty());
232
233 final String returnedBody = (String) response.get(BODY);
234 Assert.assertNotNull("body should be in the response", returnedBody);
235 Assert.assertEquals("Body should be echoed", body, returnedBody);
236
237 }
238
239 @Test
240 public void withLiveServerCustomRequestHandler() throws Exception {
241 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
242
243 final TestingFrameworkRequestHandler requestHandler = new TestingFrameworkRequestHandler() {
244 @Override
245 public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context)
246 throws HttpException, IOException {
247 try {
248 Assert.assertEquals("method not expected", "junk", request.getMethod());
249 } catch (final Throwable t) {
250 thrown = t;
251 }
252 }
253 };
254 server.registerHandler("/custom/*", requestHandler);
255
256 this.server.start();
257 final HttpHost target = new HttpHost("localhost", this.server.getPort());
258 final String defaultURI = target.toString();
259 final Map<String, Object> responseExpectations = new HashMap<>();
260
261 final Map<String, Object> request = new HashMap<>();
262 request.put(PATH, CUSTOM_PATH);
263
264 for (final String method : TestingFramework.ALL_METHODS) {
265 request.put(METHOD, method);
266
267 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
268 }
269 }
270
271 @Test
272 public void modifyRequest() {
273 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
274
275 final Map<String, Object> request = new HashMap<>();
276 final Map<String, Object> returnedRequest = adapter.modifyRequest(request);
277
278 Assert.assertSame("Same request was not returned as expected.", request, returnedRequest);
279 }
280
281 @Test
282 public void modifyResponseExpectations() {
283 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
284
285 final Map<String, Object> responseExpectations = new HashMap<>();
286 final Map<String, Object> returnedResponseExpectations = adapter.modifyResponseExpectations(null, responseExpectations);
287
288 Assert.assertSame("Same response expectations were not returned as expected.", responseExpectations, returnedResponseExpectations);
289 }
290
291 }