use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testRequestTimesoutWhenIndicatedPeriodExpiresWithoutAResponseFromRemoteServer.
@Test
public void testRequestTimesoutWhenIndicatedPeriodExpiresWithoutAResponseFromRemoteServer() {
// No response handler so timeout triggers
server.requestHandler(noOpHandler());
AtomicBoolean failed = new AtomicBoolean();
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
fail("End should not be called because the request should timeout");
});
req.exceptionHandler(t -> {
if (failed.compareAndSet(false, true)) {
assertTrue("Expected to end with timeout exception but ended with other exception: " + t, t instanceof TimeoutException);
testComplete();
}
});
req.setTimeout(1000);
req.end();
}));
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testSetHandlersOnEnd.
@Test
public void testSetHandlersOnEnd() {
String path = "/some/path";
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);
req.handler(resp -> {
});
req.endHandler(done -> {
try {
req.handler(arg -> {
});
fail();
} catch (Exception ignore) {
}
try {
req.exceptionHandler(arg -> {
});
fail();
} catch (Exception ignore) {
}
try {
req.endHandler(arg -> {
});
fail();
} catch (Exception ignore) {
}
testComplete();
});
req.end();
});
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testFormUploadAttributes.
@Test
public void testFormUploadAttributes() throws Exception {
AtomicInteger attributeCount = new AtomicInteger();
server.requestHandler(req -> {
if (req.method() == HttpMethod.POST) {
assertEquals(req.path(), "/form");
req.response().setChunked(true);
req.setExpectMultipart(true);
req.uploadHandler(upload -> upload.handler(buffer -> {
fail("Should get here");
}));
req.endHandler(v -> {
MultiMap attrs = req.formAttributes();
attributeCount.set(attrs.size());
assertEquals("vert x", attrs.get("framework"));
assertEquals("vert x", req.getFormAttribute("framework"));
assertEquals("jvm", attrs.get("runson"));
assertEquals("jvm", req.getFormAttribute("runson"));
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(2, attributeCount.get());
testComplete();
});
try {
Buffer buffer = Buffer.buffer();
buffer.appendString("framework=" + URLEncoder.encode("vert x", "UTF-8") + "&runson=jvm", "UTF-8");
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "application/x-www-form-urlencoded");
req.write(buffer).end();
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}));
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testRequestWrite.
@Test
public void testRequestWrite() {
Buffer body = TestUtils.randomBuffer(1000);
server.requestHandler(req -> {
req.bodyHandler(buff -> {
assertEquals(body, buff);
testComplete();
});
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler());
req.setChunked(true);
req.write(body);
req.end();
}));
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testRequestBodyWriteString.
private void testRequestBodyWriteString(boolean chunked, String encoding) {
String body = TestUtils.randomUnicodeString(1000);
Buffer bodyBuff;
if (encoding == null) {
bodyBuff = Buffer.buffer(body);
} else {
bodyBuff = Buffer.buffer(body, encoding);
}
server.requestHandler(req -> {
req.bodyHandler(buff -> {
assertEquals(bodyBuff, buff);
testComplete();
});
});
server.listen(onSuccess(server -> {
HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler());
if (chunked) {
req.setChunked(true);
} else {
req.headers().set("Content-Length", String.valueOf(bodyBuff.length()));
}
if (encoding == null) {
req.write(body);
} else {
req.write(body, encoding);
}
req.end();
}));
await();
}
Aggregations