use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.
the class HttpServerTest method testStartAndStop.
/**
* Test start and stop method.
*
* @throws Exception when an error occurs.
*/
@Test
void testStartAndStop() throws Exception {
int port = findPort();
HttpServer server = createServer(port);
server.start();
assertTrue(server.isRunning());
server.stop();
assertFalse(server.isRunning());
}
use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.
the class HttpServerTest method testAddHeader.
/**
* Test addHeader method.
*/
@Test
void testAddHeader() {
int port = findPort();
HttpServer server = createServer(port, (HttpServerRequest request, HttpServerResponse response) -> {
try {
response.setStatus(200);
response.setHeader(CONTENT_TYPE, TEXT_PLAIN);
response.setHeader(KEEP_ALIVE, CLOSE);
response.writeStatusLine();
response.writeHeaders();
OutputStream outputStream = response.getOutputStream();
Iterator<String> headerNames = request.getHeaderNames();
while (headerNames.hasNext()) {
String name = headerNames.next();
outputStream.write(name.toLowerCase().getBytes());
outputStream.write("\n".getBytes());
outputStream.write(request.getHeader(name).getBytes());
outputStream.write("\n".getBytes());
}
outputStream.flush();
} catch (IOException ioe) {
// nothing to do here.
}
return COMPLETED;
});
server.start();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(HTTP_LOCALHOST_PREFIX + port)).header("name", "value1").build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
assertEquals(200, response.statusCode());
String body = response.body();
assertTrue(body.contains("name"));
assertTrue(body.contains("value1"));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
} finally {
server.stop();
}
}
use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.
the class HttpServerTest method testGetLocalAddress.
/**
* Test getLocalAddress method.
*/
@Test
void testGetLocalAddress() {
int port = findPort();
HttpServer server = createServer(port, (HttpServerRequest request, HttpServerResponse response) -> {
try {
response.setStatus(200);
response.setHeader(CONTENT_TYPE, TEXT_PLAIN);
response.setHeader(KEEP_ALIVE, CLOSE);
response.writeStatusLine();
response.writeHeaders();
String value = request.getLocalAddress();
OutputStream outputStream = response.getOutputStream();
outputStream.write(value.getBytes());
outputStream.flush();
} catch (IOException ioe) {
// nothing to do here.
}
return COMPLETED;
});
server.start();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(HTTP_LOCALHOST_PREFIX + port)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
assertEquals(200, response.statusCode());
String body = response.body();
assertTrue(body.contains("127.0.0.1"));
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
} finally {
server.stop();
}
}
use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.
the class HttpServerTest method testRequestHTTP11.
/**
* Test if the server supports HTTP/1.1.
*
* @throws Exception when an error occurs.
*/
@Test
void testRequestHTTP11() throws Exception {
int port = findPort();
HttpServer server = createServer(port, HttpServerTest::returnProtocol);
server.start();
try (Socket socket = new Socket("localhost", port);
OutputStream outputStream = socket.getOutputStream()) {
outputStream.write(("GET / HTTP/1.1\r\nHost: localhost:" + port + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));
outputStream.flush();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream response = new ByteArrayOutputStream();
inputStream.transferTo(response);
assertTrue(response.toString(StandardCharsets.UTF_8).contains("HTTP/1.1"));
}
server.stop();
}
use of cloud.piranha.http.api.HttpServer in project piranha by piranhacloud.
the class HttpServerTest method testFile.
/**
* Test file.
*
* @throws Exception when a serious error occurs.
*/
@Test
void testFile() throws Exception {
int port = findPort();
HttpServer server = createServer(port);
server.start();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(HTTP_LOCALHOST_PREFIX + port + "/pom.xml")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
assertEquals(200, response.statusCode());
String responseText = response.body();
assertTrue(responseText.contains("modelVersion"));
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} finally {
server.stop();
}
}
Aggregations