test/jdk/java/net/httpclient/RequestBodyTest.java
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 52121 934969c63223
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8087112
    26  * @bug 8087112
    27  * @modules jdk.incubator.httpclient
    27  * @modules java.net.http
    28  *          java.logging
    28  *          java.logging
    29  *          jdk.httpserver
    29  *          jdk.httpserver
    30  * @library /lib/testlibrary/ /test/lib
    30  * @library /lib/testlibrary/ /test/lib
    31  * @compile ../../../com/sun/net/httpserver/LogFilter.java
    31  * @compile ../../../com/sun/net/httpserver/LogFilter.java
    32  * @compile ../../../com/sun/net/httpserver/EchoHandler.java
    32  * @compile ../../../com/sun/net/httpserver/EchoHandler.java
    33  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
    33  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
       
    34  * @build jdk.testlibrary.SimpleSSLContext
       
    35  * @build LightWeightHttpServer
    34  * @build jdk.test.lib.Platform
    36  * @build jdk.test.lib.Platform
    35  * @build jdk.test.lib.util.FileUtils
    37  * @build jdk.test.lib.util.FileUtils
    36  * @build LightWeightHttpServer
       
    37  * @build jdk.testlibrary.SimpleSSLContext
       
    38  * @run testng/othervm RequestBodyTest
    38  * @run testng/othervm RequestBodyTest
       
    39  * @run testng/othervm/java.security.policy=RequestBodyTest.policy RequestBodyTest
    39  */
    40  */
    40 
    41 
    41 import java.io.*;
    42 import java.io.*;
    42 import java.net.URI;
    43 import java.net.URI;
    43 import jdk.incubator.http.HttpClient;
    44 import java.net.http.HttpClient;
    44 import jdk.incubator.http.HttpRequest;
    45 import java.net.http.HttpRequest;
    45 import jdk.incubator.http.HttpResponse;
    46 import java.net.http.HttpResponse;
    46 import jdk.incubator.http.HttpResponse.BodyHandler;
    47 import java.net.http.HttpRequest.BodyPublishers;
       
    48 import java.net.http.HttpResponse.BodyHandler;
       
    49 import java.net.http.HttpResponse.BodyHandlers;
    47 import java.nio.charset.Charset;
    50 import java.nio.charset.Charset;
    48 import java.nio.charset.StandardCharsets;
    51 import java.nio.charset.StandardCharsets;
    49 import java.nio.file.Files;
    52 import java.nio.file.Files;
    50 import java.nio.file.Path;
    53 import java.nio.file.Path;
    51 import java.nio.file.Paths;
    54 import java.nio.file.Paths;
       
    55 import java.security.AccessController;
       
    56 import java.security.PrivilegedActionException;
       
    57 import java.security.PrivilegedExceptionAction;
    52 import java.util.ArrayList;
    58 import java.util.ArrayList;
    53 import java.util.Arrays;
    59 import java.util.Arrays;
    54 import java.util.List;
    60 import java.util.List;
    55 import java.util.Optional;
    61 import java.util.Optional;
    56 import java.util.concurrent.ConcurrentHashMap;
    62 import java.util.concurrent.ConcurrentHashMap;
    59 import javax.net.ssl.SSLContext;
    65 import javax.net.ssl.SSLContext;
    60 import jdk.test.lib.util.FileUtils;
    66 import jdk.test.lib.util.FileUtils;
    61 import static java.lang.System.out;
    67 import static java.lang.System.out;
    62 import static java.nio.charset.StandardCharsets.*;
    68 import static java.nio.charset.StandardCharsets.*;
    63 import static java.nio.file.StandardOpenOption.*;
    69 import static java.nio.file.StandardOpenOption.*;
    64 import static jdk.incubator.http.HttpRequest.BodyPublisher.*;
       
    65 import static jdk.incubator.http.HttpResponse.BodyHandler.*;
       
    66 
    70 
    67 import org.testng.annotations.AfterTest;
    71 import org.testng.annotations.AfterTest;
    68 import org.testng.annotations.BeforeTest;
    72 import org.testng.annotations.BeforeTest;
    69 import org.testng.annotations.DataProvider;
    73 import org.testng.annotations.DataProvider;
    70 import org.testng.annotations.Test;
    74 import org.testng.annotations.Test;
   189         byte[] fileAsBytes = getFileBytes(filename);
   193         byte[] fileAsBytes = getFileBytes(filename);
   190         String fileAsString = new String(fileAsBytes, UTF_8);
   194         String fileAsString = new String(fileAsBytes, UTF_8);
   191 
   195 
   192         switch (requestBodyType) {
   196         switch (requestBodyType) {
   193             case BYTE_ARRAY:
   197             case BYTE_ARRAY:
   194                 rb.POST(fromByteArray(fileAsBytes));
   198                 rb.POST(BodyPublishers.ofByteArray(fileAsBytes));
   195                 break;
   199                 break;
   196             case BYTE_ARRAY_OFFSET:
   200             case BYTE_ARRAY_OFFSET:
   197                 rb.POST(fromByteArray(fileAsBytes,
   201                 rb.POST(BodyPublishers.ofByteArray(fileAsBytes,
   198                                       DEFAULT_OFFSET,
   202                         DEFAULT_OFFSET,
   199                                       fileAsBytes.length * DEFAULT_LENGTH_FACTOR));
   203                         fileAsBytes.length * DEFAULT_LENGTH_FACTOR));
   200                 break;
   204                 break;
   201             case BYTE_ARRAYS:
   205             case BYTE_ARRAYS:
   202                 Iterable<byte[]> iterable = Arrays.asList(fileAsBytes);
   206                 Iterable<byte[]> iterable = Arrays.asList(fileAsBytes);
   203                 rb.POST(fromByteArrays(iterable));
   207                 rb.POST(BodyPublishers.ofByteArrays(iterable));
   204                 break;
   208                 break;
   205             case FILE:
   209             case FILE:
   206                 rb.POST(fromFile(file));
   210                 rb.POST(BodyPublishers.ofFile(file));
   207                 break;
   211                 break;
   208             case INPUTSTREAM:
   212             case INPUTSTREAM:
   209                 rb.POST(fromInputStream(fileInputStreamSupplier(file)));
   213                 rb.POST(BodyPublishers.ofInputStream(fileInputStreamSupplier(file)));
   210                 break;
   214                 break;
   211             case STRING:
   215             case STRING:
   212                 rb.POST(fromString(fileAsString));
   216                 rb.POST(BodyPublishers.ofString(fileAsString));
   213                 break;
   217                 break;
   214             case STRING_WITH_CHARSET:
   218             case STRING_WITH_CHARSET:
   215                 rb.POST(fromString(new String(fileAsBytes), Charset.defaultCharset()));
   219                 rb.POST(BodyPublishers.ofString(new String(fileAsBytes), Charset.defaultCharset()));
   216                 break;
   220                 break;
   217             default:
   221             default:
   218                 throw new AssertionError("Unknown request body:" + requestBodyType);
   222                 throw new AssertionError("Unknown request body:" + requestBodyType);
   219         }
   223         }
   220         return rb.build();
   224         return rb.build();
   240         Path tempFile = Paths.get("RequestBodyTest.tmp");
   244         Path tempFile = Paths.get("RequestBodyTest.tmp");
   241         FileUtils.deleteFileIfExistsWithRetry(tempFile);
   245         FileUtils.deleteFileIfExistsWithRetry(tempFile);
   242 
   246 
   243         switch (responseBodyType) {
   247         switch (responseBodyType) {
   244             case BYTE_ARRAY:
   248             case BYTE_ARRAY:
   245                 BodyHandler<byte[]> bh = asByteArray();
   249                 BodyHandler<byte[]> bh = BodyHandlers.ofByteArray();
   246                 if (bufferResponseBody) bh = buffering(bh, 50);
   250                 if (bufferResponseBody) bh = BodyHandlers.buffering(bh, 50);
   247                 HttpResponse<byte[]> bar = getResponse(client, request, bh, async);
   251                 HttpResponse<byte[]> bar = getResponse(client, request, bh, async);
   248                 assertEquals(bar.statusCode(), 200);
   252                 assertEquals(bar.statusCode(), 200);
   249                 assertEquals(bar.body(), fileAsBytes);
   253                 assertEquals(bar.body(), fileAsBytes);
   250                 break;
   254                 break;
   251             case BYTE_ARRAY_CONSUMER:
   255             case BYTE_ARRAY_CONSUMER:
   252                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
   256                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
   253                 Consumer<Optional<byte[]>> consumer = o -> consumerBytes(o, baos);
   257                 Consumer<Optional<byte[]>> consumer = o -> consumerBytes(o, baos);
   254                 BodyHandler<Void> bh1 = asByteArrayConsumer(consumer);
   258                 BodyHandler<Void> bh1 = BodyHandlers.ofByteArrayConsumer(consumer);
   255                 if (bufferResponseBody) bh1 = buffering(bh1, 49);
   259                 if (bufferResponseBody) bh1 = BodyHandlers.buffering(bh1, 49);
   256                 HttpResponse<Void> v = getResponse(client, request, bh1, async);
   260                 HttpResponse<Void> v = getResponse(client, request, bh1, async);
   257                 byte[] ba = baos.toByteArray();
   261                 byte[] ba = baos.toByteArray();
   258                 assertEquals(v.statusCode(), 200);
   262                 assertEquals(v.statusCode(), 200);
   259                 assertEquals(ba, fileAsBytes);
   263                 assertEquals(ba, fileAsBytes);
   260                 break;
   264                 break;
   261             case DISCARD:
   265             case DISCARD:
   262                 Object o = new Object();
   266                 Object o = new Object();
   263                 BodyHandler<Object> bh2 = discard(o);
   267                 BodyHandler<Object> bh2 = BodyHandlers.replacing(o);
   264                 if (bufferResponseBody) bh2 = buffering(bh2, 51);
   268                 if (bufferResponseBody) bh2 = BodyHandlers.buffering(bh2, 51);
   265                 HttpResponse<Object> or = getResponse(client, request, bh2, async);
   269                 HttpResponse<Object> or = getResponse(client, request, bh2, async);
   266                 assertEquals(or.statusCode(), 200);
   270                 assertEquals(or.statusCode(), 200);
   267                 assertSame(or.body(), o);
   271                 assertSame(or.body(), o);
   268                 break;
   272                 break;
   269             case FILE:
   273             case FILE:
   270                 BodyHandler<Path> bh3 = asFile(tempFile);
   274                 BodyHandler<Path> bh3 = BodyHandlers.ofFile(tempFile);
   271                 if (bufferResponseBody) bh3 = buffering(bh3, 48);
   275                 if (bufferResponseBody) bh3 = BodyHandlers.buffering(bh3, 48);
   272                 HttpResponse<Path> fr = getResponse(client, request, bh3, async);
   276                 HttpResponse<Path> fr = getResponse(client, request, bh3, async);
   273                 assertEquals(fr.statusCode(), 200);
   277                 assertEquals(fr.statusCode(), 200);
   274                 assertEquals(Files.size(tempFile), fileAsString.length());
   278                 assertEquals(Files.size(tempFile), fileAsString.length());
   275                 assertEquals(Files.readAllBytes(tempFile), fileAsBytes);
   279                 assertEquals(Files.readAllBytes(tempFile), fileAsBytes);
   276                 break;
   280                 break;
   277             case FILE_WITH_OPTION:
   281             case FILE_WITH_OPTION:
   278                 BodyHandler<Path> bh4 = asFile(tempFile, CREATE_NEW, WRITE);
   282                 BodyHandler<Path> bh4 = BodyHandlers.ofFile(tempFile, CREATE_NEW, WRITE);
   279                 if (bufferResponseBody) bh4 = buffering(bh4, 52);
   283                 if (bufferResponseBody) bh4 = BodyHandlers.buffering(bh4, 52);
   280                 fr = getResponse(client, request, bh4, async);
   284                 fr = getResponse(client, request, bh4, async);
   281                 assertEquals(fr.statusCode(), 200);
   285                 assertEquals(fr.statusCode(), 200);
   282                 assertEquals(Files.size(tempFile), fileAsString.length());
   286                 assertEquals(Files.size(tempFile), fileAsString.length());
   283                 assertEquals(Files.readAllBytes(tempFile), fileAsBytes);
   287                 assertEquals(Files.readAllBytes(tempFile), fileAsBytes);
   284                 break;
   288                 break;
   285             case STRING:
   289             case STRING:
   286                 BodyHandler<String> bh5 = asString();
   290                 BodyHandler<String> bh5 = BodyHandlers.ofString();
   287                 if(bufferResponseBody) bh5 = buffering(bh5, 47);
   291                 if(bufferResponseBody) bh5 = BodyHandlers.buffering(bh5, 47);
   288                 HttpResponse<String> sr = getResponse(client, request, bh5, async);
   292                 HttpResponse<String> sr = getResponse(client, request, bh5, async);
   289                 assertEquals(sr.statusCode(), 200);
   293                 assertEquals(sr.statusCode(), 200);
   290                 assertEquals(sr.body(), fileAsString);
   294                 assertEquals(sr.body(), fileAsString);
   291                 break;
   295                 break;
   292             case STRING_WITH_CHARSET:
   296             case STRING_WITH_CHARSET:
   293                 BodyHandler<String> bh6 = asString(StandardCharsets.UTF_8);
   297                 BodyHandler<String> bh6 = BodyHandlers.ofString(StandardCharsets.UTF_8);
   294                 if (bufferResponseBody) bh6 = buffering(bh6, 53);
   298                 if (bufferResponseBody) bh6 = BodyHandlers.buffering(bh6, 53);
   295                 HttpResponse<String> r = getResponse(client, request, bh6, async);
   299                 HttpResponse<String> r = getResponse(client, request, bh6, async);
   296                 assertEquals(r.statusCode(), 200);
   300                 assertEquals(r.statusCode(), 200);
   297                 assertEquals(r.body(), fileAsString);
   301                 assertEquals(r.body(), fileAsString);
   298                 break;
   302                 break;
   299             default:
   303             default:
   326         return new Supplier<>() {
   330         return new Supplier<>() {
   327             Path file = f;
   331             Path file = f;
   328             @Override
   332             @Override
   329             public FileInputStream get() {
   333             public FileInputStream get() {
   330                 try {
   334                 try {
   331                     return new FileInputStream(file.toFile());
   335                     PrivilegedExceptionAction<FileInputStream> pa =
   332                 } catch (FileNotFoundException x) {
   336                             () -> new FileInputStream(file.toFile());
   333                     throw new UncheckedIOException(x);
   337                     return AccessController.doPrivileged(pa);
       
   338                 } catch (PrivilegedActionException x) {
       
   339                     throw new UncheckedIOException((IOException)x.getCause());
   334                 }
   340                 }
   335             }
   341             }
   336         };
   342         };
   337     }
   343     }
   338 
   344