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.jupiter.api.AfterEach;
52 import org.junit.jupiter.api.Assertions;
53 import org.junit.jupiter.api.BeforeEach;
54 import org.junit.jupiter.api.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 @BeforeEach
64 public void initServer() throws Exception {
65 this.server = new ClassicTestServer(SocketConfig.custom()
66 .setSoTimeout(5, TimeUnit.SECONDS).build());
67 }
68
69 @AfterEach
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 Assertions.assertThrows(TestingFrameworkException.class, () ->
86 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
87 }
88
89 @Test
90 public void nullRequest() throws Exception {
91 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
92
93 final String defaultURI = "";
94 final Map<String, Object> request = null;
95 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
96 final Map<String, Object> responseExpectations = new HashMap<>();
97
98 Assertions.assertThrows(TestingFrameworkException.class, () ->
99 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
100 }
101
102 @Test
103 public void nullRequestHandler() throws Exception {
104 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
105
106 final String defaultURI = "";
107 final Map<String, Object> request = new HashMap<>();
108 final TestingFrameworkRequestHandler requestHandler = null;
109 final Map<String, Object> responseExpectations = new HashMap<>();
110
111 Assertions.assertThrows(TestingFrameworkException.class, () ->
112 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
113 }
114
115 @Test
116 public void nullResponseExpectations() throws Exception {
117 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
118
119 final String defaultURI = "";
120 final Map<String, Object> request = new HashMap<>();
121 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
122 final Map<String, Object> responseExpectations = null;
123
124 Assertions.assertThrows(TestingFrameworkException.class, () ->
125 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
126 }
127
128 @Test
129 public void noPath() throws Exception {
130 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
131
132 final String defaultURI = "";
133 final Map<String, Object> request = new HashMap<>();
134 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
135 final Map<String, Object> responseExpectations = new HashMap<>();
136
137 Assertions.assertThrows(TestingFrameworkException.class, () ->
138 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
139 }
140
141 @Test
142 public void noMethod() throws Exception {
143 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
144
145 final String defaultURI = "";
146
147 final Map<String, Object> request = new HashMap<>();
148 request.put(PATH, ECHO_PATH);
149
150 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
151 final Map<String, Object> responseExpectations = new HashMap<>();
152
153 Assertions.assertThrows(TestingFrameworkException.class, () ->
154 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
155 }
156
157 @Test
158 public void invalidMethod() throws Exception {
159 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
160
161 final String defaultURI = "";
162
163 final Map<String, Object> request = new HashMap<>();
164 request.put(PATH, ECHO_PATH);
165 request.put(METHOD, "JUNK");
166
167 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
168 final Map<String, Object> responseExpectations = new HashMap<>();
169
170 Assertions.assertThrows(TestingFrameworkException.class, () ->
171 adapter.execute(defaultURI, request, requestHandler, responseExpectations));
172 }
173
174 @Test
175 public void withLiveServerEcho() throws Exception {
176 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
177
178
179 server.registerHandler("/echo/*", new EchoHandler());
180
181
182 this.server.start();
183 final HttpHost target = new HttpHost("localhost", this.server.getPort());
184
185 final String defaultURI = target.toString();
186 final Map<String, Object> request = new HashMap<>();
187 request.put(PATH, ECHO_PATH);
188 request.put(METHOD, Method.POST.name());
189 final String body = "mybody";
190 request.put(BODY, body);
191
192 final Map<String, Object> responseExpectations = new HashMap<>();
193
194 final TestingFrameworkRequestHandler requestHandler = Mockito.mock(TestingFrameworkRequestHandler.class);
195 final Map<String, Object> response = adapter.execute(defaultURI, request, requestHandler, responseExpectations);
196
197 Assertions.assertNotNull(response, "response should not be null");
198 Assertions.assertEquals(200, response.get(STATUS), "status unexpected");
199
200 @SuppressWarnings("unchecked")
201 final Map<String, Object> headers = (Map<String, Object>) response.get(HEADERS);
202 Assertions.assertNotNull(headers, "headers should be in the response");
203 Assertions.assertFalse(headers.isEmpty());
204
205 final String returnedBody = (String) response.get(BODY);
206 Assertions.assertNotNull(returnedBody, "body should be in the response");
207 Assertions.assertEquals(body, returnedBody, "Body should be echoed");
208
209 }
210
211 @Test
212 public void withLiveServerCustomRequestHandler() throws Exception {
213 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
214
215 final TestingFrameworkRequestHandler requestHandler = new TestingFrameworkRequestHandler() {
216 @Override
217 public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context)
218 throws HttpException, IOException {
219 try {
220 Assertions.assertEquals("method not expected", "junk", request.getMethod());
221 } catch (final Throwable t) {
222 thrown = t;
223 }
224 }
225 };
226 server.registerHandler("/custom/*", requestHandler);
227
228 this.server.start();
229 final HttpHost target = new HttpHost("localhost", this.server.getPort());
230 final String defaultURI = target.toString();
231 final Map<String, Object> responseExpectations = new HashMap<>();
232
233 final Map<String, Object> request = new HashMap<>();
234 request.put(PATH, CUSTOM_PATH);
235
236 for (final String method : TestingFramework.ALL_METHODS) {
237 request.put(METHOD, method);
238
239 adapter.execute(defaultURI, request, requestHandler, responseExpectations);
240 }
241 }
242
243 @Test
244 public void modifyRequest() {
245 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
246
247 final Map<String, Object> request = new HashMap<>();
248 final Map<String, Object> returnedRequest = adapter.modifyRequest(request);
249
250 Assertions.assertSame(request, returnedRequest, "Same request was not returned as expected.");
251 }
252
253 @Test
254 public void modifyResponseExpectations() {
255 final ClientTestingAdapter adapter = new ClassicTestClientTestingAdapter();
256
257 final Map<String, Object> responseExpectations = new HashMap<>();
258 final Map<String, Object> returnedResponseExpectations = adapter.modifyResponseExpectations(null, responseExpectations);
259
260 Assertions.assertSame(responseExpectations, returnedResponseExpectations, "Same response expectations were not returned as expected.");
261 }
262
263 }