test/jdk/sun/net/www/http/HttpClient/RetryPost.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58009 0daf32316b47
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2006, 2019, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    24
/*
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    25
 * @test
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    26
 * @bug 6427251 6382788
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    27
 * @modules jdk.httpserver
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    28
 * @library /test/lib
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    29
 * @run main RetryPost
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    30
 * @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    31
 * @summary HttpURLConnection automatically retries non-idempotent method POST
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    32
 */
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    33
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    34
import com.sun.net.httpserver.HttpContext;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    35
import com.sun.net.httpserver.HttpExchange;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    36
import com.sun.net.httpserver.HttpHandler;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    37
import java.io.IOException;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    38
import java.io.OutputStream;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    39
import java.net.HttpURLConnection;
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    40
import java.net.InetAddress;
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    41
import java.net.InetSocketAddress;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    42
import java.net.Proxy;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    43
import java.net.SocketException;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 22625
diff changeset
    44
import java.net.URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.concurrent.Executors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.concurrent.ExecutorService;
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    47
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class RetryPost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static boolean shouldRetry = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    com.sun.net.httpserver.HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    MyHandler httpHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    ExecutorService executorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
55522
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
    57
    public static void main(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (args.length == 1 && args[0].equals("noRetry"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            shouldRetry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        new RetryPost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
55522
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
    64
    public RetryPost() throws Exception {
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
    65
        startHttpServer(shouldRetry);
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
    66
        doClient();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
55522
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
    69
    void doClient() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            InetSocketAddress address = httpServer.getAddress();
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    72
            URL url = URIBuilder.newBuilder()
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    73
                      .scheme("http")
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    74
                      .host(address.getAddress())
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    75
                      .port(address.getPort())
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    76
                      .path("/test/")
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
    77
                      .toURLUnchecked();
22625
50ee5cbd439b 7150539: HttpURLConnection.getResponseMessage() doesn't throw IOException on server error (OS X)
chegar
parents: 5506
diff changeset
    78
            HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            uc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            uc.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // if we reach here then we have failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new RuntimeException("Failed: POST request being retried");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (SocketException se) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    87
            System.out.println("Got expected exception: " + se);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // this is what we expect to happen and is OK.
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    89
            if (shouldRetry && httpHandler.getCallCount() != 2) {
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    90
                se.printStackTrace(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                throw new RuntimeException("Failed: Handler should have been called twice. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                           "It was called "+ httpHandler.getCallCount() + " times");
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    93
            } else if (!shouldRetry && httpHandler.getCallCount() != 1) {
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    94
                se.printStackTrace(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new RuntimeException("Failed: Handler should have only been called once" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                           "It was called "+ httpHandler.getCallCount() + " times");
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55522
diff changeset
    97
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            executorService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Http Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void startHttpServer(boolean shouldRetry) throws IOException {
54938
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
   108
        InetAddress loopback = InetAddress.getLoopbackAddress();
8c977741c3c8 8223856: Replace wildcard address with loopback or local host in tests - part 8
dfuchs
parents: 47216
diff changeset
   109
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(loopback, 0), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        httpHandler = new MyHandler(shouldRetry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        HttpContext ctx = httpServer.createContext("/test/", httpHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        executorService = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        httpServer.setExecutor(executorService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    class MyHandler implements HttpHandler {
55522
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
   120
        volatile int callCount = 0;
b2cbe2f674f0 8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents: 54938
diff changeset
   121
        final boolean shouldRetry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public MyHandler(boolean shouldRetry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            this.shouldRetry = shouldRetry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            callCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (callCount > 1 && !shouldRetry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // if this bug has been fixed then this method will not be called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                // when -Dhttp.retryPost=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                t.sendResponseHeaders(400, -1);  // indicate failure by returning 400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // simply close out the stream without sending any data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public int getCallCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return callCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}