use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http1xTest method testConnectionCloseHttp_1_1_NoClose.
@Test
public void testConnectionCloseHttp_1_1_NoClose() throws Exception {
testConnectionClose(HttpClientRequest::end, socket -> {
AtomicBoolean firstRequest = new AtomicBoolean(true);
socket.handler(RecordParser.newDelimited("\r\n\r\n", buffer -> {
if (firstRequest.getAndSet(false)) {
socket.write("HTTP/1.1 200 OK\n" + "Content-Type: text/plain\n" + "Content-Length: 4\n" + "\n" + "xxx\n");
} else {
socket.write("HTTP/1.1 200 OK\n" + "Content-Type: text/plain\n" + "Content-Length: 1\n" + "Connection: close\n" + "\n" + "\n");
}
}));
});
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http1xTest method testInvalidHttpResponse.
@Test
public void testInvalidHttpResponse() {
waitFor(2);
AtomicInteger count = new AtomicInteger(0);
CompletableFuture<Void> sendResp = new CompletableFuture<>();
NetServer server = vertx.createNetServer();
String match = "GET /somepath HTTP/1.1\r\nHost: localhost:8080\r\n\r\n";
server.connectHandler(so -> {
StringBuilder content = new StringBuilder();
so.handler(buff -> {
content.append(buff);
while (content.toString().startsWith(match)) {
content.delete(0, match.length());
switch(count.getAndIncrement()) {
case 0:
sendResp.thenAccept(v -> {
});
break;
case 1:
Buffer resp1 = Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n");
Buffer resp2 = Buffer.buffer("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
so.write(Buffer.buffer().appendBuffer(resp1).appendBuffer(resp2));
break;
default:
fail();
break;
}
}
});
}).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1));
AtomicBoolean fail1 = new AtomicBoolean();
HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
fail();
}).exceptionHandler(err -> {
if (fail1.compareAndSet(false, true)) {
assertEquals(IllegalArgumentException.class, err.getClass());
complete();
}
});
AtomicBoolean fail2 = new AtomicBoolean();
HttpClientRequest req2 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
resp.bodyHandler(buff -> {
assertEquals("okusa", buff.toString());
testComplete();
});
}).exceptionHandler(err -> {
if (fail2.compareAndSet(false, true)) {
assertEquals(VertxException.class, err.getClass());
complete();
}
});
req1.end();
req2.end();
}));
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HostnameResolutionTest method testHttp.
@Test
public void testHttp() throws Exception {
HttpClient client = vertx.createHttpClient();
HttpServer server = vertx.createHttpServer().requestHandler(req -> {
req.response().end("foo");
});
try {
CountDownLatch listenLatch = new CountDownLatch(1);
server.listen(8080, "vertx.io", onSuccess(s -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
client.getNow(8080, "vertx.io", "/somepath", resp -> {
Buffer buffer = Buffer.buffer();
resp.handler(buffer::appendBuffer);
resp.endHandler(v -> {
assertEquals(Buffer.buffer("foo"), buffer);
testComplete();
});
});
await();
} finally {
client.close();
server.close();
}
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http2ServerTest method testReceivePing.
@Test
public void testReceivePing() throws Exception {
Buffer expected = TestUtils.randomBuffer(8);
Context ctx = vertx.getOrCreateContext();
server.close();
server.connectionHandler(conn -> {
conn.pingHandler(buff -> {
assertOnIOContext(ctx);
assertEquals(expected, buff);
testComplete();
});
});
server.requestHandler(req -> fail());
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.encoder.writePing(request.context, false, expected.getByteBuf(), request.context.newPromise());
});
fut.sync();
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpRequestStreamTest method testReadStreamPauseResume.
@Test
public void testReadStreamPauseResume() {
String path = "/some/path";
this.server = vertx.createHttpServer(new HttpServerOptions().setAcceptBacklog(10).setPort(HttpTestBase.DEFAULT_HTTP_PORT));
ReadStream<HttpServerRequest> httpStream = server.requestStream();
AtomicBoolean paused = new AtomicBoolean();
httpStream.handler(req -> {
assertFalse(paused.get());
HttpServerResponse response = req.response();
response.setStatusCode(200).end();
response.close();
});
server.listen(listenAR -> {
assertTrue(listenAR.succeeded());
paused.set(true);
httpStream.pause();
netClient = vertx.createNetClient(new NetClientOptions().setConnectTimeout(1000));
netClient.connect(HttpTestBase.DEFAULT_HTTP_PORT, "localhost", socketAR -> {
assertTrue(socketAR.succeeded());
NetSocket socket = socketAR.result();
Buffer buffer = Buffer.buffer();
socket.handler(buffer::appendBuffer);
socket.closeHandler(v -> {
assertEquals(0, buffer.length());
paused.set(false);
httpStream.resume();
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, "localhost", path, resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}).end();
});
});
});
await();
}
Aggregations