Search in sources :

Example 6 with HttpServer

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());
}
Also used : HttpServer(cloud.piranha.http.api.HttpServer) Test(org.junit.jupiter.api.Test)

Example 7 with HttpServer

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();
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) HttpServerRequest(cloud.piranha.http.api.HttpServerRequest) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpServerResponse(cloud.piranha.http.api.HttpServerResponse) HttpClient(java.net.http.HttpClient) HttpServer(cloud.piranha.http.api.HttpServer) Test(org.junit.jupiter.api.Test)

Example 8 with HttpServer

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();
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) HttpServerRequest(cloud.piranha.http.api.HttpServerRequest) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpServerResponse(cloud.piranha.http.api.HttpServerResponse) HttpClient(java.net.http.HttpClient) HttpServer(cloud.piranha.http.api.HttpServer) Test(org.junit.jupiter.api.Test)

Example 9 with HttpServer

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();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServer(cloud.piranha.http.api.HttpServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Socket(java.net.Socket) Test(org.junit.jupiter.api.Test)

Example 10 with HttpServer

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();
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) HttpClient(java.net.http.HttpClient) HttpServer(cloud.piranha.http.api.HttpServer) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

HttpServer (cloud.piranha.http.api.HttpServer)15 IOException (java.io.IOException)12 Test (org.junit.jupiter.api.Test)12 HttpClient (java.net.http.HttpClient)9 HttpRequest (java.net.http.HttpRequest)9 DefaultWebApplication (cloud.piranha.core.impl.DefaultWebApplication)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DefaultHttpServer (cloud.piranha.http.impl.DefaultHttpServer)3 HttpWebApplicationServer (cloud.piranha.http.webapp.HttpWebApplicationServer)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 WebApplicationExtension (cloud.piranha.core.api.WebApplicationExtension)2 DefaultWebApplicationExtensionContext (cloud.piranha.core.impl.DefaultWebApplicationExtensionContext)2 HttpServerRequest (cloud.piranha.http.api.HttpServerRequest)2 HttpServerResponse (cloud.piranha.http.api.HttpServerResponse)2 ServletException (jakarta.servlet.ServletException)2 File (java.io.File)2 URI (java.net.URI)2 AnnotationManager (cloud.piranha.core.api.AnnotationManager)1 Piranha (cloud.piranha.core.api.Piranha)1