/** * @return http server builder */ public HttpServerBuilder getHttpServerBuilder() { if (httpServerBuilder == null) { httpServerBuilder = HttpServerBuilder.httpServerBuilder(); int port = getPort(); if (!Str.isEmpty(port)) { httpServerBuilder.setPort(port); } } return httpServerBuilder; }
public void connect() { port = PortUtils.findOpenPortStartAt(port); server = new HttpServerBuilder().setPort(port).build(); client = new HttpClientBuilder().setPoolSize(1).setPort(port).build(); requestReceived = new AtomicBoolean(); responseReceived = new AtomicBoolean(); messageBody = new AtomicReference<>(); responseCode = new AtomicInteger(); port++; }
public HttpServer getHttpServer() { if (httpServer == null) { httpServer = getHttpServerBuilder() .setPort(getPort()) .setHost(getHost()) .build(); final Supplier<String> webPageContentsSupplier = getWebPageContentsSupplier(); httpServer.setShouldContinueHttpRequest(httpRequest -> { /* If not the page uri we want to then, just continue by returning true. */ if (!httpRequest.getUri().equals(getHtmlPageLocation())) { return true; } final String webPageContents = webPageContentsSupplier.get(); /* Send the HTML file out to the browser. */ httpRequest.getReceiver().response(200, "text/html", webPageContents); return false; }); } return httpServer; }
public HttpServerBuilder getHttpServerBuilder() { if (httpServerBuilder == null) { httpServerBuilder = httpServerBuilder().setPort(getPort()) .setHost(getHost()) .setFlushInterval(this.getFlushInterval()) .setSystemManager(getSystemManager()); setupHealthAndStats(httpServerBuilder); } return httpServerBuilder; }
@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()); }
server = new HttpServerBuilder().setPort(port - 1) .addRequestBodyContinuePredicate(httpRequest -> { if (httpRequest.getContentLength() > 0) {
@Test public void test() throws Exception { ok = objectUnderTest.setHost("host").getHost().equals("host") || die(); ok = objectUnderTest.setHost("localhost").getHost().equals("localhost") || die(); ok = objectUnderTest.setPipeline(true).isPipeline() || die(); ok = !objectUnderTest.setPipeline(false).isPipeline() || die(); ok = objectUnderTest.setPort(9090).getPort() == 9090 || die(); ok = objectUnderTest.setPort(8080).getPort() == 8080 || die(); ok = objectUnderTest.setFlushInterval(909).getFlushInterval() == 909 || die(); ok = objectUnderTest.setFlushInterval(808).getFlushInterval() == 808 || die(); objectUnderTest.build(); } }
@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()); }
@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()); }
HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(9999).build();
HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(9999).build();
@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); }
public HttpServerBuilder getHttpServerBuilder() { if (httpServerBuilder == null) { httpServerBuilder = httpServerBuilder().setPort(getPort()) .setHost(getHost()) .setFlushInterval(this.getFlushInterval()) .setSystemManager(getSystemManager()); setupHealthAndStats(httpServerBuilder); } return httpServerBuilder; }
public HttpServerBuilder getHttpServerBuilder() { if (httpServerBuilder == null) { httpServerBuilder = httpServerBuilder().setPort(getPort()) .setHost(getHost()) .setFlushInterval(this.getFlushInterval()) .setSystemManager(getSystemManager()); setupHealthAndStats(httpServerBuilder); } return httpServerBuilder; }