Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/HeadTest.java
38867 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 688672326* @summary light weight http server doesn't return correct status code for HEAD requests27*/2829import java.net.InetSocketAddress;30import java.net.HttpURLConnection;31import java.net.URL;32import java.io.IOException;33import java.util.concurrent.ExecutorService;34import java.util.concurrent.Executors;35import com.sun.net.httpserver.HttpContext;36import com.sun.net.httpserver.HttpExchange;37import com.sun.net.httpserver.HttpHandler;38import com.sun.net.httpserver.HttpServer;3940public class HeadTest {4142public static void main(String[] args) throws Exception {43server();44}4546static void server() throws Exception {47InetSocketAddress inetAddress = new InetSocketAddress(0);48HttpServer server = HttpServer.create(inetAddress, 5);49try {50server.setExecutor(Executors.newFixedThreadPool(5));51HttpContext chunkedContext = server.createContext("/chunked");52chunkedContext.setHandler(new HttpHandler() {53@Override54public void handle(HttpExchange msg) {55try {56try {57if (msg.getRequestMethod().equals("HEAD")) {58msg.getRequestBody().close();59msg.getResponseHeaders().add("Transfer-encoding", "chunked");60msg.sendResponseHeaders(200, -1);61}62} catch(IOException ioe) {63ioe.printStackTrace();64}65} finally {66msg.close();67}68}69});70HttpContext clContext = server.createContext("/content");71clContext.setHandler(new HttpHandler() {72@Override73public void handle(HttpExchange msg) {74try {75try {76if (msg.getRequestMethod().equals("HEAD")) {77msg.getRequestBody().close();78msg.getResponseHeaders().add("Content-length", "1024");79msg.sendResponseHeaders(200, -1);80}81} catch(IOException ioe) {82ioe.printStackTrace();83}84} finally {85msg.close();86}87}88});89server.start();90String urlStr = "http://localhost:" + server.getAddress().getPort() + "/";91System.out.println("Server is at " + urlStr);9293// Run the chunked client94for(int i=0; i < 10; i++) {95runClient(urlStr + "chunked/");96}97// Run the content length client98for(int i=0; i < 10; i++) {99runClient(urlStr + "content/");100}101} finally {102// Stop the server103((ExecutorService)server.getExecutor()).shutdown();104server.stop(0);105}106}107108static void runClient(String urlStr) throws Exception {109HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();110conn.setRequestMethod("HEAD");111int status = conn.getResponseCode();112if (status != 200) {113throw new RuntimeException("HEAD request doesn't return 200, but returns " + status);114}115}116}117118119