29
29
import java .util .concurrent .TimeUnit ;
30
30
import java .util .concurrent .atomic .AtomicBoolean ;
31
31
import java .util .concurrent .atomic .AtomicReference ;
32
+ import java .util .function .Consumer ;
32
33
33
34
import org .asynchttpclient .ws .WebSocket ;
34
35
import org .junit .Test ;
@@ -78,6 +79,10 @@ protected void setUp() throws Exception {
78
79
}
79
80
Tree meta = data .getMeta ();
80
81
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 ());
81
86
Iterator <String > headers = req .getHeaders ();
82
87
while (headers .hasNext ()) {
83
88
String header = headers .next ();
@@ -90,7 +95,6 @@ protected void setUp() throws Exception {
90
95
Action action = ctx -> {
91
96
lastCtx .set (ctx );
92
97
return ctx .params ;
93
-
94
98
};
95
99
96
100
});
@@ -194,7 +198,7 @@ public void testHttpClientAPI() throws Exception {
194
198
195
199
// Connect via WebSocket
196
200
WebSocketConnection ws = cl .ws ("http://127.0.0.1:8080/ws/test" , new WebSocketHandler () {
197
-
201
+
198
202
@ Override
199
203
public void onMessage (Tree message ) {
200
204
System .out .println ("WebSocket message received: " + message );
@@ -217,16 +221,16 @@ public void onClose(WebSocket webSocket, int code, String reason) {
217
221
con [0 ] = false ;
218
222
System .out .println ("Disconnected" );
219
223
}
220
-
224
+
221
225
}, params -> {
222
-
226
+
223
227
params .setHeartbeatInterval (30 );
224
228
params .setHeartbeatTimeout (10 );
225
229
params .setReconnectDelay (5 );
226
-
230
+
227
231
params .setHeader ("CustomHeader" , "CustomValue" );
228
-
229
- });
232
+
233
+ });
230
234
ws .waitForConnection (20 , TimeUnit .SECONDS );
231
235
assertTrue (con [0 ]);
232
236
@@ -249,7 +253,7 @@ public void onClose(WebSocket webSocket, int code, String reason) {
249
253
StreamReceiver receiver = (StreamReceiver ) br .getLocalService ("streamReceiver" );
250
254
PacketStream sender = br .createStream ();
251
255
252
- Promise p = cl .post (RECEIVER_URL , sender );
256
+ cl .post (RECEIVER_URL , sender );
253
257
254
258
sender .sendData ("123" .getBytes ());
255
259
Thread .sleep (1000 );
@@ -278,8 +282,106 @@ public void onClose(WebSocket webSocket, int code, String reason) {
278
282
receiver .buffer .reset ();
279
283
receiver .closed .set (false );
280
284
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 ());
283
385
}
284
386
285
387
}
0 commit comments