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