test/jdk/java/net/httpclient/SmokeTest.java
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 55764 34d7cc00f87a
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
    30  * @library /lib/testlibrary/ /
    30  * @library /lib/testlibrary/ /
    31  * @build jdk.testlibrary.SimpleSSLContext ProxyServer
    31  * @build jdk.testlibrary.SimpleSSLContext ProxyServer
    32  * @compile ../../../com/sun/net/httpserver/LogFilter.java
    32  * @compile ../../../com/sun/net/httpserver/LogFilter.java
    33  * @compile ../../../com/sun/net/httpserver/EchoHandler.java
    33  * @compile ../../../com/sun/net/httpserver/EchoHandler.java
    34  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
    34  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
    35  * @run main/othervm -Djdk.httpclient.HttpClient.log=errors,trace SmokeTest
    35  * @run main/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=errors,trace SmokeTest
    36  */
    36  */
    37 
    37 
    38 import com.sun.net.httpserver.Headers;
    38 import com.sun.net.httpserver.Headers;
    39 import com.sun.net.httpserver.HttpContext;
    39 import com.sun.net.httpserver.HttpContext;
    40 import com.sun.net.httpserver.HttpExchange;
    40 import com.sun.net.httpserver.HttpExchange;
    45 import com.sun.net.httpserver.HttpsServer;
    45 import com.sun.net.httpserver.HttpsServer;
    46 import java.util.concurrent.atomic.AtomicInteger;
    46 import java.util.concurrent.atomic.AtomicInteger;
    47 import java.net.InetSocketAddress;
    47 import java.net.InetSocketAddress;
    48 import java.net.PasswordAuthentication;
    48 import java.net.PasswordAuthentication;
    49 import java.net.ProxySelector;
    49 import java.net.ProxySelector;
    50 import java.net.ServerSocket;
       
    51 import java.net.Socket;
       
    52 import java.net.URI;
    50 import java.net.URI;
    53 import jdk.incubator.http.HttpClient;
    51 import jdk.incubator.http.HttpClient;
    54 import jdk.incubator.http.HttpRequest;
    52 import jdk.incubator.http.HttpRequest;
    55 import jdk.incubator.http.HttpResponse;
    53 import jdk.incubator.http.HttpResponse;
    56 import java.nio.file.StandardOpenOption;
    54 import java.nio.file.StandardOpenOption;
    79 import java.util.HashSet;
    77 import java.util.HashSet;
    80 import java.util.LinkedList;
    78 import java.util.LinkedList;
    81 import java.util.List;
    79 import java.util.List;
    82 import java.util.Random;
    80 import java.util.Random;
    83 import jdk.testlibrary.SimpleSSLContext;
    81 import jdk.testlibrary.SimpleSSLContext;
    84 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile;
    82 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromFile;
    85 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromInputStream;
    83 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromInputStream;
    86 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
    84 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
    87 import static jdk.incubator.http.HttpResponse.*;
    85 import static jdk.incubator.http.HttpResponse.*;
    88 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile;
    86 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile;
    89 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
    87 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
    90 import java.util.concurrent.CountDownLatch;
    88 import java.util.concurrent.CountDownLatch;
    91 import java.util.logging.ConsoleHandler;
    89 import java.util.logging.ConsoleHandler;
   170                            .sslParameters(sslparams)
   168                            .sslParameters(sslparams)
   171                            .followRedirects(HttpClient.Redirect.ALWAYS)
   169                            .followRedirects(HttpClient.Redirect.ALWAYS)
   172                            .build();
   170                            .build();
   173 
   171 
   174         try {
   172         try {
   175 
       
   176             test1(httproot + "files/foo.txt", true);
   173             test1(httproot + "files/foo.txt", true);
   177             test1(httproot + "files/foo.txt", false);
   174             test1(httproot + "files/foo.txt", false);
   178             test1(httpsroot + "files/foo.txt", true);
   175             test1(httpsroot + "files/foo.txt", true);
   179             test1(httpsroot + "files/foo.txt", false);
   176             test1(httpsroot + "files/foo.txt", false);
   180 
   177 
   253 
   250 
   254         HttpResponse<String> response = client.send(request, asString());
   251         HttpResponse<String> response = client.send(request, asString());
   255 
   252 
   256         String body = response.body();
   253         String body = response.body();
   257         if (!body.equals("This is foo.txt\r\n")) {
   254         if (!body.equals("This is foo.txt\r\n")) {
   258             throw new RuntimeException();
   255             throw new RuntimeException("Did not get expected body: "
       
   256                 + "\n\t expected \"This is foo.txt\\r\\n\""
       
   257                 + "\n\t received \""
       
   258                 + body.replace("\r", "\\r").replace("\n","\\n") + "\"");
   259         }
   259         }
   260 
   260 
   261         // repeat async
   261         // repeat async
   262         HttpResponse<String> response1 = client.sendAsync(request, asString())
   262         HttpResponse<String> response1 = client.sendAsync(request, asString())
   263                                                .join();
   263                                                .join();
   294 
   294 
   295     // POST use echo to check reply
   295     // POST use echo to check reply
   296     static void test2a(String s) throws Exception {
   296     static void test2a(String s) throws Exception {
   297         System.out.print("test2a: " + s);
   297         System.out.print("test2a: " + s);
   298         URI uri = new URI(s);
   298         URI uri = new URI(s);
   299         Path p = Util.getTempFile(128 * 1024);
   299         Path p = getTempFile(128 * 1024);
   300         //Path p = Util.getTempFile(1 * 1024);
       
   301 
   300 
   302         HttpRequest request = HttpRequest.newBuilder(uri)
   301         HttpRequest request = HttpRequest.newBuilder(uri)
   303                                          .POST(fromFile(p))
   302                                          .POST(fromFile(p))
   304                                          .build();
   303                                          .build();
   305 
   304 
   306         Path resp = Util.getTempFile(1); // will be overwritten
   305         Path resp = getTempFile(1); // will be overwritten
   307 
   306 
   308         HttpResponse<Path> response =
   307         HttpResponse<Path> response =
   309                 client.send(request,
   308                 client.send(request,
   310                             BodyHandler.asFile(resp,
   309                             BodyHandler.asFile(resp,
   311                                                StandardOpenOption.TRUNCATE_EXISTING,
   310                                                StandardOpenOption.TRUNCATE_EXISTING,
   463     }
   462     }
   464 
   463 
   465     @SuppressWarnings("rawtypes")
   464     @SuppressWarnings("rawtypes")
   466     static void test7(String target) throws Exception {
   465     static void test7(String target) throws Exception {
   467         System.out.print("test7: " + target);
   466         System.out.print("test7: " + target);
   468         Path requestBody = Util.getTempFile(128 * 1024);
   467         Path requestBody = getTempFile(128 * 1024);
   469         // First test
   468         // First test
   470         URI uri = new URI(target);
   469         URI uri = new URI(target);
   471         HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build();
   470         HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build();
   472 
   471 
   473         for (int i=0; i<4; i++) {
   472         for (int i=0; i<4; i++) {
   642         ConsoleHandler ch = new ConsoleHandler();
   641         ConsoleHandler ch = new ConsoleHandler();
   643         logger.setLevel(Level.SEVERE);
   642         logger.setLevel(Level.SEVERE);
   644         ch.setLevel(Level.SEVERE);
   643         ch.setLevel(Level.SEVERE);
   645         logger.addHandler(ch);
   644         logger.addHandler(ch);
   646 
   645 
   647         String root = System.getProperty ("test.src")+ "/docs";
   646         String root = System.getProperty ("test.src", ".")+ "/docs";
   648         InetSocketAddress addr = new InetSocketAddress (0);
   647         InetSocketAddress addr = new InetSocketAddress (0);
   649         s1 = HttpServer.create (addr, 0);
   648         s1 = HttpServer.create (addr, 0);
   650         if (s1 instanceof HttpsServer) {
   649         if (s1 instanceof HttpsServer) {
   651             throw new RuntimeException ("should not be httpsserver");
   650             throw new RuntimeException ("should not be httpsserver");
   652         }
   651         }
   688 
   687 
   689         proxy = new ProxyServer(0, false);
   688         proxy = new ProxyServer(0, false);
   690         proxyPort = proxy.getPort();
   689         proxyPort = proxy.getPort();
   691         System.out.println("Proxy port = " + proxyPort);
   690         System.out.println("Proxy port = " + proxyPort);
   692     }
   691     }
   693 }
   692 
   694 
   693     static class RedirectHandler implements HttpHandler {
   695 class Configurator extends HttpsConfigurator {
   694         private final String root;
   696     public Configurator(SSLContext ctx) {
   695         private volatile int count = 0;
   697         super(ctx);
   696 
   698     }
   697         RedirectHandler(String root) {
   699 
   698             this.root = root;
   700     public void configure (HttpsParameters params) {
   699         }
   701         params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
   700 
   702     }
   701         @Override
   703 }
   702         public synchronized void handle(HttpExchange t) throws IOException {
   704 
   703             byte[] buf = new byte[2048];
   705 class UploadServer extends Thread {
   704             try (InputStream is = t.getRequestBody()) {
   706     int statusCode;
   705                 while (is.read(buf) != -1) ;
   707     ServerSocket ss;
   706             }
   708     int port;
   707 
   709     int size;
   708             Headers responseHeaders = t.getResponseHeaders();
   710     Object lock;
   709 
   711     boolean failed = false;
   710             if (count++ < 1) {
   712 
   711                 responseHeaders.add("Location", root + "/foo/" + count);
   713     UploadServer(int size) throws IOException {
   712             } else {
   714         this.statusCode = statusCode;
   713                 responseHeaders.add("Location", SmokeTest.midSizedFilename);
   715         this.size = size;
   714             }
   716         ss = new ServerSocket(0);
   715             t.sendResponseHeaders(301, -1);
   717         port = ss.getLocalPort();
   716             t.close();
   718         lock = new Object();
   717         }
   719     }
   718 
   720 
   719         int count() {
   721     int port() {
   720             return count;
   722           return port;
   721         }
   723     }
   722 
   724 
   723         void reset() {
   725     int size() {
   724             count = 0;
   726           return size;
   725         }
   727     }
   726     }
   728 
   727 
   729     // wait a sec before calling this
   728     static class RedirectErrorHandler implements HttpHandler {
   730     boolean failed() {
   729         private final String root;
   731         synchronized(lock) {
   730         private volatile int count = 1;
   732             return failed;
   731 
   733         }
   732         RedirectErrorHandler(String root) {
   734     }
   733             this.root = root;
   735 
   734         }
   736     @Override
   735 
   737     public void run () {
   736         synchronized int count() {
   738         int nbytes = 0;
   737             return count;
   739         Socket s = null;
   738         }
   740 
   739 
   741         synchronized(lock) {
   740         synchronized void increment() {
       
   741             count++;
       
   742         }
       
   743 
       
   744         @Override
       
   745         public synchronized void handle(HttpExchange t) throws IOException {
       
   746             try (InputStream is = t.getRequestBody()) {
       
   747                 is.readAllBytes();
       
   748             }
       
   749 
       
   750             Headers map = t.getResponseHeaders();
       
   751             String redirect = root + "/foo/" + Integer.toString(count);
       
   752             increment();
       
   753             map.add("Location", redirect);
       
   754             t.sendResponseHeaders(301, -1);
       
   755             t.close();
       
   756         }
       
   757     }
       
   758 
       
   759     static class DelayHandler implements HttpHandler {
       
   760 
       
   761         CyclicBarrier bar1 = new CyclicBarrier(2);
       
   762         CyclicBarrier bar2 = new CyclicBarrier(2);
       
   763         CyclicBarrier bar3 = new CyclicBarrier(2);
       
   764 
       
   765         CyclicBarrier barrier1() {
       
   766             return bar1;
       
   767         }
       
   768 
       
   769         CyclicBarrier barrier2() {
       
   770             return bar2;
       
   771         }
       
   772 
       
   773         @Override
       
   774         public synchronized void handle(HttpExchange he) throws IOException {
       
   775             he.getRequestBody().readAllBytes();
   742             try {
   776             try {
   743                 s = ss.accept();
   777                 bar1.await();
   744 
   778                 bar2.await();
   745                 InputStream is = s.getInputStream();
   779             } catch (Exception e) { }
   746                 OutputStream os = s.getOutputStream();
   780             he.sendResponseHeaders(200, -1); // will probably fail
   747                 os.write("HTTP/1.1 201 OK\r\nContent-length: 0\r\n\r\n".getBytes());
   781             he.close();
   748                 int n;
   782         }
   749                 byte[] buf = new byte[8000];
   783     }
   750                 while ((n=is.read(buf)) != -1) {
   784 
   751                     nbytes += n;
   785     static class Configurator extends HttpsConfigurator {
   752                 }
   786         public Configurator(SSLContext ctx) {
   753             } catch (IOException e) {
   787             super(ctx);
   754                 System.out.println ("read " + nbytes);
   788         }
   755                 System.out.println ("size " + size);
   789 
   756                 failed = nbytes >= size;
   790         public void configure (HttpsParameters params) {
   757             } finally {
   791             params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
   758                 try {
       
   759                     ss.close();
       
   760                     if (s != null)
       
   761                         s.close();
       
   762                 } catch (IOException e) {}
       
   763             }
       
   764         }
       
   765     }
       
   766 }
       
   767 
       
   768 class RedirectHandler implements HttpHandler {
       
   769     String root;
       
   770     volatile int count = 0;
       
   771 
       
   772     RedirectHandler(String root) {
       
   773         this.root = root;
       
   774     }
       
   775 
       
   776     @Override
       
   777     public synchronized void handle(HttpExchange t)
       
   778         throws IOException
       
   779     {
       
   780         byte[] buf = new byte[2048];
       
   781         try (InputStream is = t.getRequestBody()) {
       
   782             while (is.read(buf) != -1) ;
       
   783         }
       
   784 
       
   785         Headers responseHeaders = t.getResponseHeaders();
       
   786 
       
   787         if (count++ < 1) {
       
   788             responseHeaders.add("Location", root + "/foo/" + count);
       
   789         } else {
       
   790             responseHeaders.add("Location", SmokeTest.midSizedFilename);
       
   791         }
       
   792         t.sendResponseHeaders(301, -1);
       
   793         t.close();
       
   794     }
       
   795 
       
   796     int count() {
       
   797         return count;
       
   798     }
       
   799 
       
   800     void reset() {
       
   801         count = 0;
       
   802     }
       
   803 }
       
   804 
       
   805 class RedirectErrorHandler implements HttpHandler {
       
   806     String root;
       
   807     volatile int count = 1;
       
   808 
       
   809     RedirectErrorHandler(String root) {
       
   810         this.root = root;
       
   811     }
       
   812 
       
   813     synchronized int count() {
       
   814         return count;
       
   815     }
       
   816 
       
   817     synchronized void increment() {
       
   818         count++;
       
   819     }
       
   820 
       
   821     @Override
       
   822     public synchronized void handle (HttpExchange t)
       
   823         throws IOException
       
   824     {
       
   825         byte[] buf = new byte[2048];
       
   826         try (InputStream is = t.getRequestBody()) {
       
   827             while (is.read(buf) != -1) ;
       
   828         }
       
   829 
       
   830         Headers map = t.getResponseHeaders();
       
   831         String redirect = root + "/foo/" + Integer.toString(count);
       
   832         increment();
       
   833         map.add("Location", redirect);
       
   834         t.sendResponseHeaders(301, -1);
       
   835         t.close();
       
   836     }
       
   837 }
       
   838 
       
   839 class Util {
       
   840     static byte[] readAll(InputStream is) throws IOException {
       
   841         byte[] buf = new byte[1024];
       
   842         byte[] result = new byte[0];
       
   843 
       
   844         while (true) {
       
   845             int n = is.read(buf);
       
   846             if (n > 0) {
       
   847                 byte[] b1 = new byte[result.length + n];
       
   848                 System.arraycopy(result, 0, b1, 0, result.length);
       
   849                 System.arraycopy(buf, 0, b1, result.length, n);
       
   850                 result = b1;
       
   851             } else if (n == -1) {
       
   852                 return result;
       
   853             }
       
   854         }
   792         }
   855     }
   793     }
   856 
   794 
   857     static Path getTempFile(int size) throws IOException {
   795     static Path getTempFile(int size) throws IOException {
   858         File f = File.createTempFile("test", "txt");
   796         File f = File.createTempFile("test", "txt");
   859         f.deleteOnExit();
   797         f.deleteOnExit();
   860         byte[] buf = new byte[2048];
   798         byte[] buf = new byte[2048];
   861         for (int i=0; i<buf.length; i++)
   799         for (int i = 0; i < buf.length; i++)
   862             buf[i] = (byte)i;
   800             buf[i] = (byte) i;
   863 
   801 
   864         FileOutputStream fos = new FileOutputStream(f);
   802         FileOutputStream fos = new FileOutputStream(f);
   865         while (size > 0) {
   803         while (size > 0) {
   866             int amount = Math.min(size, buf.length);
   804             int amount = Math.min(size, buf.length);
   867             fos.write(buf, 0, amount);
   805             fos.write(buf, 0, amount);
   868             size -= amount;
   806             size -= amount;
   869         }
   807         }
   870         fos.close();
   808         fos.close();
   871         return f.toPath();
   809         return f.toPath();
   872     }
   810     }
   873 }
       
   874 
       
   875 class DelayHandler implements HttpHandler {
       
   876 
       
   877     CyclicBarrier bar1 = new CyclicBarrier(2);
       
   878     CyclicBarrier bar2 = new CyclicBarrier(2);
       
   879     CyclicBarrier bar3 = new CyclicBarrier(2);
       
   880 
       
   881     CyclicBarrier barrier1() {
       
   882         return bar1;
       
   883     }
       
   884 
       
   885     CyclicBarrier barrier2() {
       
   886         return bar2;
       
   887     }
       
   888 
       
   889     @Override
       
   890     public synchronized void handle(HttpExchange he) throws IOException {
       
   891         byte[] buf = Util.readAll(he.getRequestBody());
       
   892         try {
       
   893             bar1.await();
       
   894             bar2.await();
       
   895         } catch (Exception e) {}
       
   896         he.sendResponseHeaders(200, -1); // will probably fail
       
   897         he.close();
       
   898     }
       
   899 
       
   900 }
   811 }
   901 
   812 
   902 // check for simple hardcoded sequence and use remote address
   813 // check for simple hardcoded sequence and use remote address
   903 // to check.
   814 // to check.
   904 // First 4 requests executed in sequence (should use same connection/address)
   815 // First 4 requests executed in sequence (should use same connection/address)