B6369510.java revision 14606:bc3775e25b52
1/*
2 * Copyright (c) 2006, 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
24/*
25 * @test
26 * @bug 6369510
27 * @modules jdk.httpserver
28 * @run main/othervm B6369510
29 * @summary HttpURLConnection sets Content-Type to application/x-www-form-urlencoded
30 */
31
32import java.net.*;
33import java.util.*;
34import java.io.*;
35import com.sun.net.httpserver.*;
36import java.util.concurrent.Executors;
37import java.util.concurrent.ExecutorService;
38
39public class B6369510
40{
41    com.sun.net.httpserver.HttpServer httpServer;
42    ExecutorService executorService;
43
44    public static void main(String[] args) throws Exception
45    {
46        new B6369510();
47    }
48
49    public B6369510()
50    {
51        try {
52            startHttpServer();
53            doClient();
54        } catch (IOException ioe) {
55            System.err.println(ioe);
56        }
57    }
58
59    void doClient() {
60        try {
61            InetSocketAddress address = httpServer.getAddress();
62            String urlString = "http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/";
63            System.out.println("URL == " + urlString);
64
65            // GET Request
66            URL url = new URL("http://" + InetAddress.getLocalHost().getHostName() + ":" + address.getPort() + "/test/");
67            HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
68            int resp = uc.getResponseCode();
69            if (resp != 200)
70                throw new RuntimeException("Failed: Response code from GET is not 200 RSP == " + resp);
71
72            System.out.println("Response code from GET = 200 OK");
73
74            //POST Request
75            uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
76            uc.setDoOutput(true);
77            uc.setRequestMethod("POST");
78            OutputStream os = uc.getOutputStream();
79            resp = uc.getResponseCode();
80            if (resp != 200)
81                throw new RuntimeException("Failed: Response code form POST is not 200 RSP == " + resp);
82
83            System.out.println("Response code from POST = 200 OK");
84
85        } catch (IOException e) {
86            e.printStackTrace();
87            throw new RuntimeException("Failed with IOException");
88        } finally {
89            httpServer.stop(1);
90            executorService.shutdown();
91        }
92    }
93
94    /**
95     * Http Server
96     */
97    public void startHttpServer() throws IOException {
98        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
99
100        // create HttpServer context
101        HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
102
103        executorService = Executors.newCachedThreadPool();
104        httpServer.setExecutor(executorService);
105        httpServer.start();
106    }
107
108    class MyHandler implements HttpHandler {
109        public void handle(HttpExchange t) throws IOException {
110            InputStream is = t.getRequestBody();
111            Headers reqHeaders = t.getRequestHeaders();
112            Headers resHeaders = t.getResponseHeaders();
113            while (is.read () != -1) ;
114            is.close();
115
116            List<String> ct = reqHeaders.get("content-type");
117            String requestMethod = t.getRequestMethod();
118
119            if (requestMethod.equalsIgnoreCase("GET") && ct != null &&
120                ct.get(0).equals("application/x-www-form-urlencoded"))
121                t.sendResponseHeaders(400, -1);
122
123            else if (requestMethod.equalsIgnoreCase("POST") && ct != null &&
124                     !ct.get(0).equals("application/x-www-form-urlencoded"))
125                t.sendResponseHeaders(400, -1);
126
127            t.sendResponseHeaders(200, -1);
128            t.close();
129        }
130    }
131}
132