1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8175814 27 * @modules jdk.incubator.httpclient java.logging jdk.httpserver 28 * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,requests,headers,trace VersionTest 29 */ 30 31 import com.sun.net.httpserver.Headers; 32 import com.sun.net.httpserver.HttpContext; 33 import com.sun.net.httpserver.HttpExchange; 34 import com.sun.net.httpserver.HttpHandler; 35 import com.sun.net.httpserver.HttpServer; 36 import java.io.IOException; 37 import java.io.OutputStream; 38 import java.net.URI; 39 import java.util.concurrent.Executors; 40 import java.util.concurrent.ExecutorService; 41 import java.net.InetSocketAddress; 42 import jdk.incubator.http.HttpClient; 43 import jdk.incubator.http.HttpRequest; 44 import jdk.incubator.http.HttpResponse; 45 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; 46 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; 47 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; 48 import static jdk.incubator.http.HttpClient.Version.HTTP_1_1; 49 import static jdk.incubator.http.HttpClient.Version.HTTP_2; 50 51 /** 52 */ 53 public class VersionTest { 54 static HttpServer s1 ; 55 static ExecutorService executor; 56 static int port; 57 static HttpClient client; 58 static URI uri; 59 static volatile boolean error = false; 60 61 public static void main(String[] args) throws Exception { 62 initServer(); 63 64 client = HttpClient.newBuilder() 65 .executor(executor) 66 .build(); 67 // first check that the version is HTTP/2 68 if (client.version() != HttpClient.Version.HTTP_2) { 69 throw new RuntimeException("Default version not HTTP_2"); 70 } 71 try { 72 test(HTTP_1_1); 73 test(HTTP_2); 74 } finally { 75 s1.stop(0); 76 executor.shutdownNow(); 77 } 78 if (error) 79 throw new RuntimeException(); 80 } 81 82 public static void test(HttpClient.Version version) throws Exception { 83 HttpRequest r = HttpRequest.newBuilder(uri) 84 .version(version) 85 .GET() 86 .build(); 87 HttpResponse<Void> resp = client.send(r, discard(null)); 88 System.out.printf("Client: response is %d\n", resp.statusCode()); 89 if (resp.version() != HTTP_1_1) { 90 throw new RuntimeException(); 91 } 92 //System.out.printf("Client: response body is %s\n", resp.body()); 93 } 94 95 static void initServer() throws Exception { 96 InetSocketAddress addr = new InetSocketAddress (0); 97 s1 = HttpServer.create (addr, 0); 98 HttpHandler h = new Handler(); 99 100 HttpContext c1 = s1.createContext("/", h); 101 102 executor = Executors.newCachedThreadPool(); 103 s1.setExecutor(executor); 104 s1.start(); 105 106 port = s1.getAddress().getPort(); 107 uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo"); 108 System.out.println("HTTP server port = " + port); 109 } 110 } 111 112 class Handler implements HttpHandler { 113 int counter = 0; 114 115 void checkHeader(Headers h) { 116 counter++; 117 if (counter == 1 && h.containsKey("Upgrade")) { 118 VersionTest.error = true; 119 } 120 if (counter > 1 && !h.containsKey("Upgrade")) { 121 VersionTest.error = true; 122 } 123 } 124 125 @Override 126 public synchronized void handle(HttpExchange t) 127 throws IOException 128 { 129 String reply = "Hello world"; 130 int len = reply.length(); 131 Headers h = t.getRequestHeaders(); 132 checkHeader(h); 133 System.out.printf("Sending response 200\n"); 134 t.sendResponseHeaders(200, len); 135 OutputStream o = t.getResponseBody(); 136 o.write(reply.getBytes()); 137 t.close(); 138 } 139 }