Path: blob/master/test/jdk/sun/net/www/protocol/http/B6369510.java
41159 views
/*1* Copyright (c) 2006, 2019, 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* @modules jdk.httpserver27* @run main/othervm B636951028* @summary HttpURLConnection sets Content-Type to application/x-www-form-urlencoded29*/3031import java.net.*;32import java.util.*;33import java.io.*;34import com.sun.net.httpserver.*;35import java.util.concurrent.Executors;36import java.util.concurrent.ExecutorService;3738public class B636951039{40com.sun.net.httpserver.HttpServer httpServer;41ExecutorService executorService;4243public static void main(String[] args) throws Exception44{45new B6369510();46}4748public B6369510()49{50try {51startHttpServer();52doClient();53} catch (IOException ioe) {54System.err.println(ioe);55}56}5758void doClient() {59try {60InetSocketAddress address = httpServer.getAddress();61String urlString = "http://" + InetAddress.getLocalHost().getHostName()62+ ":" + address.getPort() + "/test/";63System.out.println("URL == " + urlString);6465// GET Request66URL url = new URL(urlString);67HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);68int resp = uc.getResponseCode();69if (resp != 200)70throw new RuntimeException("Failed: Response code from GET is not 200 RSP == " + resp);7172System.out.println("Response code from GET = 200 OK");7374//POST Request75uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);76uc.setDoOutput(true);77uc.setRequestMethod("POST");78OutputStream os = uc.getOutputStream();79resp = uc.getResponseCode();80if (resp != 200)81throw new RuntimeException("Failed: Response code form POST is not 200 RSP == " + resp);8283System.out.println("Response code from POST = 200 OK");8485} catch (IOException e) {86e.printStackTrace();87throw new RuntimeException("Failed with IOException");88} finally {89httpServer.stop(1);90executorService.shutdown();91}92}9394/**95* Http Server96*/97public void startHttpServer() throws IOException {98InetAddress localhost = InetAddress.getLocalHost();99httpServer = HttpServer.create(new InetSocketAddress(localhost, 0), 0);100101// create HttpServer context102HttpContext ctx = httpServer.createContext("/test/", new MyHandler());103104executorService = Executors.newCachedThreadPool();105httpServer.setExecutor(executorService);106httpServer.start();107}108109class MyHandler implements HttpHandler {110public void handle(HttpExchange t) throws IOException {111InputStream is = t.getRequestBody();112Headers reqHeaders = t.getRequestHeaders();113Headers resHeaders = t.getResponseHeaders();114while (is.read () != -1) ;115is.close();116117List<String> ct = reqHeaders.get("content-type");118String requestMethod = t.getRequestMethod();119120if (requestMethod.equalsIgnoreCase("GET") && ct != null &&121ct.get(0).equals("application/x-www-form-urlencoded"))122t.sendResponseHeaders(400, -1);123124else if (requestMethod.equalsIgnoreCase("POST") && ct != null &&125!ct.get(0).equals("application/x-www-form-urlencoded"))126t.sendResponseHeaders(400, -1);127128t.sendResponseHeaders(200, -1);129t.close();130}131}132}133134135