|
| 1 | +package robaho.net.httpserver.http2; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.InetSocketAddress; |
| 5 | +import java.net.URI; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpClient.Version; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse.BodyHandlers; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.logging.ConsoleHandler; |
| 13 | +import java.util.logging.Level; |
| 14 | +import java.util.logging.Logger; |
| 15 | + |
| 16 | +import org.testng.Assert; |
| 17 | +import org.testng.annotations.AfterMethod; |
| 18 | +import org.testng.annotations.BeforeMethod; |
| 19 | +import org.testng.annotations.Test; |
| 20 | + |
| 21 | +import com.sun.net.httpserver.HttpContext; |
| 22 | +import com.sun.net.httpserver.HttpExchange; |
| 23 | +import com.sun.net.httpserver.HttpHandler; |
| 24 | +import com.sun.net.httpserver.HttpServer; |
| 25 | + |
| 26 | +import robaho.net.httpserver.Http2ExchangeImpl; |
| 27 | +import robaho.net.httpserver.LoggingFilter; |
| 28 | + |
| 29 | +@Test(enabled=false) // this is disabled since JDK HttpClient cannot perform "prior knowledge" Http2 connections over non-SSL |
| 30 | + |
| 31 | +public class Http2Test { |
| 32 | + |
| 33 | + static { |
| 34 | + // System.setProperty("jdk.httpclient.HttpClient.log", "all"); |
| 35 | + // System.setProperty("jdk.internal.httpclient.websocket.debug", "true"); |
| 36 | + } |
| 37 | + |
| 38 | + private static final int port = 9000; |
| 39 | + private static final String path = "/echo"; |
| 40 | + |
| 41 | + HttpServer server; |
| 42 | + |
| 43 | + private volatile boolean foundHttp2 = false; |
| 44 | + |
| 45 | + @BeforeMethod |
| 46 | + public void setUp() throws IOException { |
| 47 | + Logger logger = Logger.getLogger(Http2Test.class.getName()); |
| 48 | + ConsoleHandler ch = new ConsoleHandler(); |
| 49 | + logger.setLevel(Level.ALL); |
| 50 | + ch.setLevel(Level.ALL); |
| 51 | + logger.addHandler(ch); |
| 52 | + |
| 53 | + server = HttpServer.create(new InetSocketAddress(port), 0); |
| 54 | + HttpHandler h = new EchoHandler(); |
| 55 | + HttpContext c = server.createContext(path, h); |
| 56 | + c.getFilters().add(new LoggingFilter(logger)); |
| 57 | + server.setExecutor(Executors.newCachedThreadPool()); |
| 58 | + server.start(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterMethod |
| 62 | + public void tearDown() { |
| 63 | + server.stop(0); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testHttp2Request() throws InterruptedException, IOException, URISyntaxException { |
| 68 | + var client = HttpClient.newBuilder().version(Version.HTTP_2).build(); |
| 69 | + var request = HttpRequest.newBuilder(new URI("http://localhost:9000"+path)).POST(HttpRequest.BodyPublishers.ofString("This is a test")).build(); |
| 70 | + var response = client.send(request,BodyHandlers.ofString()); |
| 71 | + Assert.assertEquals(response.body(),"This is a test"); |
| 72 | + Assert.assertTrue(foundHttp2); |
| 73 | + } |
| 74 | + |
| 75 | + private class EchoHandler implements HttpHandler { |
| 76 | + |
| 77 | + @Override |
| 78 | + public void handle(HttpExchange he) throws IOException { |
| 79 | + if(he instanceof Http2ExchangeImpl) { |
| 80 | + foundHttp2 = true; |
| 81 | + } |
| 82 | + he.sendResponseHeaders(200,0); |
| 83 | + try (var exchange = he) { |
| 84 | + exchange.getRequestBody().transferTo(exchange.getResponseBody()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments