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