test/jdk/java/net/httpclient/RetryPost.java
author chegar
Fri, 01 Jun 2018 16:43:54 +0100
branchhttp-client-branch
changeset 56652 8e00f02b7dfc
permissions -rw-r--r--
http-client-branch: fix bug in property value reading
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56652
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     1
/*
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     4
 *
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     8
 *
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    13
 * accompanied this code).
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    14
 *
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    18
 *
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    21
 * questions.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    22
 */
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    23
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    24
/*
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    25
 * @test
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    26
 * @summary Ensure that the POST method is retied when the property is set.
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    27
 * @run testng/othervm -Djdk.httpclient.enableAllMethodRetry RetryPost
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    28
 * @run testng/othervm -Djdk.httpclient.enableAllMethodRetry=true RetryPost
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    29
 */
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    30
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    31
import java.io.IOException;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    32
import java.io.InputStream;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    33
import java.io.OutputStream;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    34
import java.io.UncheckedIOException;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    35
import java.net.InetAddress;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    36
import java.net.InetSocketAddress;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    37
import java.net.ServerSocket;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    38
import java.net.Socket;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    39
import java.net.URI;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    40
import java.net.http.HttpClient;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    41
import java.net.http.HttpRequest;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    42
import java.net.http.HttpRequest.BodyPublishers;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    43
import java.net.http.HttpResponse;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    44
import org.testng.annotations.AfterTest;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    45
import org.testng.annotations.BeforeTest;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    46
import org.testng.annotations.DataProvider;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    47
import org.testng.annotations.Test;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    48
import static java.lang.System.out;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    49
import static java.net.http.HttpClient.Builder.NO_PROXY;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    50
import static java.net.http.HttpResponse.BodyHandlers.ofString;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    51
import static java.nio.charset.StandardCharsets.US_ASCII;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    52
import static org.testng.Assert.assertEquals;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    53
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    54
public class RetryPost {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    55
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    56
    FixedLengthServer fixedLengthServer;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    57
    String httpURIFixLen;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    58
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    59
    static final String RESPONSE_BODY =
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    60
            "You use a glass mirror to see your face: you use works of art to see your soul.";
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    61
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    62
    @DataProvider(name = "uris")
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    63
    public Object[][] variants() {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    64
        return new Object[][] {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    65
                { httpURIFixLen, true  },
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    66
                { httpURIFixLen, false },
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    67
        };
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    68
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    69
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    70
    static final int ITERATION_COUNT = 3;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    71
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    72
    static final String REQUEST_BODY = "Body";
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    73
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    74
    @Test(dataProvider = "uris")
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    75
    void testSynchronousPOST(String url, boolean sameClient) throws Exception  {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    76
        out.print("---\n");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    77
        HttpClient client = null;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    78
        for (int i=0; i< ITERATION_COUNT; i++) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    79
            if (!sameClient || client == null)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    80
                client = HttpClient.newBuilder().proxy(NO_PROXY).build();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    81
            HttpRequest request = HttpRequest.newBuilder(URI.create(url))
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    82
                    .POST(BodyPublishers.ofString(REQUEST_BODY))
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    83
                    .build();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    84
            HttpResponse<String> response = client.send(request, ofString());
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    85
            String body = response.body();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    86
            out.println(response + ": " + body);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    87
            assertEquals(response.statusCode(), 200);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    88
            assertEquals(body, RESPONSE_BODY);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    89
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    90
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    91
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    92
    @Test(dataProvider = "uris")
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    93
    void testAsynchronousPOST(String url, boolean sameClient) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    94
        out.print("---\n");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    95
        HttpClient client = null;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    96
        for (int i=0; i< ITERATION_COUNT; i++) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    97
            if (!sameClient || client == null)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    98
                client = HttpClient.newBuilder().proxy(NO_PROXY).build();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
    99
            HttpRequest request = HttpRequest.newBuilder(URI.create(url))
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   100
                    .POST(BodyPublishers.ofString(REQUEST_BODY))
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   101
                    .build();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   102
            client.sendAsync(request, ofString())
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   103
                    .thenApply(r -> { out.println(r + ": " + r.body()); return r; })
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   104
                    .thenApply(r -> { assertEquals(r.statusCode(), 200); return r; })
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   105
                    .thenApply(HttpResponse::body)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   106
                    .thenAccept(b -> assertEquals(b, RESPONSE_BODY))
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   107
                    .join();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   108
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   109
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   110
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   111
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   112
    /**
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   113
     * A server that issues a valid fixed-length reply on even requests, and
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   114
     * immediately closes the connection on odd requests ( tick-tock ).
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   115
     */
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   116
    static class FixedLengthServer extends Thread implements AutoCloseable {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   117
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   118
        static final String RESPONSE_HEADERS =
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   119
                "HTTP/1.1 200 OK\r\n" +
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   120
                "Content-Type: text/html; charset=utf-8\r\n" +
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   121
                "Content-Length: " + RESPONSE_BODY.length() + "\r\n" +
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   122
                "Connection: close\r\n\r\n";
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   123
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   124
        static final String RESPONSE = RESPONSE_HEADERS + RESPONSE_BODY;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   125
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   126
        private final ServerSocket ss;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   127
        private volatile boolean closed;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   128
        private int invocationTimes;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   129
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   130
        FixedLengthServer() throws IOException {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   131
            super("FixedLengthServer");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   132
            ss = new ServerSocket();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   133
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   134
            this.start();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   135
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   136
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   137
        public int getPort() { return ss.getLocalPort(); }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   138
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   139
        @Override
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   140
        public void run() {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   141
            while (!closed) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   142
                try (Socket s = ss.accept()) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   143
                    invocationTimes++;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   144
                    out.print("FixedLengthServer: got connection ");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   145
                    if ((invocationTimes & 0x1) == 0x1) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   146
                        out.println(" closing immediately");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   147
                        s.close();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   148
                        continue;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   149
                    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   150
                    InputStream is = s.getInputStream();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   151
                    URI requestMethod = readRequestMethod(is);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   152
                    out.print(requestMethod + " ");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   153
                    URI uriPath = readRequestPath(is);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   154
                    out.println(uriPath);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   155
                    readRequestHeaders(is);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   156
                    byte[] body = is.readNBytes(4);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   157
                    assert body.length == REQUEST_BODY.length() :
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   158
                            "Unexpected request body " + body.length;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   159
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   160
                    OutputStream os = s.getOutputStream();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   161
                    out.println("Server: writing response bytes");
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   162
                    byte[] responseBytes = RESPONSE.getBytes(US_ASCII);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   163
                    os.write(responseBytes);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   164
                } catch (IOException e) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   165
                    if (!closed)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   166
                        throw new UncheckedIOException("Unexpected", e);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   167
                }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   168
            }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   169
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   170
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   171
        @Override
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   172
        public void close() {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   173
            if (closed)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   174
                return;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   175
            closed = true;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   176
            try {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   177
                ss.close();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   178
            } catch (IOException e) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   179
                throw new UncheckedIOException("Unexpected", e);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   180
            }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   181
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   182
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   183
        static final byte[] requestEnd = new byte[] { '\r', '\n', '\r', '\n' };
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   184
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   185
        // Read the request method
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   186
        static URI readRequestMethod(InputStream is) throws IOException {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   187
            StringBuilder sb = new StringBuilder();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   188
            int r;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   189
            while ((r = is.read()) != -1 && r != 0x20) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   190
                sb.append((char)r);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   191
            }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   192
            return URI.create(sb.toString());
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   193
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   194
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   195
        // Read the request URI path
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   196
        static URI readRequestPath(InputStream is) throws IOException {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   197
            StringBuilder sb = new StringBuilder();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   198
            int r;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   199
            while ((r = is.read()) != -1 && r != 0x20) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   200
                sb.append((char)r);
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   201
            }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   202
            return URI.create(sb.toString());
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   203
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   204
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   205
        // Read until the end of a HTTP request headers
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   206
        static void readRequestHeaders(InputStream is) throws IOException {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   207
            int requestEndCount = 0, r;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   208
            while ((r = is.read()) != -1) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   209
                if (r == requestEnd[requestEndCount]) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   210
                    requestEndCount++;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   211
                    if (requestEndCount == 4) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   212
                        break;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   213
                    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   214
                } else {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   215
                    requestEndCount = 0;
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   216
                }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   217
            }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   218
        }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   219
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   220
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   221
    static String serverAuthority(FixedLengthServer server) {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   222
        return InetAddress.getLoopbackAddress().getHostName() + ":"
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   223
                + server.getPort();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   224
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   225
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   226
    @BeforeTest
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   227
    public void setup() throws Exception {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   228
        fixedLengthServer = new FixedLengthServer();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   229
        httpURIFixLen = "http://" + serverAuthority(fixedLengthServer)
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   230
                + "/http1/fixed/baz";
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   231
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   232
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   233
    @AfterTest
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   234
    public void teardown() throws Exception {
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   235
        fixedLengthServer.close();
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   236
    }
8e00f02b7dfc http-client-branch: fix bug in property value reading
chegar
parents:
diff changeset
   237
}