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