|
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 /* |
|
25 * @test |
|
26 * @summary Demonstrates how to achieve testing without network connections |
|
27 * @build FixedHttpHeaders DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient |
|
28 * @run testng/othervm OfflineTesting |
|
29 */ |
|
30 |
|
31 import java.io.IOException; |
|
32 import java.net.URI; |
|
33 import jdk.incubator.http.HttpClient; |
|
34 import jdk.incubator.http.HttpRequest; |
|
35 import jdk.incubator.http.HttpResponse; |
|
36 import org.testng.annotations.Test; |
|
37 import static java.nio.charset.StandardCharsets.UTF_8; |
|
38 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString; |
|
39 import static jdk.incubator.http.HttpResponse.BodyHandler.asByteArray; |
|
40 import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
|
41 import static org.testng.Assert.assertEquals; |
|
42 import static org.testng.Assert.assertTrue; |
|
43 import static org.testng.Assert.fail; |
|
44 |
|
45 public class OfflineTesting { |
|
46 |
|
47 private static HttpClient getClient() { |
|
48 // be sure to return the appropriate client when testing |
|
49 //return HttpClient.newHttpClient(); |
|
50 return FixedResponseHttpClient.createClientFrom( |
|
51 HttpClient.newBuilder(), |
|
52 200, |
|
53 FixedHttpHeaders.of("Server", "nginx", |
|
54 "Content-Type", "text/html"), |
|
55 "A response message"); |
|
56 } |
|
57 |
|
58 @Test |
|
59 public void testResponseAsString() { |
|
60 HttpClient client = getClient(); |
|
61 |
|
62 HttpRequest request = HttpRequest.newBuilder() |
|
63 .uri(URI.create("http://openjdk.java.net/")) |
|
64 .build(); |
|
65 |
|
66 client.sendAsync(request, asString()) |
|
67 .thenAccept(response -> { |
|
68 System.out.println("response: " + response); |
|
69 assertEquals(response.statusCode(), 200); |
|
70 assertTrue(response.headers().firstValue("Server").isPresent()); |
|
71 assertEquals(response.body(), "A response message"); } ) |
|
72 .join(); |
|
73 } |
|
74 |
|
75 @Test |
|
76 public void testResponseAsByteArray() { |
|
77 HttpClient client = getClient(); |
|
78 |
|
79 HttpRequest request = HttpRequest.newBuilder() |
|
80 .uri(URI.create("http://openjdk.java.net/")) |
|
81 .build(); |
|
82 |
|
83 client.sendAsync(request, asByteArray()) |
|
84 .thenAccept(response -> { |
|
85 System.out.println("response: " + response); |
|
86 assertEquals(response.statusCode(), 200); |
|
87 assertTrue(response.headers().firstValue("Content-Type").isPresent()); |
|
88 assertEquals(response.body(), "A response message".getBytes(UTF_8)); } ) |
|
89 .join(); |
|
90 } |
|
91 |
|
92 @Test |
|
93 public void testFileNotFound() { |
|
94 //HttpClient client = HttpClient.newHttpClient(); |
|
95 HttpClient client = FixedResponseHttpClient.createClientFrom( |
|
96 HttpClient.newBuilder(), |
|
97 404, |
|
98 FixedHttpHeaders.of("Connection", "keep-alive", |
|
99 "Content-Length", "162", |
|
100 "Content-Type", "text/html", |
|
101 "Date", "Mon, 15 Jan 2018 15:01:16 GMT", |
|
102 "Server", "nginx"), |
|
103 "<html>\n" + |
|
104 "<head><title>404 Not Found</title></head>\n" + |
|
105 "<body bgcolor=\"white\">\n" + |
|
106 "<center><h1>404 Not Found</h1></center>\n" + |
|
107 "<hr><center>nginx</center>\n" + |
|
108 "</body>\n" + |
|
109 "</html>"); |
|
110 |
|
111 HttpRequest request = HttpRequest.newBuilder() |
|
112 .uri(URI.create("http://openjdk.java.net/notFound")) |
|
113 .build(); |
|
114 |
|
115 client.sendAsync(request, asString()) |
|
116 .thenAccept(response -> { |
|
117 assertEquals(response.statusCode(), 404); |
|
118 response.headers().firstValue("Content-Type") |
|
119 .ifPresentOrElse(type -> assertEquals(type, "text/html"), |
|
120 () -> fail("Content-Type not present")); |
|
121 assertTrue(response.body().contains("404 Not Found")); } ) |
|
122 .join(); |
|
123 } |
|
124 |
|
125 @Test |
|
126 public void testEcho() { |
|
127 HttpClient client = FixedResponseHttpClient.createEchoClient( |
|
128 HttpClient.newBuilder(), |
|
129 200, |
|
130 FixedHttpHeaders.of("Connection", "keep-alive")); |
|
131 |
|
132 HttpRequest request = HttpRequest.newBuilder() |
|
133 .uri(URI.create("http://openjdk.java.net/echo")) |
|
134 .POST(fromString("Hello World")) |
|
135 .build(); |
|
136 |
|
137 client.sendAsync(request, asString()) |
|
138 .thenAccept(response -> { |
|
139 System.out.println("response: " + response); |
|
140 assertEquals(response.statusCode(), 200); |
|
141 assertEquals(response.body(), "Hello World"); } ) |
|
142 .join(); |
|
143 } |
|
144 |
|
145 @Test |
|
146 public void testEchoBlocking() throws IOException, InterruptedException { |
|
147 HttpClient client = FixedResponseHttpClient.createEchoClient( |
|
148 HttpClient.newBuilder(), |
|
149 200, |
|
150 FixedHttpHeaders.of("Connection", "keep-alive")); |
|
151 |
|
152 HttpRequest request = HttpRequest.newBuilder() |
|
153 .uri(URI.create("http://openjdk.java.net/echo")) |
|
154 .POST(fromString("Hello chegar!!")) |
|
155 .build(); |
|
156 |
|
157 HttpResponse<String> response = client.send(request, asString()); |
|
158 System.out.println("response: " + response); |
|
159 assertEquals(response.statusCode(), 200); |
|
160 assertEquals(response.body(), "Hello chegar!!"); |
|
161 } |
|
162 } |