jdk/test/java/net/URLConnection/ZeroContentLength.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4507412
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4506998
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check that a 304 "Not-Modified" response from a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          doesn't cause http client to close a keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          Check that a content-length of 0 results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *          empty input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ZeroContentLength {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Is debugging enabled - start with -d to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void debug(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * The response string and content-length that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * the server should return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static String response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int contentLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static synchronized void setResponse(String rsp, int cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        response = rsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        contentLength = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Worker thread to service single connection - can service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * multiple http requests on same connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Worker(Socket s, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                s.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                int max = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    // read entire request from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    InputStream in = s.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    int n, total=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                            if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    } catch (SocketTimeoutException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    debug("worker " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        ": Read request from client " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        "(" + total + " bytes).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    if (total == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        debug("worker: " + id + ": Shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    // response to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    out.print("HTTP/1.1 " + response + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    if (contentLength >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        out.print("Content-Length: " + contentLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                    "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    for (int i=0; i<contentLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        out.write( (byte)'.' );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    debug("worked " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        ": Sent response to client, length: " + contentLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if (--max == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Server thread to accept connection and create worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * to service each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean shutdown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Server(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public synchronized int connectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public synchronized void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            shutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                ss.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        debug("server: Waiting for connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    } catch (SocketTimeoutException te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            if (shutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                debug("server: Shuting down.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        id = connectionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    Worker w = new Worker(s, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    w.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    debug("server: Started worker " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Make a single http request and return the content length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * received. Also do sanity check to ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * content-length header matches the total received on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    int doRequest(String uri) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        URL url = new URL(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int cl = http.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        InputStream in = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (cl >= 0 && total != cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            System.err.println("content-length header indicated: " + cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            System.err.println("Actual received: " + total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new Exception("Content-length didn't match actual received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Send http requests to "server" and check that they all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * use the same network connection and that the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * length corresponds to the content length expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    ZeroContentLength() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        /* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        Server svr = new Server(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        String uri = "http://localhost:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                     Integer.toString(ss.getLocalPort()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                     "/foo.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int expectedTotal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int actualTotal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.out.println("200 OK, content-length:1024 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        setResponse("200 OK", 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            expectedTotal += 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        System.out.println("200 OK, content-length:0 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        setResponse("200 OK", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println("304 Not-Modified, (no content-length) ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        setResponse("304 Not-Modifed", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        System.out.println("204 No-Content, (no content-length) ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        setResponse("204 No-Content", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // shutdown server - we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (actualTotal == expectedTotal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            System.out.println("Passed: Actual total equal to expected total");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new Exception("Actual total != Expected total!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int cnt = svr.connectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (cnt == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            System.out.println("Passed: Only 1 connection established");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new Exception("Test failed: Number of connections " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                "established: " + cnt + " - see log for details.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (args.length > 0 && args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        new ZeroContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}