Path: blob/master/test/jdk/sun/net/www/http/HttpURLConnection/PostOnDelete.java
41154 views
/*1* Copyright (c) 2013, 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*/2223import com.sun.net.httpserver.*;24import java.io.IOException;25import java.io.InputStream;26import java.io.OutputStream;27import java.net.*;2829/*30* @test31* @bug 715736032* @modules jdk.httpserver33* @summary HttpURLConnection: HTTP method DELETE doesn't support output34*/35public class PostOnDelete {3637/* string to send */38private static String msg = "Hello Server";39/* length of the string to verify */40private int len = msg.length();4142public static void main(String[] args) throws Exception {43new PostOnDelete().runTest();44}4546public void runTest() throws Exception {47Server s = null;48try {49s = new Server();50s.startServer();51URL url = new URL("http://" + s.getAuthority());52HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();53urlConnection.setRequestMethod("DELETE");54urlConnection.setDoOutput(true);55OutputStream os = urlConnection.getOutputStream();56os.write(msg.getBytes());57os.close();58int code = urlConnection.getResponseCode();5960if (code != 200) {61throw new RuntimeException("Request entity for DELETE failed!");62}63} finally {64s.stopServer();65}66}6768class Server {69HttpServer server;7071public void startServer() {72InetAddress loopback = InetAddress.getLoopbackAddress();73InetSocketAddress addr = new InetSocketAddress(loopback,0);74try {75server = HttpServer.create(addr, 0);76} catch (IOException ioe) {77throw new RuntimeException("Server could not be created");78}7980server.createContext("/", new EmptyPathHandler());81server.start();82}8384public String getAuthority() {85InetAddress address = server.getAddress().getAddress();86String hostaddr = address.isAnyLocalAddress() ? "localhost" : address.getHostAddress();87hostaddr = (hostaddr.indexOf(':') >= 0) ? ("[" + hostaddr + "]") : hostaddr;88return hostaddr + ":" + getPort();89}9091public int getPort() {92return server.getAddress().getPort();93}9495public void stopServer() {96server.stop(0);97}98}99100class EmptyPathHandler implements HttpHandler {101102@Override103public void handle(HttpExchange exchange) throws IOException {104String requestMethod = exchange.getRequestMethod();105106if (requestMethod.equalsIgnoreCase("DELETE")) {107InputStream is = exchange.getRequestBody();108109int count = 0;110while (is.read() != -1) {111count++;112}113is.close();114115Headers responseHeaders = exchange.getResponseHeaders();116responseHeaders.set("Content-Type", "text/plain");117exchange.sendResponseHeaders((count == len) ? 200 : 400, 0);118OutputStream os = exchange.getResponseBody();119String str = "Hello from server!";120os.write(str.getBytes());121os.flush();122os.close();123}124}125}126}127128129