PostOnDelete.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import com.sun.net.httpserver.*; 25import java.io.IOException; 26import java.io.InputStream; 27import java.io.OutputStream; 28import java.net.*; 29 30/* 31 * @test 32 * @bug 7157360 33 * @modules jdk.httpserver 34 * @summary HttpURLConnection: HTTP method DELETE doesn't support output 35 */ 36public class PostOnDelete { 37 38 /* string to send */ 39 private static String msg = "Hello Server"; 40 /* length of the string to verify */ 41 private int len = msg.length(); 42 43 public static void main(String[] args) throws Exception { 44 new PostOnDelete().runTest(); 45 } 46 47 public void runTest() throws Exception { 48 Server s = null; 49 try { 50 s = new Server(); 51 s.startServer(); 52 URL url = new URL("http://localhost:" + s.getPort()); 53 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 54 urlConnection.setRequestMethod("DELETE"); 55 urlConnection.setDoOutput(true); 56 OutputStream os = urlConnection.getOutputStream(); 57 os.write(msg.getBytes()); 58 os.close(); 59 int code = urlConnection.getResponseCode(); 60 61 if (code != 200) { 62 throw new RuntimeException("Request entity for DELETE failed!"); 63 } 64 } finally { 65 s.stopServer(); 66 } 67 } 68 69 class Server { 70 HttpServer server; 71 72 public void startServer() { 73 InetSocketAddress addr = new InetSocketAddress(0); 74 try { 75 server = HttpServer.create(addr, 0); 76 } catch (IOException ioe) { 77 throw new RuntimeException("Server could not be created"); 78 } 79 80 server.createContext("/", new EmptyPathHandler()); 81 server.start(); 82 } 83 84 public int getPort() { 85 return server.getAddress().getPort(); 86 } 87 88 public void stopServer() { 89 server.stop(0); 90 } 91 } 92 93 class EmptyPathHandler implements HttpHandler { 94 95 @Override 96 public void handle(HttpExchange exchange) throws IOException { 97 String requestMethod = exchange.getRequestMethod(); 98 99 if (requestMethod.equalsIgnoreCase("DELETE")) { 100 InputStream is = exchange.getRequestBody(); 101 102 int count = 0; 103 while (is.read() != -1) { 104 count++; 105 } 106 is.close(); 107 108 Headers responseHeaders = exchange.getResponseHeaders(); 109 responseHeaders.set("Content-Type", "text/plain"); 110 exchange.sendResponseHeaders((count == len) ? 200 : 400, 0); 111 OutputStream os = exchange.getResponseBody(); 112 String str = "Hello from server!"; 113 os.write(str.getBytes()); 114 os.flush(); 115 os.close(); 116 } 117 } 118 } 119} 120