use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class SenderTestCase method testAsyncSender.
@Test
public void testAsyncSender() throws IOException {
StringBuilder sb = new StringBuilder(SENDS);
for (int i = 0; i < SENDS; ++i) {
sb.append("a");
}
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/lots?blocking=false");
TestHttpClient client = new TestHttpClient();
try {
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(sb.toString(), HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class SetAttributeTestCase method testRewrite.
@Test
public void testRewrite() throws IOException {
DefaultServer.setRootHandler(rewrite("regex['/somePath/(.*)']", "/otherPath/$1", getClass().getClassLoader(), path().addPrefixPath("/otherPath", new InfoHandler()).addPrefixPath("/relative", rewrite("path-template['/foo/{bar}/{woz}']", "/foo?bar=${bar}&woz=${woz}", getClass().getClassLoader(), new InfoHandler()))));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/relative/foo/a/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("URI: /relative/foo?bar=a&woz=b relative: /foo QS:?bar=a&woz=b bar: a woz: b", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/somePath/foo/a/b");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("URI: /otherPath/foo/a/b relative: /foo/a/b QS:", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/somePath/foo?a=b");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("URI: /otherPath/foo relative: /foo QS:a=b a: b", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class SimpleNonBlockingServerTestCase method sendHttpOneZeroRequest.
@Test
public void sendHttpOneZeroRequest() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders("MyHeader");
Assert.assertEquals("MyValue", header[0].getValue());
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class AccessLogFileTestCase method testLogLotsOfThreads.
@Test
public void testLogLotsOfThreads() throws IOException, InterruptedException, ExecutionException {
Path directory = logDirectory;
Path logFileName = directory.resolve("server2.log");
DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), directory, "server2.");
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(NUM_REQUESTS * NUM_THREADS, new AccessLogHandler(HELLO_HANDLER, logReceiver, "REQ %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
try {
final List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < NUM_THREADS; ++i) {
final int threadNo = i;
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
TestHttpClient client = new TestHttpClient();
try {
for (int i = 0; i < NUM_REQUESTS; ++i) {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "thread-" + threadNo + "-request-" + i);
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
final String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("Hello", response);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
client.getConnectionManager().shutdown();
}
}
}));
}
for (Future<?> future : futures) {
future.get();
}
} finally {
executor.shutdown();
}
latchHandler.await();
logReceiver.awaitWrittenForTest();
String completeLog = new String(Files.readAllBytes(logFileName));
for (int i = 0; i < NUM_THREADS; ++i) {
for (int j = 0; j < NUM_REQUESTS; ++j) {
Assert.assertTrue(completeLog.contains("REQ thread-" + i + "-request-" + j));
}
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class AccessLogFileTestCase method testForcedLogRotation.
@Test
public void testForcedLogRotation() throws IOException, InterruptedException {
Path logFileName = logDirectory.resolve("server.log");
DefaultAccessLogReceiver logReceiver = new DefaultAccessLogReceiver(DefaultServer.getWorker(), logDirectory, "server.");
CompletionLatchHandler latchHandler;
DefaultServer.setRootHandler(latchHandler = new CompletionLatchHandler(new AccessLogHandler(HELLO_HANDLER, logReceiver, "Remote address %a Code %s test-header %{i,test-header}", AccessLogFileTestCase.class.getClassLoader())));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "v1");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
logReceiver.awaitWrittenForTest();
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1\n", new String(Files.readAllBytes(logFileName)));
logReceiver.rotate();
logReceiver.awaitWrittenForTest();
Assert.assertFalse(Files.exists(logFileName));
Path firstLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".log");
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v1\n", new String(Files.readAllBytes(firstLogRotate)));
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.addHeader("test-header", "v2");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("Hello", HttpClientUtils.readResponse(result));
latchHandler.await();
latchHandler.reset();
logReceiver.awaitWrittenForTest();
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2\n", new String(Files.readAllBytes(logFileName)));
logReceiver.rotate();
logReceiver.awaitWrittenForTest();
Assert.assertFalse(Files.exists(logFileName));
Path secondLogRotate = logDirectory.resolve("server." + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "-1.log");
Assert.assertEquals("Remote address " + DefaultServer.getDefaultServerAddress().getAddress().getHostAddress() + " Code 200 test-header v2\n", new String(Files.readAllBytes(secondLogRotate)));
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations