< prev index next >

test/jdk/java/net/httpclient/HttpRedirectTest.java

Print this page

  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 import com.sun.net.httpserver.HttpServer;
 24 import com.sun.net.httpserver.HttpsConfigurator;
 25 import com.sun.net.httpserver.HttpsServer;

 26 import jdk.test.lib.net.SimpleSSLContext;
 27 import org.testng.annotations.BeforeClass;
 28 import org.testng.annotations.AfterClass;
 29 import org.testng.annotations.DataProvider;
 30 import org.testng.annotations.Test;
 31 import static java.net.http.HttpClient.Version.HTTP_1_1;
 32 import static java.net.http.HttpClient.Version.HTTP_2;
 33 import static org.testng.Assert.*;
 34 
 35 import javax.net.ssl.SSLContext;
 36 import java.io.IOException;
 37 import java.io.InputStream;
 38 import java.io.OutputStream;
 39 import java.net.InetAddress;
 40 import java.net.InetSocketAddress;
 41 import java.net.Proxy;
 42 import java.net.ProxySelector;
 43 import java.net.SocketAddress;
 44 import java.net.URI;
 45 import java.net.URISyntaxException;
 46 import java.net.http.HttpClient;
 47 import java.net.http.HttpRequest;
 48 import java.net.http.HttpResponse;
 49 import java.nio.charset.StandardCharsets;
 50 import java.util.List;
 51 import java.util.Map;
 52 import java.util.Random;
 53 import java.util.Set;
 54 import java.util.concurrent.CompletableFuture;
 55 import java.util.concurrent.CopyOnWriteArrayList;
 56 import java.util.concurrent.CopyOnWriteArraySet;
 57 import java.util.concurrent.ExecutorService;
 58 import java.util.concurrent.LinkedBlockingQueue;
 59 import java.util.concurrent.ThreadPoolExecutor;
 60 import java.util.concurrent.TimeUnit;
 61 import java.util.concurrent.atomic.AtomicLong;
 62 import jdk.httpclient.test.lib.common.HttpServerAdapters;
 63 import jdk.httpclient.test.lib.http2.Http2TestServer;
 64 
 65 /**
 66  * @test
 67  * @bug 8232625
 68  * @summary This test verifies that the HttpClient works correctly when redirecting a post request.
 69  * @library /test/lib /test/jdk/java/net/httpclient/lib
 70  * @build jdk.test.lib.net.SimpleSSLContext DigestEchoServer HttpRedirectTest
 71  *        jdk.httpclient.test.lib.common.HttpServerAdapters

 72  * @run testng/othervm -Dtest.requiresHost=true
 73  *                   -Djdk.httpclient.HttpClient.log=headers
 74  *                   -Djdk.internal.httpclient.debug=false
 75  *                   HttpRedirectTest
 76  *
 77  */
 78 public class HttpRedirectTest implements HttpServerAdapters {
 79     static final String GET_RESPONSE_BODY = "Lorem ipsum dolor sit amet";
 80     static final String REQUEST_BODY = "Here it goes";
 81     static final SSLContext context;
 82     static {
 83         try {
 84             context = new SimpleSSLContext().get();
 85             SSLContext.setDefault(context);
 86         } catch (Exception x) {
 87             throw new ExceptionInInitializerError(x);
 88         }
 89     }
 90 
 91     final AtomicLong requestCounter = new AtomicLong();

158         }
159         return tests;
160     }
161 
162     @BeforeClass
163     public void setUp() throws Exception {
164         try {
165             InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
166 
167             // HTTP/1.1
168             http1Server = HttpTestServer.create(HTTP_1_1, null, executor);
169             http1Server.addHandler(new HttpTestRedirectHandler("http", http1Server),
170                     "/HttpRedirectTest/http1/");
171             http1Server.start();
172             http1URI = new URI("http://" + http1Server.serverAuthority() + "/HttpRedirectTest/http1/");
173 
174 
175             // HTTPS/1.1
176             HttpsServer sserver1 = HttpsServer.create(sa, 100);
177             sserver1.setExecutor(executor);
178             sserver1.setHttpsConfigurator(new HttpsConfigurator(context));
179             https1Server = HttpTestServer.of(sserver1);
180             https1Server.addHandler(new HttpTestRedirectHandler("https", https1Server),
181                     "/HttpRedirectTest/https1/");
182             https1Server.start();
183             https1URI = new URI("https://" + https1Server.serverAuthority() + "/HttpRedirectTest/https1/");
184 
185             // HTTP/2.0
186             http2Server = HttpTestServer.create(HTTP_2);
187             http2Server.addHandler(new HttpTestRedirectHandler("http", http2Server),
188                     "/HttpRedirectTest/http2/");
189             http2Server.start();
190             http2URI = new URI("http://" + http2Server.serverAuthority() + "/HttpRedirectTest/http2/");
191 
192             // HTTPS/2.0
193             https2Server = HttpTestServer.create(HTTP_2, SSLContext.getDefault());
194             https2Server.addHandler(new HttpTestRedirectHandler("https", https2Server),
195                     "/HttpRedirectTest/https2/");
196             https2Server.start();
197             https2URI = new URI("https://" + https2Server.serverAuthority() + "/HttpRedirectTest/https2/");
198 

  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */


 23 import com.sun.net.httpserver.HttpsServer;
 24 import jdk.httpclient.test.lib.common.TestServerConfigurator;
 25 import jdk.test.lib.net.SimpleSSLContext;
 26 import org.testng.annotations.BeforeClass;
 27 import org.testng.annotations.AfterClass;
 28 import org.testng.annotations.DataProvider;
 29 import org.testng.annotations.Test;
 30 import static java.net.http.HttpClient.Version.HTTP_1_1;
 31 import static java.net.http.HttpClient.Version.HTTP_2;
 32 import static org.testng.Assert.*;
 33 
 34 import javax.net.ssl.SSLContext;
 35 import java.io.IOException;
 36 import java.io.InputStream;
 37 import java.io.OutputStream;
 38 import java.net.InetAddress;
 39 import java.net.InetSocketAddress;
 40 import java.net.Proxy;
 41 import java.net.ProxySelector;
 42 import java.net.SocketAddress;
 43 import java.net.URI;
 44 import java.net.URISyntaxException;
 45 import java.net.http.HttpClient;
 46 import java.net.http.HttpRequest;
 47 import java.net.http.HttpResponse;
 48 import java.nio.charset.StandardCharsets;
 49 import java.util.List;
 50 import java.util.Map;
 51 import java.util.Random;
 52 import java.util.Set;
 53 import java.util.concurrent.CompletableFuture;
 54 import java.util.concurrent.CopyOnWriteArrayList;
 55 import java.util.concurrent.CopyOnWriteArraySet;
 56 import java.util.concurrent.ExecutorService;
 57 import java.util.concurrent.LinkedBlockingQueue;
 58 import java.util.concurrent.ThreadPoolExecutor;
 59 import java.util.concurrent.TimeUnit;
 60 import java.util.concurrent.atomic.AtomicLong;
 61 import jdk.httpclient.test.lib.common.HttpServerAdapters;

 62 
 63 /**
 64  * @test
 65  * @bug 8232625
 66  * @summary This test verifies that the HttpClient works correctly when redirecting a post request.
 67  * @library /test/lib /test/jdk/java/net/httpclient/lib
 68  * @build jdk.test.lib.net.SimpleSSLContext DigestEchoServer HttpRedirectTest
 69  *        jdk.httpclient.test.lib.common.HttpServerAdapters
 70  *        jdk.httpclient.test.lib.common.TestServerConfigurator
 71  * @run testng/othervm -Dtest.requiresHost=true
 72  *                   -Djdk.httpclient.HttpClient.log=headers
 73  *                   -Djdk.internal.httpclient.debug=false
 74  *                   HttpRedirectTest
 75  *
 76  */
 77 public class HttpRedirectTest implements HttpServerAdapters {
 78     static final String GET_RESPONSE_BODY = "Lorem ipsum dolor sit amet";
 79     static final String REQUEST_BODY = "Here it goes";
 80     static final SSLContext context;
 81     static {
 82         try {
 83             context = new SimpleSSLContext().get();
 84             SSLContext.setDefault(context);
 85         } catch (Exception x) {
 86             throw new ExceptionInInitializerError(x);
 87         }
 88     }
 89 
 90     final AtomicLong requestCounter = new AtomicLong();

157         }
158         return tests;
159     }
160 
161     @BeforeClass
162     public void setUp() throws Exception {
163         try {
164             InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
165 
166             // HTTP/1.1
167             http1Server = HttpTestServer.create(HTTP_1_1, null, executor);
168             http1Server.addHandler(new HttpTestRedirectHandler("http", http1Server),
169                     "/HttpRedirectTest/http1/");
170             http1Server.start();
171             http1URI = new URI("http://" + http1Server.serverAuthority() + "/HttpRedirectTest/http1/");
172 
173 
174             // HTTPS/1.1
175             HttpsServer sserver1 = HttpsServer.create(sa, 100);
176             sserver1.setExecutor(executor);
177             sserver1.setHttpsConfigurator(new TestServerConfigurator(sa.getAddress(), context));
178             https1Server = HttpTestServer.of(sserver1);
179             https1Server.addHandler(new HttpTestRedirectHandler("https", https1Server),
180                     "/HttpRedirectTest/https1/");
181             https1Server.start();
182             https1URI = new URI("https://" + https1Server.serverAuthority() + "/HttpRedirectTest/https1/");
183 
184             // HTTP/2.0
185             http2Server = HttpTestServer.create(HTTP_2);
186             http2Server.addHandler(new HttpTestRedirectHandler("http", http2Server),
187                     "/HttpRedirectTest/http2/");
188             http2Server.start();
189             http2URI = new URI("http://" + http2Server.serverAuthority() + "/HttpRedirectTest/http2/");
190 
191             // HTTPS/2.0
192             https2Server = HttpTestServer.create(HTTP_2, SSLContext.getDefault());
193             https2Server.addHandler(new HttpTestRedirectHandler("https", https2Server),
194                     "/HttpRedirectTest/https2/");
195             https2Server.start();
196             https2URI = new URI("https://" + https2Server.serverAuthority() + "/HttpRedirectTest/https2/");
197 
< prev index next >