use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method testResponseDataTimeout.
@Test
public void testResponseDataTimeout() {
Buffer expected = TestUtils.randomBuffer(1000);
server.requestHandler(req -> {
req.response().setChunked(true).write(expected);
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI);
Buffer received = Buffer.buffer();
req.handler(resp -> {
req.setTimeout(500);
resp.handler(received::appendBuffer);
});
AtomicInteger count = new AtomicInteger();
req.exceptionHandler(t -> {
if (count.getAndIncrement() == 0) {
assertTrue(t instanceof TimeoutException);
assertEquals(expected, received);
testComplete();
}
});
req.sendHead();
}));
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method testDeliverPausedBufferWhenResume.
@Test
public void testDeliverPausedBufferWhenResume() throws Exception {
Buffer data = TestUtils.randomBuffer(20);
int num = 10;
waitFor(num);
List<CompletableFuture<Void>> resumes = Collections.synchronizedList(new ArrayList<>());
for (int i = 0; i < num; i++) {
resumes.add(new CompletableFuture<>());
}
server.requestHandler(req -> {
int idx = Integer.parseInt(req.path().substring(1));
HttpServerResponse resp = req.response();
resumes.get(idx).thenAccept(v -> {
resp.end();
});
resp.setChunked(true).write(data);
});
startServer();
client.close();
client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(1).setKeepAlive(true));
for (int i = 0; i < num; i++) {
int idx = i;
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/" + i, resp -> {
Buffer body = Buffer.buffer();
resp.handler(buff -> {
resumes.get(idx).complete(null);
body.appendBuffer(buff);
});
resp.endHandler(v -> {
assertEquals(data, body);
complete();
});
resp.pause();
vertx.setTimer(10, id -> {
resp.resume();
});
}).end();
}
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class HttpTest method testFormUploadFile.
private void testFormUploadFile(String contentStr, boolean streamToDisk) throws Exception {
Buffer content = Buffer.buffer(contentStr);
AtomicInteger attributeCount = new AtomicInteger();
server.requestHandler(req -> {
if (req.method() == HttpMethod.POST) {
assertEquals(req.path(), "/form");
req.response().setChunked(true);
req.setExpectMultipart(true);
assertTrue(req.isExpectMultipart());
req.setExpectMultipart(true);
assertTrue(req.isExpectMultipart());
req.uploadHandler(upload -> {
Buffer tot = Buffer.buffer();
assertEquals("file", upload.name());
assertEquals("tmp-0.txt", upload.filename());
assertEquals("image/gif", upload.contentType());
String uploadedFileName;
if (!streamToDisk) {
upload.handler(buffer -> tot.appendBuffer(buffer));
uploadedFileName = null;
} else {
uploadedFileName = new File(testDir, UUID.randomUUID().toString()).getPath();
upload.streamToFileSystem(uploadedFileName);
}
upload.endHandler(v -> {
if (streamToDisk) {
Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
assertEquals(content, uploaded);
} else {
assertEquals(content, tot);
}
assertTrue(upload.isSizeAvailable());
assertEquals(content.length(), upload.size());
});
});
req.endHandler(v -> {
MultiMap attrs = req.formAttributes();
attributeCount.set(attrs.size());
req.response().end();
});
}
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/form", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals(0, body.length());
});
assertEquals(0, attributeCount.get());
testComplete();
});
String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
Buffer buffer = Buffer.buffer();
String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" + "Content-Type: image/gif\r\n" + "\r\n" + contentStr + "\r\n" + "--" + boundary + "--\r\n";
buffer.appendString(body);
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "multipart/form-data; boundary=" + boundary);
req.write(buffer).end();
}));
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http2ServerTest method testNetSocketSendFile.
private void testNetSocketSendFile(Buffer expected, String path, long offset, long length) throws Exception {
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
socket.sendFile(path, offset, length, ar -> {
assertTrue(ar.succeeded());
socket.end();
});
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals("200", headers.status().toString());
assertFalse(endStream);
});
}
Buffer received = Buffer.buffer();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.appendBuffer(Buffer.buffer(data.copy()));
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(received, expected);
testComplete();
});
}
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http2ServerTest method testStreamPauseResume.
private void testStreamPauseResume(Function<HttpServerRequest, ReadStream<Buffer>> streamProvider) throws Exception {
Buffer expected = Buffer.buffer();
String chunk = TestUtils.randomAlphaString(1000);
AtomicBoolean done = new AtomicBoolean();
AtomicBoolean paused = new AtomicBoolean();
Buffer received = Buffer.buffer();
server.requestHandler(req -> {
ReadStream<Buffer> stream = streamProvider.apply(req);
vertx.setPeriodic(1, timerID -> {
if (paused.get()) {
vertx.cancelTimer(timerID);
done.set(true);
vertx.setTimer(100, id -> {
stream.resume();
});
}
});
stream.handler(received::appendBuffer);
stream.endHandler(v -> {
assertEquals(expected, received);
testComplete();
});
stream.pause();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, POST("/form").set("content-type", "text/plain"), 0, false, request.context.newPromise());
request.context.flush();
Http2Stream stream = request.connection.stream(id);
class Anonymous {
void send() {
boolean writable = request.encoder.flowController().isWritable(stream);
if (writable) {
Buffer buf = Buffer.buffer(chunk);
expected.appendBuffer(buf);
request.encoder.writeData(request.context, id, buf.getByteBuf(), 0, false, request.context.newPromise());
request.context.flush();
request.context.executor().execute(this::send);
} else {
request.encoder.writeData(request.context, id, Unpooled.EMPTY_BUFFER, 0, true, request.context.newPromise());
request.context.flush();
paused.set(true);
}
}
}
new Anonymous().send();
});
fut.sync();
await();
}
Aggregations