< prev index next >
   1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   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 
  24 /**
  25  * @test @bug 8087112
  26  * @library /lib/testlibrary/ /
  27  * @compile ../../../com/sun/net/httpserver/LogFilter.java
  28  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
  29  * @build LightWeightHttpServer
  30  * @build jdk.testlibrary.SimpleSSLContext ProxyServer
  31  * @run main/othervm RequestBodyTest
  32  */
  33 
  34 import java.io.IOException;
  35 import java.net.URI;
  36 import java.net.URISyntaxException;
  37 import java.net.http.HttpClient;
  38 import java.net.http.HttpRequest;
  39 import java.net.http.HttpResponse;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.concurrent.Executors;
  43 import javax.net.ssl.SSLContext;
  44 
  45 public class RequestBodyTest {
  46 
  47     final static String STRING = "string";
  48     final static String BYTE_ARRAY = "byteArray";
  49     final static String BYTE_ARRAYS = "byteArrays";
  50     final static String BYTE_ARRAY_OFFSET = "byteArray_offset";
  51     final static String FILE = "file";
  52     final static String STRING_CHARSET = "string_charset";
  53     final static String INPUTSTREAM = "InputStream";
  54 
  55     final static String midSizedFilename = "/files/notsobigfile.txt";
  56     final static String smallFilename = "/files/smallfile.txt";
  57     static Path midSizedFile;
  58     static Path smallFile;
  59     static String fileroot;
  60     static HttpClient client;
  61     static SSLContext ctx;
  62     static String httproot;
  63     static String httpsroot;
  64 
  65     public static void main(String args[]) throws Exception {
  66         fileroot = System.getProperty("test.src") + "/docs";
  67         midSizedFile = Paths.get(fileroot + midSizedFilename);
  68         smallFile = Paths.get(fileroot + smallFilename);
  69         //start the server
  70         LightWeightHttpServer.initServer();
  71 
  72         httproot = LightWeightHttpServer.httproot;
  73         httpsroot = LightWeightHttpServer.httpsroot;
  74         ctx = LightWeightHttpServer.ctx;
  75         client = HttpClient.create().sslContext(ctx)
  76                 .followRedirects(HttpClient.Redirect.ALWAYS)
  77                 .executorService(Executors.newCachedThreadPool())
  78                 .build();
  79 
  80         String TARGET = httproot + "echo/foo";
  81         boolean isSync = false;
  82         requestBodyTypes(TARGET, STRING, STRING, isSync);
  83         requestBodyTypes(TARGET, STRING, BYTE_ARRAY, isSync);
  84         requestBodyTypes(TARGET, STRING, BYTE_ARRAYS, isSync);
  85         requestBodyTypes(TARGET, STRING, INPUTSTREAM, isSync);
  86         requestBodyTypes(TARGET, STRING, FILE, isSync);
  87 
  88         requestBodyTypes(TARGET, BYTE_ARRAY, STRING, isSync);
  89         requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAY, isSync);
  90         requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAYS, isSync);
  91         requestBodyTypes(TARGET, BYTE_ARRAY, INPUTSTREAM, isSync);
  92         requestBodyTypes(TARGET, BYTE_ARRAY, FILE, isSync);
  93 
  94         requestBodyTypes(TARGET, BYTE_ARRAYS, STRING, isSync);
  95         requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAY, isSync);
  96         requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAYS, isSync);
  97         requestBodyTypes(TARGET, BYTE_ARRAYS, INPUTSTREAM, isSync);
  98         requestBodyTypes(TARGET, BYTE_ARRAYS, FILE, isSync);
  99 
 100         requestBodyTypes(TARGET, INPUTSTREAM, STRING, isSync);
 101         requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAY, isSync);
 102         requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAYS, isSync);
 103         requestBodyTypes(TARGET, INPUTSTREAM, INPUTSTREAM, isSync);
 104         requestBodyTypes(TARGET, INPUTSTREAM, FILE, isSync);
 105 
 106         requestBodyTypes(TARGET, FILE, STRING, isSync);
 107         requestBodyTypes(TARGET, FILE, BYTE_ARRAY, isSync);
 108         requestBodyTypes(TARGET, FILE, BYTE_ARRAYS, isSync);
 109         requestBodyTypes(TARGET, FILE, INPUTSTREAM, isSync);
 110         requestBodyTypes(TARGET, FILE, FILE, isSync);
 111 
 112         isSync = true;
 113         requestBodyTypes(TARGET, STRING, STRING, isSync);
 114         requestBodyTypes(TARGET, STRING, BYTE_ARRAY, isSync);
 115         requestBodyTypes(TARGET, STRING, BYTE_ARRAYS, isSync);
 116         requestBodyTypes(TARGET, STRING, INPUTSTREAM, isSync);
 117         requestBodyTypes(TARGET, STRING, FILE, isSync);
 118 
 119         requestBodyTypes(TARGET, BYTE_ARRAY, STRING, isSync);
 120         requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAY, isSync);
 121         requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAYS, isSync);
 122         requestBodyTypes(TARGET, BYTE_ARRAY, INPUTSTREAM, isSync);
 123         requestBodyTypes(TARGET, BYTE_ARRAY, FILE, isSync);
 124 
 125         requestBodyTypes(TARGET, BYTE_ARRAYS, STRING, isSync);
 126         requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAY, isSync);
 127         requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAYS, isSync);
 128         requestBodyTypes(TARGET, BYTE_ARRAYS, INPUTSTREAM, isSync);
 129         requestBodyTypes(TARGET, BYTE_ARRAYS, FILE, isSync);
 130 
 131         requestBodyTypes(TARGET, INPUTSTREAM, STRING, isSync);
 132         requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAY, isSync);
 133         requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAYS, isSync);
 134         requestBodyTypes(TARGET, INPUTSTREAM, INPUTSTREAM, isSync);
 135         requestBodyTypes(TARGET, INPUTSTREAM, FILE, isSync);
 136 
 137         requestBodyTypes(TARGET, FILE, STRING, isSync);
 138         requestBodyTypes(TARGET, FILE, BYTE_ARRAY, isSync);
 139         requestBodyTypes(TARGET, FILE, BYTE_ARRAYS, isSync);
 140         requestBodyTypes(TARGET, FILE, INPUTSTREAM, isSync);
 141         requestBodyTypes(TARGET, FILE, FILE, isSync);
 142 
 143     }
 144 
 145     static void requestBodyTypes(final String target,
 146                                  final String requestType,
 147                                  final String responseType,
 148                                  final boolean isAsync)
 149         throws Exception
 150     {
 151         System.out.println("Running test_request_body_type " + requestType +
 152                 " and response type " + responseType + " and sync=" + isAsync);
 153         URI uri = new URI(target);
 154         byte buf[];
 155         String filename = smallFile.toFile().getAbsolutePath();
 156         String fileContents = HttpUtils.getFileContent(filename);
 157         buf = fileContents.getBytes();
 158         HttpRequest.Builder builder = HttpUtils.getHttpRequestBuilder(client,
 159                                                                       requestType,
 160                                                                       uri);
 161         HttpResponse response;
 162         if (!isAsync) {
 163             response = builder.GET().response();
 164         } else {
 165             response = builder.GET().responseAsync().join();
 166         }
 167         HttpUtils.checkResponse(response, requestType, responseType);
 168         System.out.println("OK");
 169     }
 170 }
< prev index next >