aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/Http2Test.java
blob: 34b16dca20fd783114c4eb4722512f51972341e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import com.yahoo.container.logging.ConnectionLog;
import com.yahoo.container.logging.ConnectionLogEntry;
import com.yahoo.jdisc.http.ConnectorConfig;
import com.yahoo.jdisc.http.ServerConfig;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

import static com.yahoo.jdisc.Response.Status.OK;
import static com.yahoo.jdisc.http.server.jetty.Utils.createHttp2Client;
import static com.yahoo.jdisc.http.server.jetty.Utils.createSslTestDriver;
import static com.yahoo.jdisc.http.server.jetty.Utils.generatePrivateKeyAndCertificate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bjorncs
 */
class Http2Test {
    @Test
    void requireThatServerCanRespondToHttp2Request(@TempDir Path tmpFolder) throws Exception {
        Path privateKeyFile = tmpFolder.resolve("private-key.pem");
        Path certificateFile = tmpFolder.resolve("certificate.pem");
        generatePrivateKeyAndCertificate(privateKeyFile, certificateFile);

        MetricConsumerMock metricConsumer = new MetricConsumerMock();
        InMemoryConnectionLog connectionLog = new InMemoryConnectionLog();
        JettyTestDriver driver = createSslTestDriver(certificateFile, privateKeyFile, metricConsumer, connectionLog);
        try (CloseableHttpAsyncClient client = createHttp2Client(driver)) {
            String uri = "https://localhost:" + driver.server().getListenPort() + "/status.html";
            SimpleHttpResponse response = client.execute(SimpleRequestBuilder.get(uri).build(), null).get();
            assertNull(response.getBodyText());
            assertEquals(OK, response.getCode());
        }
        assertTrue(driver.close());
        ConnectionLogEntry entry = connectionLog.logEntries().get(0);
        assertEquals("HTTP/2.0", entry.httpProtocol().get());
    }

    @Test
    void requireThatServerCanRespondToHttp2PlainTextRequest() throws Exception {
        InMemoryConnectionLog connectionLog = new InMemoryConnectionLog();
        JettyTestDriver driver = JettyTestDriver.newConfiguredInstance(
                new EchoRequestHandler(),
                new ServerConfig.Builder().connectionLog(new ServerConfig.ConnectionLog.Builder().enabled(true)),
                new ConnectorConfig.Builder(),
                binder -> binder.bind(ConnectionLog.class).toInstance(connectionLog));
        try (CloseableHttpAsyncClient client = createHttp2Client(driver)) {
            String uri = "http://localhost:" + driver.server().getListenPort() + "/status.html";
            SimpleHttpResponse response = client.execute(SimpleRequestBuilder.get(uri).build(), null).get();
            assertNull(response.getBodyText());
            assertEquals(OK, response.getCode());
        }
        assertTrue(driver.close());
        ConnectionLogEntry entry = connectionLog.logEntries().get(0);
        assertEquals("HTTP/2.0", entry.httpProtocol().get());
    }

}