blob: 347db29745c7814461e00077ae85d91ae998c47f [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 6472250
27 * @run main/othervm StreamingOutputStream
28 * @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times
29 */
30
31import java.net.*;
32import java.io.*;
33import com.sun.net.httpserver.*;
34
35/*
36 * Calling HttpURLConnection.getOutputStream multiple times when streaming
37 * should not create a new OutputStream each time.
38 */
39
40public class StreamingOutputStream
41{
42 com.sun.net.httpserver.HttpServer httpServer;
43
44 public static void main(String[] args) {
45 new StreamingOutputStream();
46 }
47
48 public StreamingOutputStream() {
49 try {
50 startHttpServer();
51 doClient();
52 } catch (IOException ioe) {
53 ioe.printStackTrace();
54 }
55 }
56
57 void doClient() {
58 try {
59 InetSocketAddress address = httpServer.getAddress();
60
61 URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
62 HttpURLConnection uc = (HttpURLConnection)url.openConnection();
63
64 uc.setDoOutput(true);
65 uc.setFixedLengthStreamingMode(1);
66 OutputStream os1 = uc.getOutputStream();
67 OutputStream os2 = uc.getOutputStream();
68
69 if (os1 != os2)
70 throw new RuntimeException("Failed: OutputStreams should reference the same object");
71
72 os2.write('b');
73 os2.close();
74
75 int resp = uc.getResponseCode();
76 if (resp != 200)
77 throw new RuntimeException("Failed: Server should return 200 OK");
78
79 } catch (IOException e) {
80 e.printStackTrace();
81 } finally {
82 httpServer.stop(1);
83 }
84 }
85 /**
86 * Http Server
87 */
88 void startHttpServer() throws IOException {
89 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90 HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
91 httpServer.start();
92 }
93
94 class MyHandler implements HttpHandler {
95 public void handle(HttpExchange t) throws IOException {
96 InputStream is = t.getRequestBody();
97 while(is.read() != -1);
98 is.close();
99
100 t.sendResponseHeaders(200, -1);
101 t.close();
102 }
103 }
104}