Java Code Examples for io.vertx.test.core.TestUtils#randomBuffer()

The following examples show how to use io.vertx.test.core.TestUtils#randomBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactiveWriteStreamTest.java    From vertx-reactive-streams with Apache License 2.0 6 votes vote down vote up
private void testWriteQueueFullAndDrain(ReactiveWriteStream<Buffer> rws, int writeQueueMaxSize) throws Exception {
  rws.setWriteQueueMaxSize(writeQueueMaxSize);
  MySubscriber subscriber = new MySubscriber();
  rws.subscribe(subscriber);
  for (int i = 0; i < writeQueueMaxSize - 1; i++) {
    rws.write(TestUtils.randomBuffer(50));
  }
  assertFalse(rws.writeQueueFull());
  Buffer buff2 = TestUtils.randomBuffer(100);
  rws.write(buff2);
  assertTrue(rws.writeQueueFull());
  rws.drainHandler(v -> {
    assertFalse(rws.writeQueueFull());
    testComplete();
  });
  assertWaitUntil(() -> subscriber.subscription != null);
  subscriber.subscription.request(2);
  await();
}
 
Example 2
Source File: WebClientTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamHttpServerRequest() throws Exception {
  Buffer expected = TestUtils.randomBuffer(10000);
  HttpServer server2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> req.bodyHandler(body -> {
    assertEquals(body, expected);
    req.response().end();
  }));
  startServer(server2);
  WebClient webClient = WebClient.create(vertx);
  try {
    server.requestHandler(req -> webClient.postAbs("http://localhost:8081/")
      .sendStream(req, onSuccess(resp -> req.response().end("ok"))));
    startServer();
    webClient.post(8080, "localhost", "/").sendBuffer(expected, onSuccess(resp -> {
      assertEquals("ok", resp.bodyAsString());
      complete();
    }));
    await();
  } finally {
    server2.close();
  }
}
 
Example 3
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private void testFileUpload(String uploadsDir, int size) throws Exception {
  String name = "somename";
  String fileName = "somefile.dat";
  String contentType = "application/octet-stream";
  Buffer fileData = TestUtils.randomBuffer(size);
  router.route().handler(rc -> {
    Set<FileUpload> fileUploads = rc.fileUploads();
    assertNotNull(fileUploads);
    assertEquals(1, fileUploads.size());
    FileUpload upload = fileUploads.iterator().next();
    assertEquals(name, upload.name());
    assertEquals(fileName, upload.fileName());
    assertEquals(contentType, upload.contentType());
    assertEquals("binary", upload.contentTransferEncoding());
    assertEquals(fileData.length(), upload.size());
    String uploadedFileName = upload.uploadedFileName();
    assertTrue(uploadedFileName.startsWith(uploadsDir + File.separator));
    Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName);
    assertEquals(fileData, uploaded);
    // the data is upload as HTML form, so the body should be empty
    Buffer rawBody = rc.getBody();
    assertEquals(0, rawBody.length());
    rc.response().end();
  });
  sendFileUploadRequest(fileData, 200, "OK");
}
 
Example 4
Source File: WebClientTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testResponseBodyAsBuffer() throws Exception {
  Buffer expected = TestUtils.randomBuffer(2000);
  server.requestHandler(req -> req.response().end(expected));
  startServer();
  HttpRequest<Buffer> get = webClient.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
  get.send(onSuccess(resp -> {
    assertEquals(200, resp.statusCode());
    assertEquals(expected, resp.body());
    testComplete();
  }));
  await();
}
 
Example 5
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testBodyBuffer() throws Exception {
  Buffer buff = TestUtils.randomBuffer(1000);
  router.route().handler(rc -> {
    assertEquals(buff, rc.getBody());
    rc.response().end();
  });
  testRequest(HttpMethod.POST, "/", req -> {
    req.setChunked(true);
    req.write(buff);
  }, 200, "OK", null);
}
 
Example 6
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testBodyTooBig() throws Exception {
  router.clear();
  router.route().handler(BodyHandler.create().setBodyLimit(5000));
  Buffer buff = TestUtils.randomBuffer(10000);
  router.route().handler(rc -> fail("Should not be called"));
  testRequest(HttpMethod.POST, "/", req -> {
    req.setChunked(true);
    req.write(buff);
  }, 413, "Request Entity Too Large", null);
}
 
Example 7
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testBodyTooBig2() throws Exception {
  router.clear();
  router.route().handler(BodyHandler.create().setBodyLimit(500));
  Buffer buff = TestUtils.randomBuffer(1000);
  router.route().handler(rc -> fail("Should not be called"));
  testRequest(HttpMethod.POST, "/", req -> {
    req.setChunked(true);
    req.write(buff);
  }, 413, "Request Entity Too Large", null);
}
 
Example 8
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileUploadTooBig() throws Exception {
  router.clear();
  router.route().handler(BodyHandler.create().setBodyLimit(20000));

  Buffer fileData = TestUtils.randomBuffer(50000);
  router.route().handler(rc -> fail("Should not be called"));
  sendFileUploadRequest(fileData, 413, "Request Entity Too Large");
}
 
Example 9
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileUploadTooBig2() throws Exception {
  router.clear();
  router.route().handler(BodyHandler.create().setBodyLimit(20000));

  Buffer fileData = TestUtils.randomBuffer(50000);
  router.route().handler(rc -> fail("Should not be called"));
  sendFileUploadRequest(fileData, 413, "Request Entity Too Large");
}
 
Example 10
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUploadDirMultiPartFormData() throws Exception
{
  String dirName = getNotCreatedTemporaryFolderName();
  router.clear();
  router.route().handler(BodyHandler.create(false).setUploadsDirectory(dirName));

  Buffer fileData = TestUtils.randomBuffer(50);
  router.route().handler(rc -> {
    rc.response().end();
    assertFalse("Upload directory must not be created.", vertx.fileSystem().existsBlocking(dirName));
  });
  sendFileUploadRequest(fileData, 200, "OK");
}
 
Example 11
Source File: BodyHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testBodyLimitWithHandleFileUploadsFalse() throws Exception
{
  router.clear();

  BodyHandler bodyHandler = BodyHandler.create(false).setBodyLimit(2048);
  router.route().handler(bodyHandler);

  Buffer fileData = TestUtils.randomBuffer(4096);
  router.route().handler(rc -> {
    rc.response().end();
  });
  sendFileUploadRequest(fileData, 413, "Request Entity Too Large");
}
 
Example 12
Source File: WebClientTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendBufferBody() throws Exception {
  Buffer body = TestUtils.randomBuffer(2048);
  testSendBody(body, (contentType, buff) -> assertEquals(body, buff));
}