test/jdk/java/net/httpclient/RequestBodyTest.java
branchhttp-client-branch
changeset 55766 2136ad3694e4
parent 55763 634d8e14c172
child 55973 4d9b002587db
equal deleted inserted replaced
55765:c56b15998860 55766:2136ad3694e4
   174             throw x;
   174             throw x;
   175         }
   175         }
   176     }
   176     }
   177 
   177 
   178     static final int DEFAULT_OFFSET = 10;
   178     static final int DEFAULT_OFFSET = 10;
   179     static final int DEFAULT_LENGTH = 1000;
   179     static final int DEFAULT_LENGTH_FACTOR = 4/5;
   180 
   180 
   181     HttpRequest createRequest(URI uri,
   181     HttpRequest createRequest(URI uri,
   182                               RequestBody requestBodyType,
   182                               RequestBody requestBodyType,
   183                               Path file)
   183                               Path file)
   184         throws IOException
   184         throws IOException
   192         switch (requestBodyType) {
   192         switch (requestBodyType) {
   193             case BYTE_ARRAY:
   193             case BYTE_ARRAY:
   194                 rb.POST(fromByteArray(fileAsBytes));
   194                 rb.POST(fromByteArray(fileAsBytes));
   195                 break;
   195                 break;
   196             case BYTE_ARRAY_OFFSET:
   196             case BYTE_ARRAY_OFFSET:
   197                 rb.POST(fromByteArray(fileAsBytes, DEFAULT_OFFSET, DEFAULT_LENGTH));
   197                 rb.POST(fromByteArray(fileAsBytes,
       
   198                                       DEFAULT_OFFSET,
       
   199                                       fileAsBytes.length * DEFAULT_LENGTH_FACTOR));
   198                 break;
   200                 break;
   199             case BYTE_ARRAYS:
   201             case BYTE_ARRAYS:
   200                 Iterable<byte[]> iterable = Arrays.asList(fileAsBytes);
   202                 Iterable<byte[]> iterable = Arrays.asList(fileAsBytes);
   201                 rb.POST(fromByteArrays(iterable));
   203                 rb.POST(fromByteArrays(iterable));
   202                 break;
   204                 break;
   229     {
   231     {
   230         String filename = file.toFile().getAbsolutePath();
   232         String filename = file.toFile().getAbsolutePath();
   231         byte[] fileAsBytes = getFileBytes(filename);
   233         byte[] fileAsBytes = getFileBytes(filename);
   232         if (requestBodyType == RequestBody.BYTE_ARRAY_OFFSET) {
   234         if (requestBodyType == RequestBody.BYTE_ARRAY_OFFSET) {
   233             // Truncate the expected response body, if only a portion was sent
   235             // Truncate the expected response body, if only a portion was sent
   234             fileAsBytes = Arrays.copyOfRange(fileAsBytes,
   236             int length = DEFAULT_OFFSET + (fileAsBytes.length * DEFAULT_LENGTH_FACTOR);
   235                                              DEFAULT_OFFSET,
   237             fileAsBytes = Arrays.copyOfRange(fileAsBytes, DEFAULT_OFFSET, length);
   236                                              DEFAULT_OFFSET + DEFAULT_LENGTH);
       
   237         }
   238         }
   238         String fileAsString = new String(fileAsBytes, UTF_8);
   239         String fileAsString = new String(fileAsBytes, UTF_8);
   239         Path tempFile = Paths.get("RequestBodyTest.tmp");
   240         Path tempFile = Paths.get("RequestBodyTest.tmp");
   240         FileUtils.deleteFileIfExistsWithRetry(tempFile);
   241         FileUtils.deleteFileIfExistsWithRetry(tempFile);
   241 
   242