55 */
66package io .jooby .test ;
77
8+ import static okhttp3 .RequestBody .create ;
9+
810import java .io .IOException ;
911import java .net .SocketTimeoutException ;
1012import java .nio .charset .StandardCharsets ;
1416import java .security .cert .X509Certificate ;
1517import java .util .ArrayList ;
1618import java .util .List ;
19+ import java .util .Map ;
1720import java .util .concurrent .*;
1821import java .util .concurrent .atomic .AtomicBoolean ;
1922import java .util .function .BiConsumer ;
2326import javax .net .ssl .TrustManager ;
2427import javax .net .ssl .X509TrustManager ;
2528
26- import org .jetbrains .annotations .NotNull ;
27- import org .jetbrains .annotations .Nullable ;
29+ import org .jspecify .annotations .Nullable ;
2830
29- import edu .umd .cs .findbugs .annotations .NonNull ;
3031import io .jooby .Server ;
3132import io .jooby .ServerSentMessage ;
3233import io .jooby .SneakyThrows ;
3334import io .jooby .WebSocketCloseStatus ;
34- import okhttp3 .Headers ;
35- import okhttp3 .OkHttpClient ;
36- import okhttp3 .RequestBody ;
37- import okhttp3 .Response ;
38- import okhttp3 .WebSocket ;
39- import okhttp3 .WebSocketListener ;
35+ import okhttp3 .*;
4036import okhttp3 .sse .EventSource ;
4137import okhttp3 .sse .EventSourceListener ;
4238import okhttp3 .sse .EventSources ;
@@ -59,31 +55,30 @@ public SyncWebSocketListener(String testName) {
5955 }
6056
6157 @ Override
62- public void onOpen (@ NotNull WebSocket webSocket , @ NotNull Response response ) {
58+ public void onOpen (WebSocket webSocket , Response response ) {
6359 opened .countDown ();
6460 }
6561
6662 @ Override
67- public void onClosed (@ NotNull WebSocket webSocket , int code , @ NotNull String reason ) {
63+ public void onClosed (WebSocket webSocket , int code , String reason ) {
6864 closed .set (true );
6965 }
7066
7167 @ Override
72- public void onFailure (
73- @ NotNull WebSocket webSocket , @ NotNull Throwable e , @ Nullable Response response ) {
68+ public void onFailure (WebSocket webSocket , Throwable e , @ Nullable Response response ) {
7469 if (!Server .connectionLost (e )) {
7570 System .err .println ("Unexpected web socket error: " + testName );
7671 e .printStackTrace ();
7772 }
7873 }
7974
8075 @ Override
81- public void onMessage (@ NotNull WebSocket webSocket , @ NotNull String text ) {
76+ public void onMessage (WebSocket webSocket , String text ) {
8277 messages .offer (text );
8378 }
8479
8580 @ Override
86- public void onMessage (@ NonNull WebSocket webSocket , @ NonNull ByteString bytes ) {
81+ public void onMessage (WebSocket webSocket , ByteString bytes ) {
8782 messages .offer (new String (bytes .toByteArray (), StandardCharsets .UTF_8 ));
8883 }
8984
@@ -96,7 +91,7 @@ public String lastMessage() {
9691 }
9792
9893 @ Override
99- public void onClosing (@ NotNull WebSocket webSocket , int code , @ NotNull String reason ) {
94+ public void onClosing (WebSocket webSocket , int code , String reason ) {
10095 super .onClosing (webSocket , code , reason );
10196 }
10297 }
@@ -232,10 +227,16 @@ public Request invoke(String method, String path) {
232227 }
233228
234229 public Request invoke (String method , String path , RequestBody body ) {
235- okhttp3 .Request .Builder req = new okhttp3 .Request .Builder ();
230+ return invoke (method , path , Map .of (), body );
231+ }
232+
233+ public Request invoke (String method , String path , Map <String , Object > query , RequestBody body ) {
234+ var req = new okhttp3 .Request .Builder ();
236235 req .method (method , body );
237236 setRequestHeaders (req );
238- req .url (scheme + "://localhost:" + port + path );
237+ var url = HttpUrl .parse (scheme + "://localhost:" + port + path ).newBuilder ();
238+ query .forEach ((name , value ) -> url .addQueryParameter (name , value .toString ()));
239+ req .url (url .build ());
239240 return new Request (req );
240241 }
241242
@@ -252,7 +253,11 @@ private void setRequestHeaders(okhttp3.Request.Builder req) {
252253 }
253254
254255 public Request get (String path ) {
255- return invoke ("GET" , path , null );
256+ return get (path , Map .of ());
257+ }
258+
259+ public Request get (String path , Map <String , Object > query ) {
260+ return invoke ("GET" , path , query , null );
256261 }
257262
258263 public ServerSentMessageIterator sse (String path ) {
@@ -266,31 +271,29 @@ public ServerSentMessageIterator sse(String path) {
266271 req .build (),
267272 new EventSourceListener () {
268273 @ Override
269- public void onClosed (@ NotNull EventSource eventSource ) {
274+ public void onClosed (EventSource eventSource ) {
270275 eventSource .cancel ();
271276 }
272277
273278 @ Override
274279 public void onEvent (
275- @ NotNull EventSource eventSource ,
280+ EventSource eventSource ,
276281 @ Nullable String id ,
277282 @ Nullable String type ,
278- @ NotNull String data ) {
283+ String data ) {
279284 // retry is not part of public API
280285 ServerSentMessage message = new ServerSentMessage (data ).setId (id ).setEvent (type );
281286 messages .offer (message );
282287 }
283288
284289 @ Override
285290 public void onFailure (
286- @ NotNull EventSource eventSource ,
287- @ Nullable Throwable t ,
288- @ Nullable Response response ) {
291+ EventSource eventSource , @ Nullable Throwable t , @ Nullable Response response ) {
289292 super .onFailure (eventSource , t , response );
290293 }
291294
292295 @ Override
293- public void onOpen (@ NotNull EventSource eventSource , @ NotNull Response response ) {
296+ public void onOpen (EventSource eventSource , Response response ) {
294297 super .onOpen (eventSource , response );
295298 }
296299 });
@@ -301,6 +304,11 @@ public void get(String path, SneakyThrows.Consumer<Response> callback) {
301304 get (path ).execute (callback );
302305 }
303306
307+ public void get (
308+ String path , Map <String , Object > query , SneakyThrows .Consumer <Response > callback ) {
309+ get (path , query ).execute (callback );
310+ }
311+
304312 public void syncWebSocket (String path , SneakyThrows .Consumer <BlockingWebSocket > consumer ) {
305313 okhttp3 .Request .Builder req = new okhttp3 .Request .Builder ();
306314 req .url ("ws://localhost:" + port + path );
@@ -318,6 +326,14 @@ public void syncWebSocket(String path, SneakyThrows.Consumer<BlockingWebSocket>
318326 blockingWebSocket .close ();
319327 }
320328
329+ public WebSocket webSocket (String path , WebSocketListener listener ) {
330+ okhttp3 .Request .Builder req = new okhttp3 .Request .Builder ();
331+ req .url ("ws://localhost:" + port + path );
332+ setRequestHeaders (req );
333+ okhttp3 .Request r = req .build ();
334+ return client .newWebSocket (r , listener );
335+ }
336+
321337 public Request options (String path ) {
322338 return invoke ("OPTIONS" , path , null );
323339 }
@@ -358,6 +374,10 @@ public void post(String path, RequestBody form, SneakyThrows.Consumer<Response>
358374 post (path , form ).execute (callback );
359375 }
360376
377+ public void postJson (String path , String json , SneakyThrows .Consumer <Response > callback ) {
378+ post (path , create (json , MediaType .parse ("application/json" ))).execute (callback );
379+ }
380+
361381 public Request put (String path ) {
362382 return invoke ("put" , path , EMPTY_BODY );
363383 }
0 commit comments