@Override public ServiceEndpointServer startServerAndWait() { doStart(); if (httpServer instanceof HttpServer) { ((HttpServer) httpServer).startServerAndWait(); } else { httpServer.start(); } return this; }
public void run() { server.startServerAndWait(); client.startClient(); }
@Test public void testBinary() throws Exception { final int port = PortUtils.findOpenPortStartAt(4001); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); final AtomicReference<byte[]> messageRef = new AtomicReference<>(); final CountDownLatch countDownLatch = new CountDownLatch(2); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendBytes("world".getBytes()); countDownLatch.countDown(); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); webSocket.setBinaryMessageConsumer(message -> { messageRef.set(message); countDownLatch.countDown(); }); webSocket.openAndWait(); webSocket.sendBinary("hello".getBytes()); countDownLatch.await(5, TimeUnit.SECONDS); assertEquals("world", new String(messageRef.get(), StandardCharsets.UTF_8)); assertEquals("hello", new String(((byte[]) bodyRef.get()), StandardCharsets.UTF_8)); }
@Test public void testText() throws Exception { final int port = PortUtils.findOpenPortStartAt(4000); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); final AtomicReference<String> messageRef = new AtomicReference<>(); final CountDownLatch countDownLatch = new CountDownLatch(2); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendText("world"); countDownLatch.countDown(); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); webSocket.setTextMessageConsumer(message -> { messageRef.set(message); countDownLatch.countDown(); }); webSocket.openAndWait(); webSocket.sendText("hello"); countDownLatch.await(5, TimeUnit.SECONDS); assertEquals("world", messageRef.get()); assertEquals("hello", bodyRef.get().toString()); }
@Test public void testTextQueueWithBatchSize() throws Exception { final int port = PortUtils.findOpenPortStartAt(4000); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendText("world"); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); final WebSocketTextQueue queue = new WebSocketTextQueue(webSocket, 100, 100, TimeUnit.MILLISECONDS); webSocket.openAndWait(); webSocket.sendText("hello"); String message = queue.receiveQueue().pollWait(); assertEquals("world", message); assertEquals("hello", bodyRef.get().toString()); }
@Test public void testTextQueue() throws Exception { final int port = PortUtils.findOpenPortStartAt(4000); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendText("world"); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); final WebSocketTextQueue queue = new WebSocketTextQueue(webSocket); webSocket.openAndWait(); webSocket.sendText("hello"); String message = queue.receiveQueue().pollWait(); assertEquals("world", message); assertEquals("hello", bodyRef.get().toString()); }
@Override public ServiceEndpointServer startServerAndWait() { doStart(); if (httpServer instanceof HttpServer) { ((HttpServer) httpServer).startServerAndWait(); }else { httpServer.start(); } return this; }
@Override public ServiceEndpointServer startServerAndWait() { doStart(); if (httpServer instanceof HttpServer) { ((HttpServer) httpServer).startServerAndWait(); } else { httpServer.start(); } return this; }
@Test public void test() throws Exception { final int openPortStartAt = PortUtils.findOpenPortStartAt(7777); HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(openPortStartAt).build(); server.setHttpRequestConsumer(serverRequest -> { final MultiMap<String, String> headers = MultiMap.multiMap(); headers.add("foo", "bar").add("foo", "baz"); serverRequest.getReceiver().response(200, "application/json", "true", headers); }); server.startServerAndWait(); HttpClient client = HttpClientBuilder.httpClientBuilder() .setPort(openPortStartAt) .setHost("localhost").build(); client.start(); final HttpTextResponse httpResponse = client.get("/hi"); puts(httpResponse.headers()); boolean foundFoo = httpResponse.headers().keySet().contains("foo"); assertTrue(foundFoo); }