use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method testFollowRedirectWithChunkedBody.
@Test
public void testFollowRedirectWithChunkedBody() throws Exception {
Buffer buff1 = Buffer.buffer(TestUtils.randomAlphaString(2048));
Buffer buff2 = Buffer.buffer(TestUtils.randomAlphaString(2048));
Buffer expected = Buffer.buffer().appendBuffer(buff1).appendBuffer(buff2);
AtomicBoolean redirected = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
server.requestHandler(req -> {
boolean redirect = redirected.compareAndSet(false, true);
if (redirect) {
latch.countDown();
}
req.bodyHandler(body -> {
assertEquals(HttpMethod.PUT, req.method());
assertEquals(body, expected);
if (redirect) {
req.response().setStatusCode(307).putHeader(HttpHeaders.LOCATION, "http://localhost:8080/whatever").end();
} else {
req.response().end();
}
});
});
startServer();
HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
testComplete();
}).setFollowRedirects(true).setChunked(true).write(buff1);
awaitLatch(latch);
req.end(buff2);
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method drainingServer.
private void drainingServer(Consumer<Future<Void>> consumer) {
Future<Void> resumeFuture = Future.future();
server.requestHandler(req -> {
req.response().setChunked(true);
assertFalse(req.response().writeQueueFull());
req.response().setWriteQueueMaxSize(1000);
Buffer buff = TestUtils.randomBuffer(10000);
vertx.setPeriodic(1, id -> {
req.response().write(buff);
if (req.response().writeQueueFull()) {
vertx.cancelTimer(id);
req.response().drainHandler(v -> {
assertFalse(req.response().writeQueueFull());
testComplete();
});
resumeFuture.complete();
}
});
});
server.listen(onSuccess(s -> consumer.accept(resumeFuture)));
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method test100ContinueHandledManually.
@Test
public void test100ContinueHandledManually() throws Exception {
server.close();
server = vertx.createHttpServer(createBaseServerOptions());
Buffer toSend = TestUtils.randomBuffer(1000);
server.requestHandler(req -> {
assertEquals("100-continue", req.getHeader("expect"));
req.response().writeContinue();
req.bodyHandler(data -> {
assertEquals(toSend, data);
req.response().end();
});
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
resp.endHandler(v -> testComplete());
});
req.headers().set("Expect", "100-continue");
req.setChunked(true);
req.continueHandler(v -> {
req.write(toSend);
req.end();
});
req.sendHead();
}));
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http1xTest method testCloseTheConnectionAfterResetPersistentClientResponse.
private void testCloseTheConnectionAfterResetPersistentClientResponse(boolean pipelined) throws Exception {
waitFor(2);
server.close();
NetServer server = vertx.createNetServer();
try {
AtomicInteger count = new AtomicInteger();
AtomicBoolean closed = new AtomicBoolean();
server.connectHandler(so -> {
Buffer total = Buffer.buffer();
switch(count.getAndIncrement()) {
case 0:
so.handler(buff -> {
total.appendBuffer(buff);
if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
so.write(Buffer.buffer("HTTP/1.1 200 OK\r\n" + "Content-Length: 200\r\n" + "\r\n" + "Some-Buffer"));
so.closeHandler(v -> {
closed.set(true);
});
}
});
break;
case 1:
so.handler(buff -> {
total.appendBuffer(buff);
if (total.toString().equals("GET /somepath HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "\r\n")) {
so.write("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 11\r\n" + "\r\n" + "Hello world");
complete();
}
});
break;
default:
fail("Invalid count");
break;
}
});
CountDownLatch listenLatch = new CountDownLatch(1);
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> {
listenLatch.countDown();
}));
awaitLatch(listenLatch);
client.close();
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(pipelined).setKeepAlive(true));
HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
if (pipelined) {
req1.handler(resp1 -> {
resp1.handler(buff -> {
req1.reset();
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals("Hello world", body.toString());
complete();
});
});
});
});
req1.end();
} else {
req1.handler(resp -> {
resp.handler(buff -> {
req1.reset();
});
});
req1.end();
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals("Hello world", body.toString());
complete();
});
});
}
await();
} finally {
server.close();
}
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http1xTest method testHttpConnect.
@Test
public void testHttpConnect() {
Buffer buffer = TestUtils.randomBuffer(128);
Buffer received = Buffer.buffer();
vertx.createNetServer(new NetServerOptions().setPort(1235)).connectHandler(socket -> {
socket.handler(socket::write);
}).listen(onSuccess(netServer -> {
server.requestHandler(req -> {
vertx.createNetClient(new NetClientOptions()).connect(netServer.actualPort(), "localhost", onSuccess(socket -> {
req.response().setStatusCode(200);
req.response().setStatusMessage("Connection established");
req.response().end();
Pump.pump(req.netSocket(), socket).start();
Pump.pump(socket, req.netSocket()).start();
req.netSocket().closeHandler(v -> socket.close());
}));
});
server.listen(onSuccess(s -> {
client.request(HttpMethod.CONNECT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
assertEquals(200, resp.statusCode());
NetSocket socket = resp.netSocket();
socket.handler(buff -> {
received.appendBuffer(buff);
if (received.length() == buffer.length()) {
netServer.close();
assertEquals(buffer, received);
testComplete();
}
});
socket.write(buffer);
}).end();
}));
}));
await();
}
Aggregations