|
1 /* |
|
2 * Copyright (c) 2018, 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 package openjdk.http.tutorial; |
|
25 |
|
26 /** |
|
27 * @author Chris Hegarty |
|
28 */ |
|
29 |
|
30 import java.io.IOException; |
|
31 import java.io.UncheckedIOException; |
|
32 import java.net.URI; |
|
33 import java.nio.file.Files; |
|
34 import java.nio.file.Path; |
|
35 import java.nio.file.Paths; |
|
36 import java.util.Arrays; |
|
37 import java.util.Map; |
|
38 import java.util.concurrent.CompletableFuture; |
|
39 import java.util.function.Function; |
|
40 |
|
41 import com.fasterxml.jackson.core.type.TypeReference; |
|
42 import com.fasterxml.jackson.databind.ObjectMapper; |
|
43 import jdk.incubator.http.HttpClient; |
|
44 import jdk.incubator.http.HttpRequest; |
|
45 import jdk.incubator.http.HttpResponse; |
|
46 import org.junit.Assert; |
|
47 import org.junit.Rule; |
|
48 import org.junit.Test; |
|
49 import org.junit.rules.TestRule; |
|
50 import org.junit.rules.TestWatcher; |
|
51 import org.junit.runner.Description; |
|
52 import openjdk.http.tutorial.exercise1.Retrievals; |
|
53 import openjdk.http.tutorial.exercise1.Retrievals.UncheckedObjectMapper; |
|
54 import static java.lang.System.out; |
|
55 import static java.nio.file.StandardOpenOption.*; |
|
56 import static java.util.stream.Collectors.joining; |
|
57 import static jdk.incubator.http.HttpClient.Version.*; |
|
58 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
|
59 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
|
60 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
|
61 import static org.junit.Assert.assertEquals; |
|
62 |
|
63 public class Exercise1Test { |
|
64 |
|
65 @Test |
|
66 public void retrieveTheStatusCode() |
|
67 throws IOException, InterruptedException |
|
68 { |
|
69 URI uri = URI.create("http://httpbin.org/get"); |
|
70 |
|
71 int expectedStatusCode = statusCode(uri); |
|
72 int actualStatusCode = Retrievals.retrieveTheStatusCode(uri); |
|
73 |
|
74 assertEquals("Unexpected status code", |
|
75 expectedStatusCode, |
|
76 actualStatusCode); |
|
77 } |
|
78 |
|
79 @Test |
|
80 public void retrieveResourceAsString() |
|
81 throws IOException, InterruptedException |
|
82 { |
|
83 URI uri = URI.create("http://httpbin.org/get"); |
|
84 |
|
85 String expectedResponseBody = bodyAsString(uri); |
|
86 String actualResponseBody = Retrievals.retrieveResourceAsString(uri); |
|
87 |
|
88 assertEquals("Unexpected response body", |
|
89 expectedResponseBody, |
|
90 actualResponseBody); |
|
91 } |
|
92 |
|
93 @Test |
|
94 public void retrieveResourceAsFile() |
|
95 throws IOException, InterruptedException |
|
96 { |
|
97 URI uri = URI.create("http://httpbin.org/get"); |
|
98 |
|
99 Path expectedResponseBodyFile = bodyAsFile(uri); |
|
100 Path actualResponseBodyFile = Retrievals.retrieveResourceAsFile(uri); |
|
101 byte[] b1 = Files.readAllBytes(expectedResponseBodyFile); |
|
102 byte[] b2 = Files.readAllBytes(actualResponseBodyFile); |
|
103 |
|
104 System.out.println("CHEGAR b1 = " + new String(b1)); |
|
105 System.out.println("CHEGAR b2 = " + new String(b2)); |
|
106 |
|
107 Assert.assertArrayEquals("Unexpected response body", b1, b2); |
|
108 } |
|
109 |
|
110 @Test |
|
111 public void retrieveResourceAsStringUsingAsyncAPI() |
|
112 throws IOException, InterruptedException |
|
113 { |
|
114 URI uri = URI.create("http://httpbin.org/get"); |
|
115 |
|
116 String expectedResponseBody = bodyAsString(uri); |
|
117 String actualResponseBody = |
|
118 Retrievals.retrieveResourceAsStringUsingAsyncAPI(uri).join(); |
|
119 |
|
120 assertEquals("Unexpected response body", |
|
121 expectedResponseBody, |
|
122 actualResponseBody); |
|
123 } |
|
124 |
|
125 // @Test |
|
126 // public void JSONBodyAsMap() { |
|
127 // throws IOException, InterruptedException |
|
128 // URI uri = URI.create("http://httpbin.org/get"); |
|
129 // |
|
130 // UncheckedObjectMapper objectMapper = new UncheckedObjectMapper(); |
|
131 // String expectedResponseBody = objectMapper(bodyAsString(uri)); |
|
132 // String actualResponseBody = |
|
133 // Retrievals.retrieveResourceAsStringUsingAsyncAPI(uri).join(); |
|
134 // |
|
135 // assertEquals("Unexpected response body", |
|
136 // expectedResponseBody, |
|
137 // actualResponseBody); |
|
138 // } |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 //@Test |
|
145 public void postData() |
|
146 throws IOException, InterruptedException |
|
147 { |
|
148 URI uri = URI.create("http://httpbin.org/post"); |
|
149 String message = "Hello there!"; |
|
150 |
|
151 String actualResponseBody = Retrievals.postData(uri, message); |
|
152 String expectedResponseBody = message; |
|
153 |
|
154 assertEquals("Unexpected response body", |
|
155 expectedResponseBody, |
|
156 actualResponseBody); |
|
157 } |
|
158 |
|
159 /** Wrapper around Jackson's ObjectMapper that provides unchecked readValue. */ |
|
160 static class UncheckedObjectMapper extends ObjectMapper{ |
|
161 |
|
162 Map<String,String> readValue(String content) { |
|
163 try { |
|
164 return this.readValue(content, new TypeReference<>(){}); |
|
165 } catch (IOException ioe) { |
|
166 throw new UncheckedIOException(ioe); |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 private UncheckedObjectMapper objectMapper = new UncheckedObjectMapper(); |
|
172 |
|
173 |
|
174 /** |
|
175 * The Echo JSON service on echo.jsontest.com returns a customized |
|
176 * JSON object that you can define through a REST-style URL. For |
|
177 * example, calling http://echo.jsontest.com/key/value/one/two |
|
178 * will return the following JSON: |
|
179 * |
|
180 * { |
|
181 * “one”: “two”, |
|
182 * “key”: “value” |
|
183 * } |
|
184 */ |
|
185 @Test |
|
186 public void bodyAsJSON() { |
|
187 String[] pairs = new String[] { |
|
188 "Name", "chegar", |
|
189 "Country", "Ireland", |
|
190 "Citizenship", "Irish" |
|
191 }; |
|
192 String path = Arrays.stream(pairs).collect(joining("/")); |
|
193 URI uri = URI.create("http://echo.jsontest.com/" + path); |
|
194 |
|
195 HttpClient client = HttpClient.newBuilder().build(); |
|
196 HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
197 client.sendAsync(request, asString()) |
|
198 .thenCompose(response -> { // maps HttpResponse to String |
|
199 assertEquals(response.statusCode(), 200); |
|
200 return CompletableFuture.completedFuture(response.body()); }) |
|
201 .thenAccept(body -> { // consumes the response body |
|
202 out.println("received: " + body); |
|
203 Map<String, String> map = objectMapper.readValue(body); |
|
204 |
|
205 assertEquals(map.get("Name"), "chegar"); |
|
206 assertEquals(map.get("Country"), "Ireland"); |
|
207 assertEquals(map.get("Citizenship"), "Irish"); }) |
|
208 .join(); |
|
209 } |
|
210 |
|
211 |
|
212 // ---- some trivial infrastructure to help output messages |
|
213 |
|
214 @Rule |
|
215 public TestRule watcher = new TestWatcher() { |
|
216 @Override |
|
217 protected void starting(Description description) { |
|
218 out.println("\nStarting test: " + description.getMethodName()); |
|
219 } |
|
220 @Override |
|
221 protected void finished(Description description) { |
|
222 out.println("Finished test: " + description.getMethodName()); |
|
223 } |
|
224 @Override |
|
225 protected void failed(Throwable e, Description description) { |
|
226 e.printStackTrace(); |
|
227 } |
|
228 }; |
|
229 |
|
230 |
|
231 |
|
232 // ---- demonstration code below |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 interface Peeker<T> extends Function<T,T> |
|
270 { |
|
271 void peek(T t); |
|
272 |
|
273 default T apply(T t) |
|
274 { |
|
275 peek(t); |
|
276 return t; |
|
277 } |
|
278 } |
|
279 |
|
280 static void assertStatusCode200(HttpResponse<?> response) { |
|
281 assertEquals(200, response.statusCode()); |
|
282 } |
|
283 |
|
284 private static int statusCode(URI uri) { |
|
285 return HttpClient.newBuilder().version(HTTP_1_1).build() |
|
286 .sendAsync(HttpRequest.newBuilder(uri).build(), discard(null)) |
|
287 .thenApply((Peeker<HttpResponse<?>>)Exercise1Test::assertStatusCode200) |
|
288 .thenApply(HttpResponse::statusCode) |
|
289 .join(); |
|
290 } |
|
291 |
|
292 private static String bodyAsString(URI uri) { |
|
293 return HttpClient.newBuilder().version(HTTP_1_1).build() |
|
294 .sendAsync(HttpRequest.newBuilder(uri).build(), asString()) |
|
295 .thenApply((Peeker<HttpResponse<String>>)Exercise1Test::assertStatusCode200) |
|
296 .thenApply(HttpResponse::body) |
|
297 .join(); |
|
298 } |
|
299 |
|
300 // private static String bodyAsString(URI uri) |
|
301 // throws IOException, InterruptedException |
|
302 // { |
|
303 // HttpClient client = HttpClient.newBuilder().build(); |
|
304 // HttpRequest request = HttpRequest.newBuilder(uri) |
|
305 // .version(HttpClient.Version.HTTP_1_1) |
|
306 // .GET() |
|
307 // .build(); |
|
308 // HttpResponse<String> response = client.send(request, asString()); |
|
309 // |
|
310 // Assert.assertEquals(200, response.statusCode()); |
|
311 // |
|
312 // return response.body(); |
|
313 // } |
|
314 |
|
315 private static Path bodyAsFile(URI uri) |
|
316 throws IOException, InterruptedException |
|
317 { |
|
318 HttpClient client = HttpClient.newBuilder().build(); |
|
319 HttpRequest request = HttpRequest.newBuilder(uri) |
|
320 .version(HttpClient.Version.HTTP_1_1) |
|
321 .GET() |
|
322 .build(); |
|
323 Path p = Paths.get("Exercise1Test_bodyAsFile.txt"); |
|
324 HttpResponse<Path> response = client.send(request, |
|
325 asFile(p, TRUNCATE_EXISTING, WRITE)); |
|
326 |
|
327 Assert.assertEquals(200, response.statusCode()); |
|
328 |
|
329 return response.body(); |
|
330 } |
|
331 |
|
332 private static String postDataGetResponseBody(URI uri, String data) |
|
333 throws IOException, InterruptedException |
|
334 { |
|
335 HttpClient client = HttpClient.newBuilder().build(); |
|
336 HttpRequest request = HttpRequest.newBuilder() |
|
337 .uri(uri) |
|
338 .version(HttpClient.Version.HTTP_1_1) |
|
339 .POST(HttpRequest.BodyProcessor.fromString(data)) |
|
340 .build(); |
|
341 HttpResponse<String> response = client.send(request, asString()); |
|
342 |
|
343 ObjectMapper objectMapper = new ObjectMapper(); |
|
344 Map<String,Object> map = objectMapper.readValue(response.body(), new TypeReference<>(){}); |
|
345 |
|
346 System.out.println("CHEGAR map: " + map); |
|
347 |
|
348 //JSONObject json = new JSONObject(myResponse); |
|
349 |
|
350 Assert.assertEquals(200, response.statusCode()); |
|
351 |
|
352 return response.body(); |
|
353 } |
|
354 } |