Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpURLConnection/PostOnDelete.java
38867 views
/*1* Copyright (c) 2013, 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* @summary HttpURLConnection: HTTP method DELETE doesn't support output33*/34public class PostOnDelete {3536/* string to send */37private static String msg = "Hello Server";38/* length of the string to verify */39private int len = msg.length();4041public static void main(String[] args) throws Exception {42new PostOnDelete().runTest();43}4445public void runTest() throws Exception {46Server s = null;47try {48s = new Server();49s.startServer();50URL url = new URL("http://localhost:" + s.getPort());51HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();52urlConnection.setRequestMethod("DELETE");53urlConnection.setDoOutput(true);54OutputStream os = urlConnection.getOutputStream();55os.write(msg.getBytes());56os.close();57int code = urlConnection.getResponseCode();5859if (code != 200) {60throw new RuntimeException("Request entity for DELETE failed!");61}62} finally {63s.stopServer();64}65}6667class Server {68HttpServer server;6970public void startServer() {71InetSocketAddress addr = new InetSocketAddress(0);72try {73server = HttpServer.create(addr, 0);74} catch (IOException ioe) {75throw new RuntimeException("Server could not be created");76}7778server.createContext("/", new EmptyPathHandler());79server.start();80}8182public int getPort() {83return server.getAddress().getPort();84}8586public void stopServer() {87server.stop(0);88}89}9091class EmptyPathHandler implements HttpHandler {9293@Override94public void handle(HttpExchange exchange) throws IOException {95String requestMethod = exchange.getRequestMethod();9697if (requestMethod.equalsIgnoreCase("DELETE")) {98InputStream is = exchange.getRequestBody();99100int count = 0;101while (is.read() != -1) {102count++;103}104is.close();105106Headers responseHeaders = exchange.getResponseHeaders();107responseHeaders.set("Content-Type", "text/plain");108exchange.sendResponseHeaders((count == len) ? 200 : 400, 0);109OutputStream os = exchange.getResponseBody();110String str = "Hello from server!";111os.write(str.getBytes());112os.flush();113os.close();114}115}116}117}118119120