use of java.util.concurrent.atomic.AtomicInteger 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 java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class HttpTest method testConnectionErrorsGetReportedToRequest.
@Test
public void testConnectionErrorsGetReportedToRequest() throws InterruptedException {
AtomicInteger req1Exceptions = new AtomicInteger();
AtomicInteger req2Exceptions = new AtomicInteger();
AtomicInteger req3Exceptions = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(3);
// This one should cause an error in the Client Exception handler, because it has no exception handler set specifically.
HttpClientRequest req1 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl1", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req1.exceptionHandler(t -> {
assertEquals("More than one call to req1 exception handler was not expected", 1, req1Exceptions.incrementAndGet());
latch.countDown();
});
HttpClientRequest req2 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req2.exceptionHandler(t -> {
assertEquals("More than one call to req2 exception handler was not expected", 1, req2Exceptions.incrementAndGet());
latch.countDown();
});
HttpClientRequest req3 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req3.exceptionHandler(t -> {
assertEquals("More than one call to req2 exception handler was not expected", 1, req3Exceptions.incrementAndGet());
latch.countDown();
});
req1.end();
req2.end();
req3.end();
awaitLatch(latch);
testComplete();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class HttpTest method testClearHandlersOnEnd.
@Test
public void testClearHandlersOnEnd() {
String path = "/some/path";
server = vertx.createHttpServer(createBaseServerOptions());
server.requestHandler(req -> req.response().setStatusCode(200).end());
server.listen(ar -> {
assertTrue(ar.succeeded());
HttpClientRequest req = client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path);
AtomicInteger count = new AtomicInteger();
req.handler(resp -> {
resp.endHandler(v -> {
try {
resp.endHandler(null);
resp.exceptionHandler(null);
resp.handler(null);
} catch (Exception e) {
fail("Was expecting to set to null the handlers when the response is completed");
return;
}
if (count.incrementAndGet() == 2) {
testComplete();
}
});
});
req.endHandler(done -> {
try {
req.handler(null);
req.exceptionHandler(null);
req.endHandler(null);
} catch (Exception e) {
e.printStackTrace();
fail("Was expecting to set to null the handlers when the response is completed");
return;
}
if (count.incrementAndGet() == 2) {
testComplete();
}
});
req.end();
});
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class HttpTest method testResponseEndHandlers1.
@Test
public void testResponseEndHandlers1() {
waitFor(2);
AtomicInteger cnt = new AtomicInteger();
server.requestHandler(req -> {
req.response().headersEndHandler(v -> {
req.response().putHeader("extraheader", "wibble");
assertEquals(0, cnt.getAndIncrement());
});
req.response().bodyEndHandler(v -> {
assertEquals(0, req.response().bytesWritten());
assertEquals(1, cnt.getAndIncrement());
complete();
});
req.response().end();
}).listen(onSuccess(server -> {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
assertEquals(200, res.statusCode());
assertEquals("wibble", res.headers().get("extraheader"));
complete();
}).end();
}));
await();
}
use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.
the class HttpTest method testFollowRedirectHost.
@Test
public void testFollowRedirectHost() throws Exception {
String scheme = createBaseClientOptions().isSsl() ? "https" : "http";
waitFor(2);
AtomicInteger redirections = new AtomicInteger();
server.requestHandler(req -> {
switch(redirections.getAndIncrement()) {
case 0:
req.response().setStatusCode(301).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end();
break;
case 1:
assertEquals(scheme + "://the_host/whatever", req.absoluteURI());
req.response().end();
complete();
break;
}
});
startServer();
client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(scheme + "://the_host/whatever", resp.request().absoluteURI());
complete();
}).setFollowRedirects(true).setHost("the_host").end();
await();
}
Aggregations