Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6369510.java
38867 views
/*1* Copyright (c) 2006, 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 636951026* @run main/othervm B636951027* @summary HttpURLConnection sets Content-Type to application/x-www-form-urlencoded28*/2930import java.net.*;31import java.util.*;32import java.io.*;33import com.sun.net.httpserver.*;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;3637public class B636951038{39com.sun.net.httpserver.HttpServer httpServer;40ExecutorService executorService;4142public static void main(String[] args)43{44new B6369510();45}4647public B6369510()48{49try {50startHttpServer();51doClient();52} catch (IOException ioe) {53System.err.println(ioe);54}55}5657void doClient() {58try {59InetSocketAddress address = httpServer.getAddress();6061// GET Request62URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");63HttpURLConnection uc = (HttpURLConnection)url.openConnection();64int resp = uc.getResponseCode();65if (resp != 200)66throw new RuntimeException("Failed: Response code from GET is not 200");6768System.out.println("Response code from GET = 200 OK");6970//POST Request71uc = (HttpURLConnection)url.openConnection();72uc.setDoOutput(true);73uc.setRequestMethod("POST");74OutputStream os = uc.getOutputStream();75resp = uc.getResponseCode();76if (resp != 200)77throw new RuntimeException("Failed: Response code form POST is not 200");7879System.out.println("Response code from POST = 200 OK");8081} catch (IOException e) {82e.printStackTrace();83} finally {84httpServer.stop(1);85executorService.shutdown();86}87}8889/**90* Http Server91*/92public void startHttpServer() throws IOException {93httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);9495// create HttpServer context96HttpContext ctx = httpServer.createContext("/test/", new MyHandler());9798executorService = Executors.newCachedThreadPool();99httpServer.setExecutor(executorService);100httpServer.start();101}102103class MyHandler implements HttpHandler {104public void handle(HttpExchange t) throws IOException {105InputStream is = t.getRequestBody();106Headers reqHeaders = t.getRequestHeaders();107Headers resHeaders = t.getResponseHeaders();108while (is.read () != -1) ;109is.close();110111List<String> ct = reqHeaders.get("content-type");112String requestMethod = t.getRequestMethod();113114if (requestMethod.equalsIgnoreCase("GET") && ct != null &&115ct.get(0).equals("application/x-www-form-urlencoded"))116t.sendResponseHeaders(400, -1);117118else if (requestMethod.equalsIgnoreCase("POST") && ct != null &&119!ct.get(0).equals("application/x-www-form-urlencoded"))120t.sendResponseHeaders(400, -1);121122t.sendResponseHeaders(200, -1);123t.close();124}125}126}127128129