Skip to content

Commit ce94f60

Browse files
committed
More tests
1 parent b48e322 commit ce94f60

File tree

5 files changed

+126
-83
lines changed

5 files changed

+126
-83
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ dependencies {
6161
// =============== HTTP CLIENT =================
6262

6363
// https://mvnrepository.com/artifact/org.asynchttpclient/async-http-client
64-
compile group: 'org.asynchttpclient', name: 'async-http-client', version: '2.10.3'
64+
compile group: 'org.asynchttpclient', name: 'async-http-client', version: '2.10.4'
6565

6666
// ================= MOLECULER =================
6767

6868
// Moleculer web (required)
69-
compile group: 'com.github.berkesa', name: 'moleculer-java-web', version: '1.2.4'
69+
compile group: 'com.github.berkesa', name: 'moleculer-java-web', version: '1.2.5'
7070

7171
// ================== LOGGING ==================
7272

@@ -123,7 +123,7 @@ modifyPom {
123123
dependency {
124124
groupId 'org.asynchttpclient'
125125
artifactId 'async-http-client'
126-
version '2.10.3'
126+
version '2.10.4'
127127
scope 'runtime'
128128
}
129129
}

src/main/java/services/moleculer/httpclient/HttpClient.java

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public class HttpClient extends DefaultAsyncHttpClientConfig.Builder {
120120
* Shut down ScheduledExecutorService on stop().
121121
*/
122122
protected boolean shutDownThreadPools;
123-
123+
124124
// --- INIT HTTP CLIENT ---
125125

126126
public void start() {
@@ -221,21 +221,7 @@ public Promise get(String url, Tree request, Consumer<RequestParams> configurato
221221
* @return {@link Promise}
222222
*/
223223
public Promise connect(String url) {
224-
return connect(url, null, null);
225-
}
226-
227-
/**
228-
* Executes an HTTP CONNECT request.
229-
*
230-
* @param url
231-
* A well formed URL.
232-
* @param request
233-
* Request parameters in a Tree
234-
*
235-
* @return {@link Promise}
236-
*/
237-
public Promise connect(String url, Tree request) {
238-
return connect(url, request, null);
224+
return connect(url, null);
239225
}
240226

241227
/**
@@ -249,23 +235,7 @@ public Promise connect(String url, Tree request) {
249235
* @return {@link Promise}
250236
*/
251237
public Promise connect(String url, Consumer<RequestParams> configurator) {
252-
return connect(url, null, configurator);
253-
}
254-
255-
/**
256-
* Executes an HTTP CONNECT request.
257-
*
258-
* @param url
259-
* A well formed URL.
260-
* @param request
261-
* Request parameters in a Tree
262-
* @param configurator
263-
* Consumer for set the parameters of the request
264-
*
265-
* @return {@link Promise}
266-
*/
267-
public Promise connect(String url, Tree request, Consumer<RequestParams> configurator) {
268-
return execute(url, "CONNECT", new TreeConfigurator(configurator, request, false));
238+
return execute(url, "CONNECT", configurator);
269239
}
270240

271241
/**
@@ -321,7 +291,7 @@ public Promise options(String url, Consumer<RequestParams> configurator) {
321291
* @return {@link Promise}
322292
*/
323293
public Promise options(String url, Tree request, Consumer<RequestParams> configurator) {
324-
return execute(url, "OPTIONS", new TreeConfigurator(configurator, request, true));
294+
return execute(url, "OPTIONS", new TreeConfigurator(configurator, request, false));
325295
}
326296

327297
/**
@@ -333,21 +303,7 @@ public Promise options(String url, Tree request, Consumer<RequestParams> configu
333303
* @return {@link Promise}
334304
*/
335305
public Promise head(String url) {
336-
return head(url, null, null);
337-
}
338-
339-
/**
340-
* Executes an HTTP HEAD request.
341-
*
342-
* @param url
343-
* A well formed URL.
344-
* @param request
345-
* Request parameters in a Tree
346-
*
347-
* @return {@link Promise}
348-
*/
349-
public Promise head(String url, Tree request) {
350-
return head(url, request, null);
306+
return head(url, null);
351307
}
352308

353309
/**
@@ -361,23 +317,7 @@ public Promise head(String url, Tree request) {
361317
* @return {@link Promise}
362318
*/
363319
public Promise head(String url, Consumer<RequestParams> configurator) {
364-
return head(url, null, configurator);
365-
}
366-
367-
/**
368-
* Executes an HTTP HEAD request.
369-
*
370-
* @param url
371-
* A well formed URL.
372-
* @param request
373-
* Request parameters in a Tree
374-
* @param configurator
375-
* Consumer for set the parameters of the request
376-
*
377-
* @return {@link Promise}
378-
*/
379-
public Promise head(String url, Tree request, Consumer<RequestParams> configurator) {
380-
return execute(url, "HEAD", new TreeConfigurator(configurator, request, false));
320+
return execute(url, "HEAD", configurator);
381321
}
382322

383323
/**
@@ -717,7 +657,7 @@ public Promise trace(String url, Consumer<RequestParams> configurator) {
717657
* @return {@link Promise}
718658
*/
719659
public Promise trace(String url, Tree request, Consumer<RequestParams> configurator) {
720-
return execute(url, "TRACE", new TreeConfigurator(configurator, request, true));
660+
return execute(url, "TRACE", new TreeConfigurator(configurator, request, false));
721661
}
722662

723663
// --- WEBSOCKET LISTENER / RECEIVER ---
@@ -753,7 +693,7 @@ protected Promise execute(String url, String method, Consumer<RequestParams> con
753693
}
754694
if (params.handler == null) {
755695
params.handler = new ResponseToJson(params);
756-
}
696+
}
757697
return new Promise(res -> {
758698
client.executeRequest(params.build(), new AsyncHandler<Void>() {
759699

src/main/java/services/moleculer/httpclient/ResponseToBytes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception
5757
buffer.get(bytes, 0, len);
5858
} else {
5959
byte[] expanded = new byte[bytes.length + len];
60-
System.arraycopy(bytes, 0, expanded, 0, bytes.length);
6160
buffer.get(expanded, bytes.length, len);
61+
System.arraycopy(bytes, 0, expanded, 0, bytes.length);
62+
bytes = expanded;
6263
}
6364
return State.CONTINUE;
6465
}

src/main/java/services/moleculer/httpclient/ResponseToJson.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Tree onCompleted() throws Exception {
4545
if (bytes.length == 0) {
4646
rsp = new Tree();
4747
} else {
48-
rsp = new Tree(bytes);
48+
rsp = new Tree(bytes);
4949
}
5050
addStatusAndHeaders(rsp);
5151
return rsp;

src/test/java/services/moleculer/httpclient/HttpClientTest.java

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.concurrent.TimeUnit;
3030
import java.util.concurrent.atomic.AtomicBoolean;
3131
import java.util.concurrent.atomic.AtomicReference;
32+
import java.util.function.Consumer;
3233

3334
import org.asynchttpclient.ws.WebSocket;
3435
import org.junit.Test;
@@ -78,6 +79,10 @@ protected void setUp() throws Exception {
7879
}
7980
Tree meta = data.getMeta();
8081
meta.put("method", req.getMethod());
82+
meta.put("path", req.getPath());
83+
meta.put("query", req.getQuery());
84+
meta.put("multipart", req.isMultipart());
85+
meta.put("address", req.getAddress());
8186
Iterator<String> headers = req.getHeaders();
8287
while (headers.hasNext()) {
8388
String header = headers.next();
@@ -90,7 +95,6 @@ protected void setUp() throws Exception {
9095
Action action = ctx -> {
9196
lastCtx.set(ctx);
9297
return ctx.params;
93-
9498
};
9599

96100
});
@@ -194,7 +198,7 @@ public void testHttpClientAPI() throws Exception {
194198

195199
// Connect via WebSocket
196200
WebSocketConnection ws = cl.ws("http://127.0.0.1:8080/ws/test", new WebSocketHandler() {
197-
201+
198202
@Override
199203
public void onMessage(Tree message) {
200204
System.out.println("WebSocket message received: " + message);
@@ -217,16 +221,16 @@ public void onClose(WebSocket webSocket, int code, String reason) {
217221
con[0] = false;
218222
System.out.println("Disconnected");
219223
}
220-
224+
221225
}, params -> {
222-
226+
223227
params.setHeartbeatInterval(30);
224228
params.setHeartbeatTimeout(10);
225229
params.setReconnectDelay(5);
226-
230+
227231
params.setHeader("CustomHeader", "CustomValue");
228-
229-
});
232+
233+
});
230234
ws.waitForConnection(20, TimeUnit.SECONDS);
231235
assertTrue(con[0]);
232236

@@ -249,7 +253,7 @@ public void onClose(WebSocket webSocket, int code, String reason) {
249253
StreamReceiver receiver = (StreamReceiver) br.getLocalService("streamReceiver");
250254
PacketStream sender = br.createStream();
251255

252-
Promise p = cl.post(RECEIVER_URL, sender);
256+
cl.post(RECEIVER_URL, sender);
253257

254258
sender.sendData("123".getBytes());
255259
Thread.sleep(1000);
@@ -278,8 +282,106 @@ public void onClose(WebSocket webSocket, int code, String reason) {
278282
receiver.buffer.reset();
279283
receiver.closed.set(false);
280284

281-
rsp = p.waitFor(2000);
282-
System.out.println(rsp);
285+
Consumer<RequestParams> returnAll = new Consumer<RequestParams>() {
286+
287+
@Override
288+
public void accept(RequestParams params) {
289+
params.returnStatusCode().returnHttpHeaders();
290+
}
291+
292+
};
293+
294+
// GET
295+
check("GET", null, false, cl.get(TEST_URL));
296+
check("GET", req, false, cl.get(TEST_URL, req));
297+
check("GET", null, true, cl.get(TEST_URL, returnAll));
298+
check("GET", req, true, cl.get(TEST_URL, req, returnAll));
299+
300+
// CONNECT
301+
check("CONNECT", null, false, cl.connect(TEST_URL));
302+
check("CONNECT", null, true, cl.connect(TEST_URL, returnAll));
303+
304+
// OPTIONS
305+
check("OPTIONS", null, false, cl.options(TEST_URL));
306+
check("OPTIONS", req, false, cl.options(TEST_URL, req));
307+
check("OPTIONS", null, true, cl.options(TEST_URL, returnAll));
308+
check("OPTIONS", req, true, cl.options(TEST_URL, req, returnAll));
309+
310+
// HEAD
311+
check("HEAD", null, false, cl.head(TEST_URL));
312+
check("HEAD", null, true, cl.head(TEST_URL, returnAll));
313+
314+
// POST
315+
check("POST", null, false, cl.post(TEST_URL));
316+
check("POST", req, false, cl.post(TEST_URL, req));
317+
check("POST", null, true, cl.post(TEST_URL, returnAll));
318+
check("POST", req, true, cl.post(TEST_URL, req, returnAll));
319+
320+
// PUT
321+
check("PUT", null, false, cl.put(TEST_URL));
322+
check("PUT", req, false, cl.put(TEST_URL, req));
323+
check("PUT", null, true, cl.put(TEST_URL, returnAll));
324+
check("PUT", req, true, cl.put(TEST_URL, req, returnAll));
325+
326+
// DELETE
327+
check("DELETE", null, false, cl.delete(TEST_URL));
328+
check("DELETE", req, false, cl.delete(TEST_URL, req));
329+
check("DELETE", null, true, cl.delete(TEST_URL, returnAll));
330+
check("DELETE", req, true, cl.delete(TEST_URL, req, returnAll));
331+
332+
// PATCH
333+
check("PATCH", null, false, cl.patch(TEST_URL));
334+
check("PATCH", req, false, cl.patch(TEST_URL, req));
335+
check("PATCH", null, true, cl.patch(TEST_URL, returnAll));
336+
check("PATCH", req, true, cl.patch(TEST_URL, req, returnAll));
337+
338+
// TRACE
339+
check("TRACE", null, false, cl.trace(TEST_URL));
340+
check("TRACE", req, false, cl.trace(TEST_URL, req));
341+
check("TRACE", null, true, cl.trace(TEST_URL, returnAll));
342+
check("TRACE", req, true, cl.trace(TEST_URL, req, returnAll));
343+
344+
}
345+
346+
private void check(String method, Tree request, boolean returnAll, Promise responsePromise) throws Exception {
347+
long start = System.currentTimeMillis();
348+
System.out.println("Testing " + method + " method...");
349+
Tree rsp = responsePromise.waitFor(1000);
350+
long duration = System.currentTimeMillis() - start;
351+
assertTrue(duration < 100);
352+
if ("CONNECT".equals(method)) {
353+
assertEquals("SERVICE_NOT_FOUND_ERROR", rsp.get("type", ""));
354+
return;
355+
}
356+
Context ctx = reset();
357+
if (request != null) {
358+
String s = request.toString(null, false, false).replace("\"", "");
359+
assertEquals(s, rsp.toString(null, false, false).replace("\"", ""));
360+
assertEquals(s, ctx.params.toString(null, false, false).replace("\"", ""));
361+
}
362+
if (returnAll) {
363+
assertOk(rsp);
364+
assertRestResponse(rsp);
365+
} else {
366+
assertNull(rsp.getMeta(false));
367+
}
368+
if (request == null) {
369+
assertTrue(rsp.isEmpty());
370+
} else {
371+
assertFalse(rsp.isEmpty());
372+
}
373+
Tree meta = ctx.params.getMeta();
374+
assertFalse(meta.isEmpty());
375+
assertEquals(method, meta.get("method", ""));
376+
assertTrue(meta.get("path", "").startsWith("/test.action"));
377+
if (request != null && ("GET".equals(method) || "CONNECT".equals(method) || "HEAD".equals(method)
378+
|| "OPTIONS".equals(method) || "TRACE".equals(method))) {
379+
assertFalse(meta.get("query", "").isEmpty());
380+
} else {
381+
assertNull(meta.get("query", (String) null));
382+
}
383+
assertFalse(meta.get("multipart", true));
384+
assertFalse(meta.get("address", "").isEmpty());
283385
}
284386

285387
}

0 commit comments

Comments
 (0)