jdk/test/sun/net/www/http/ChunkedOutputStream/Test.java
author jjiang
Wed, 25 May 2016 03:06:24 +0100
changeset 38557 5c485e1ea6fa
parent 7668 d4a77089c587
permissions -rw-r--r--
8157635: Fix module dependencies for /sun/* tests Summary: Module declarations for the tests in /sun/* Reviewed-by: alanb Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 7668
diff changeset
     2
 * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
    26
 * @bug 5026745 6631048
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 7668
diff changeset
    27
 * @modules jdk.httpserver
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm/timeout=500 Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary Cannot flush output stream when writing to an HttpUrlConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Test implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    38
    static volatile int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                "1234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public void handle(HttpExchange exchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String reqbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    50
            switch (exchange.getRequestURI().toString()) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    51
            case "/test/test1": /* test1 -- keeps conn alive */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    52
            case "/test/test2": /* test2 -- closes conn */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                if (!reqbody.equals(str1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                Headers headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                String chunk =  headers.getFirst("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                if (!"chunked".equals (chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    Headers resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    76
            case "/test/test3": /* test 3 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (!reqbody.equals(str2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                int clen = Integer.parseInt( headers.getFirst("Content-length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (clen != str2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                Headers resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    97
            case "/test/test4": /* test 4 */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    98
            case "/test/test5": /* test 5 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   101
            case "/test/test6": /* test 6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                resHeaders.set("Location", "http://foo.bar/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                exchange.sendResponseHeaders(307, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   108
            case "/test/test7": /* test 7 */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   109
            case "/test/test8": /* test 8 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (reqbody != null && !"".equals(reqbody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                exchange.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   120
            case "/test/test9": /* test 9 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                if (!reqbody.equals(str1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                chunk =  headers.getFirst("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (!"chunked".equals(chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   138
            case "/test/test10": /* test10 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                InputStream is = exchange.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                String s = read (is, str1.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                for (int i=10; i< 200 * 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    byte c = (byte)is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (c != (byte)i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        System.out.println ("error at position " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (!s.equals(str1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    System.out.println ("received string : " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                } else if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    System.out.println ("error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    exchange.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   162
            case "/test/test11": /* test11 */
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   163
                printRequestURI(exchange);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   164
                is = exchange.getRequestBody();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   165
                s = read (is, str1.length());
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   166
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   167
                error = false;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   168
                for (int i=10; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   169
                    byte c = (byte)is.read();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   170
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   171
                    if (c != (byte)i) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   172
                        error = true;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   173
                        System.out.println ("error at position " + i);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   174
                    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   175
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   176
                if (!s.equals(str1) ) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   177
                    System.out.println ("received string : " + s);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   178
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   179
                } else if (error) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   180
                    System.out.println ("error");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   181
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   182
                } else {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   183
                    exchange.sendResponseHeaders(200, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   184
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   185
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   186
            case "/test/test12": /* test12 */
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   187
                printRequestURI(exchange);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   188
                is = exchange.getRequestBody();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   189
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   190
                error = false;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   191
                for (int i=10; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   192
                    byte c = (byte)is.read();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   193
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   194
                    if (c != (byte)i) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   195
                        error = true;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   196
                        System.out.println ("error at position " + i);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   197
                    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   198
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   199
                if (error) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   200
                    System.out.println ("error");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   201
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   202
                } else {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   203
                    exchange.sendResponseHeaders(200, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   204
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   205
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   207
            count ++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            exchange.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    static void printRequestURI(HttpExchange exchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        URI uri = exchange.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        System.out.println("HttpServer: handle " + uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static String read (InputStream is, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            byte[] ba = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                l += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return new String (ba, 0, l, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static String read(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            byte[] ba = new byte [8096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int off = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            while ((c= is.read(ba, off, ba.length)) != -1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return new String(ba, 0, off, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static void write(OutputStream os, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            byte[] ba = str.getBytes("ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            os.write(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static void readAndCompare(InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        byte buf[] = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int len = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        while ((c=is.read(buf, off, len)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            len -= c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        String s1 = new String(buf, 0, off, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (!cmp.equals(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new IOException("strings not same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /* basic chunked test (runs twice) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static void test1 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /* basic fixed length test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    static void test3 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        urlc.setFixedLengthStreamingMode (str2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        readAndCompare (is, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /* write too few bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    static void test4 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        urlc.setFixedLengthStreamingMode (str2.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /* write too many bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static void test5 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        urlc.setFixedLengthStreamingMode (str2.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* check for HttpRetryException on redirection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    static void test6 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new Exception ("should have gotten HttpRetryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (HttpRetryException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (e.responseCode() != 307) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                throw new Exception ("Wrong response code " + e.responseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (!e.getLocation().equals ("http://foo.bar/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throw new Exception ("Wrong location " + e.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /* next two tests send zero length posts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    static void test7 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    static void test8 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        urlc.setFixedLengthStreamingMode (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /* calling setChunkedStreamingMode with -1 should entail using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
       the default chunk size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
   static void test9 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        urlc.setChunkedStreamingMode (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
   static void test10 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        urlc.setChunkedStreamingMode (4 * 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        byte[] buf = new byte [200 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        for (int i=0; i< 200 * 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            buf[i] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /* write a small bit first, and then the large buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   438
    static void test11 (String u) throws Exception {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   439
        URL url = new URL (u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   440
        System.out.println ("client opening connection to: " + u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   441
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   442
        urlc.setChunkedStreamingMode (36 * 1024);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   443
        urlc.setDoOutput(true);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   444
        urlc.setRequestMethod ("POST");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   445
        OutputStream os = urlc.getOutputStream ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   446
        byte[] buf = new byte [30 * 1024];
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   447
        for (int i=0; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   448
            buf[i] = (byte) i;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   449
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   450
        /* write a small bit first, and then the large buffer */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   451
        os.write (str1.getBytes());
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   452
        //os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   453
        os.write (buf, 10, (10 * 1024) - 10);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   454
        os.write (buf, (10 * 1024), (10 * 1024));
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   455
        os.write (buf, (20 * 1024), (10 * 1024));
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   456
        os.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   457
        InputStream is = urlc.getInputStream();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   458
        is.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   459
        int ret = urlc.getResponseCode();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   460
        if (ret != 200) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   461
            throw new Exception ("Expected 200: got " + ret);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   462
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   463
    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   464
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   465
    static void test12 (String u) throws Exception {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   466
        URL url = new URL (u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   467
        System.out.println ("client opening connection to: " + u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   468
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   469
        urlc.setChunkedStreamingMode (36 * 1024);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   470
        urlc.setDoOutput(true);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   471
        urlc.setRequestMethod ("POST");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   472
        OutputStream os = urlc.getOutputStream ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   473
        byte[] buf = new byte [30 * 1024];
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   474
        for (int i=0; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   475
            buf[i] = (byte) i;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   476
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   477
        os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   478
        os.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   479
        InputStream is = urlc.getInputStream();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   480
        is.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   481
        int ret = urlc.getResponseCode();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   482
        if (ret != 200) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   483
            throw new Exception ("Expected 200: got " + ret);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   484
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   485
    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   486
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   487
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    static com.sun.net.httpserver.HttpServer httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            HttpContext ctx = httpserver.createContext("/test/", new Test() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            httpserver.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            int port = httpserver.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            System.out.println ("Server started: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            test1("http://localhost:"+ port + "/test/test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            test1("http://localhost:"+ port + "/test/test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            test3("http://localhost:"+ port + "/test/test3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            test4("http://localhost:"+ port + "/test/test4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            test5("http://localhost:"+ port + "/test/test5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            test6("http://localhost:"+ port + "/test/test6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            test7("http://localhost:"+ port + "/test/test7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            test8("http://localhost:"+ port + "/test/test8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            test9("http://localhost:"+ port + "/test/test9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            test10("http://localhost:"+ port + "/test/test10");
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   509
            test11("http://localhost:"+ port + "/test/test11");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   510
            test12("http://localhost:"+ port + "/test/test12");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (httpserver != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                httpserver.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
}