use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testQuotedBoundary.
@Test
public void testQuotedBoundary() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
post.setHeader(Headers.CONTENT_TYPE_STRING, "multipart/form-data; boundary=\"s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\"");
StringEntity entity = new StringEntity("--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"formValue\"\r\n" + "\r\n" + "myValue\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + "file contents\r\n" + "\r\n" + "--s58IGsuzbg6GBG1yIgUO8;n4WkVf7clWMje--\r\n");
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class PathTestCase method testBasicPathHanding.
@Test
public void testBasicPathHanding() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
final PathHandler handler = new PathHandler();
handler.addPrefixPath("a", new RemainingPathHandler("/a"));
handler.addPrefixPath("/aa", new RemainingPathHandler("/aa"));
handler.addExactPath("/aa", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("Exact /aa match:" + exchange.getRelativePath() + ":" + exchange.getResolvedPath());
}
});
handler.addPrefixPath("/aa/anotherSubPath", new RemainingPathHandler("/aa/anotherSubPath"));
final PathHandler sub = new PathHandler();
handler.addPrefixPath("/path", sub);
sub.addPrefixPath("/subpath", new RemainingPathHandler("/subpath"));
sub.addPrefixPath("/", new RemainingPathHandler("/path"));
DefaultServer.setRootHandler(handler);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/notamatchingpath");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
result = client.execute(get);
Assert.assertEquals(StatusCodes.NOT_FOUND, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
runPathTest(client, "/path", "/path", "");
runPathTest(client, "/path/a", "/path", "/a");
runPathTest(client, "/path/subpath", "/subpath", "");
runPathTest(client, "/path/subpath/", "/subpath", "/");
runPathTest(client, "/path/subpath/foo", "/subpath", "/foo");
runPathTest(client, "/a", "/a", "");
runPathTest(client, "/aa/anotherSubPath", "/aa/anotherSubPath", "");
runPathTest(client, "/aa/anotherSubPath/bob", "/aa/anotherSubPath", "/bob");
runPathTest(client, "/aa/b?a=b", "/aa", "/b", Collections.singletonMap("a", "b"));
runPathTest(client, "/path/:bar/baz", "/path", "/:bar/baz");
//now test the exact path match
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/aa");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Exact /aa match::/aa", HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class AbstractLoadBalancingProxyTestCase method testConnectionTimeout.
@Test
public void testConnectionTimeout() throws Exception {
TestHttpClient client = new TestHttpClient();
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/timeout");
get.addHeader("Connection", "close");
HttpResponse result = client.execute(get);
boolean res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
Assert.assertEquals(false, res);
try {
for (int i = 0; i < 20; ++i) {
//try and make sure that all IO threads have been used, this is not reliable however
result = client.execute(get);
HttpClientUtils.readResponse(result);
}
result = client.execute(get);
res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
//Assert.assertEquals(true, res); //this will fail sometime, unless we make a huge number of requests to make sure all IO threads are utilised
Thread.sleep(IDLE_TIMEOUT + 1000);
UndertowLogger.ROOT_LOGGER.info("Sending timed out request");
result = client.execute(get);
res = Boolean.parseBoolean(HttpClientUtils.readResponse(result));
Assert.assertEquals(false, res);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class AbstractLoadBalancingProxyTestCase method testLoadSharedWithServerShutdown.
@Test
public void testLoadSharedWithServerShutdown() throws Exception {
final StringBuilder resultString = new StringBuilder();
for (int i = 0; i < 6; ++i) {
TestHttpClient client = new TestHttpClient();
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
HttpResponse result = client.execute(get);
Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
resultString.append(HttpClientUtils.readResponse(result));
resultString.append(' ');
server1.stop();
Thread.sleep(600);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
result = client.execute(get);
Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
resultString.append(HttpClientUtils.readResponse(result));
resultString.append(' ');
server1.start();
server2.stop();
Thread.sleep(600);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/name");
result = client.execute(get);
Assert.assertEquals("Test failed with i=" + i, StatusCodes.OK, result.getStatusLine().getStatusCode());
resultString.append(HttpClientUtils.readResponse(result));
resultString.append(' ');
server2.start();
}
Assert.assertTrue(resultString.toString().contains("server1"));
Assert.assertTrue(resultString.toString().contains("server2"));
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class LoadBalancerConnectionPoolingTestCase method shouldReduceConnectionPool.
@Test
public void shouldReduceConnectionPool() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(10);
PoolingClientConnectionManager conman = new PoolingClientConnectionManager();
conman.setDefaultMaxPerRoute(20);
final TestHttpClient client = new TestHttpClient(conman);
int requests = 20;
final CountDownLatch latch = new CountDownLatch(requests);
long ttlStartExpire = TTL + System.currentTimeMillis();
try {
for (int i = 0; i < requests; ++i) {
executorService.submit(new Runnable() {
@Override
public void run() {
HttpGet get = new HttpGet("http://" + host + ":" + (port + 1));
try {
HttpResponse response = client.execute(get);
Assert.assertEquals(StatusCodes.OK, response.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(response);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
latch.countDown();
}
}
});
}
if (!latch.await(2000, TimeUnit.MILLISECONDS)) {
Assert.fail();
}
} finally {
client.getConnectionManager().shutdown();
executorService.shutdown();
}
if (activeConnections.size() != 1) {
//uncommon, but we guard against it to prevent intermittent failures
if (System.currentTimeMillis() < ttlStartExpire) {
Assert.fail("there should still be a connection");
}
}
long end = System.currentTimeMillis() + (TTL * 3);
while (!activeConnections.isEmpty() && System.currentTimeMillis() < end) {
Thread.sleep(100);
}
Assert.assertEquals(0, activeConnections.size());
}
Aggregations