49765
|
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
|
56619
|
27 |
* @build DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient
|
49765
|
28 |
* @run testng/othervm OfflineTesting
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.net.URI;
|
|
33 |
import java.net.http.HttpClient;
|
56619
|
34 |
import java.net.http.HttpHeaders;
|
49765
|
35 |
import java.net.http.HttpRequest;
|
|
36 |
import java.net.http.HttpRequest.BodyPublishers;
|
|
37 |
import java.net.http.HttpResponse;
|
|
38 |
import java.net.http.HttpResponse.BodyHandlers;
|
56619
|
39 |
import java.util.HashMap;
|
|
40 |
import java.util.List;
|
|
41 |
import java.util.Map;
|
|
42 |
import java.util.function.BiPredicate;
|
49765
|
43 |
import org.testng.annotations.Test;
|
56619
|
44 |
import static java.lang.String.format;
|
49765
|
45 |
import static java.nio.charset.StandardCharsets.UTF_8;
|
56619
|
46 |
import static java.util.Objects.requireNonNull;
|
49765
|
47 |
import static org.testng.Assert.assertEquals;
|
|
48 |
import static org.testng.Assert.assertTrue;
|
|
49 |
import static org.testng.Assert.fail;
|
|
50 |
|
|
51 |
public class OfflineTesting {
|
|
52 |
|
|
53 |
private static HttpClient getClient() {
|
|
54 |
// be sure to return the appropriate client when testing
|
|
55 |
//return HttpClient.newHttpClient();
|
|
56 |
return FixedResponseHttpClient.createClientFrom(
|
|
57 |
HttpClient.newBuilder(),
|
|
58 |
200,
|
56619
|
59 |
headersOf("Server", "nginx",
|
|
60 |
"Content-Type", "text/html"),
|
49765
|
61 |
"A response message");
|
|
62 |
}
|
|
63 |
|
|
64 |
@Test
|
|
65 |
public void testResponseAsString() {
|
|
66 |
HttpClient client = getClient();
|
|
67 |
|
|
68 |
HttpRequest request = HttpRequest.newBuilder()
|
|
69 |
.uri(URI.create("http://openjdk.java.net/"))
|
|
70 |
.build();
|
|
71 |
|
|
72 |
client.sendAsync(request, BodyHandlers.ofString())
|
|
73 |
.thenAccept(response -> {
|
|
74 |
System.out.println("response: " + response);
|
|
75 |
assertEquals(response.statusCode(), 200);
|
|
76 |
assertTrue(response.headers().firstValue("Server").isPresent());
|
|
77 |
assertEquals(response.body(), "A response message"); } )
|
|
78 |
.join();
|
|
79 |
}
|
|
80 |
|
|
81 |
@Test
|
|
82 |
public void testResponseAsByteArray() {
|
|
83 |
HttpClient client = getClient();
|
|
84 |
|
|
85 |
HttpRequest request = HttpRequest.newBuilder()
|
|
86 |
.uri(URI.create("http://openjdk.java.net/"))
|
|
87 |
.build();
|
|
88 |
|
|
89 |
client.sendAsync(request, BodyHandlers.ofByteArray())
|
|
90 |
.thenAccept(response -> {
|
|
91 |
System.out.println("response: " + response);
|
|
92 |
assertEquals(response.statusCode(), 200);
|
|
93 |
assertTrue(response.headers().firstValue("Content-Type").isPresent());
|
|
94 |
assertEquals(response.body(), "A response message".getBytes(UTF_8)); } )
|
|
95 |
.join();
|
|
96 |
}
|
|
97 |
|
|
98 |
@Test
|
|
99 |
public void testFileNotFound() {
|
|
100 |
//HttpClient client = HttpClient.newHttpClient();
|
|
101 |
HttpClient client = FixedResponseHttpClient.createClientFrom(
|
|
102 |
HttpClient.newBuilder(),
|
|
103 |
404,
|
56619
|
104 |
headersOf("Connection", "keep-alive",
|
|
105 |
"Content-Length", "162",
|
|
106 |
"Content-Type", "text/html",
|
|
107 |
"Date", "Mon, 15 Jan 2018 15:01:16 GMT",
|
|
108 |
"Server", "nginx"),
|
49765
|
109 |
"<html>\n" +
|
|
110 |
"<head><title>404 Not Found</title></head>\n" +
|
|
111 |
"<body bgcolor=\"white\">\n" +
|
|
112 |
"<center><h1>404 Not Found</h1></center>\n" +
|
|
113 |
"<hr><center>nginx</center>\n" +
|
|
114 |
"</body>\n" +
|
|
115 |
"</html>");
|
|
116 |
|
|
117 |
HttpRequest request = HttpRequest.newBuilder()
|
|
118 |
.uri(URI.create("http://openjdk.java.net/notFound"))
|
|
119 |
.build();
|
|
120 |
|
|
121 |
client.sendAsync(request, BodyHandlers.ofString())
|
|
122 |
.thenAccept(response -> {
|
|
123 |
assertEquals(response.statusCode(), 404);
|
|
124 |
response.headers().firstValue("Content-Type")
|
|
125 |
.ifPresentOrElse(type -> assertEquals(type, "text/html"),
|
|
126 |
() -> fail("Content-Type not present"));
|
|
127 |
assertTrue(response.body().contains("404 Not Found")); } )
|
|
128 |
.join();
|
|
129 |
}
|
|
130 |
|
|
131 |
@Test
|
|
132 |
public void testEcho() {
|
|
133 |
HttpClient client = FixedResponseHttpClient.createEchoClient(
|
|
134 |
HttpClient.newBuilder(),
|
|
135 |
200,
|
56619
|
136 |
headersOf("Connection", "keep-alive"));
|
49765
|
137 |
|
|
138 |
HttpRequest request = HttpRequest.newBuilder()
|
|
139 |
.uri(URI.create("http://openjdk.java.net/echo"))
|
|
140 |
.POST(BodyPublishers.ofString("Hello World"))
|
|
141 |
.build();
|
|
142 |
|
|
143 |
client.sendAsync(request, BodyHandlers.ofString())
|
|
144 |
.thenAccept(response -> {
|
|
145 |
System.out.println("response: " + response);
|
|
146 |
assertEquals(response.statusCode(), 200);
|
|
147 |
assertEquals(response.body(), "Hello World"); } )
|
|
148 |
.join();
|
|
149 |
}
|
|
150 |
|
|
151 |
@Test
|
|
152 |
public void testEchoBlocking() throws IOException, InterruptedException {
|
|
153 |
HttpClient client = FixedResponseHttpClient.createEchoClient(
|
|
154 |
HttpClient.newBuilder(),
|
|
155 |
200,
|
56619
|
156 |
headersOf("Connection", "keep-alive"));
|
49765
|
157 |
|
|
158 |
HttpRequest request = HttpRequest.newBuilder()
|
|
159 |
.uri(URI.create("http://openjdk.java.net/echo"))
|
|
160 |
.POST(BodyPublishers.ofString("Hello chegar!!"))
|
|
161 |
.build();
|
|
162 |
|
|
163 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
|
164 |
System.out.println("response: " + response);
|
|
165 |
assertEquals(response.statusCode(), 200);
|
|
166 |
assertEquals(response.body(), "Hello chegar!!");
|
|
167 |
}
|
56619
|
168 |
|
|
169 |
// ---
|
|
170 |
|
|
171 |
public static IllegalArgumentException newIAE(String message, Object... args) {
|
|
172 |
return new IllegalArgumentException(format(message, args));
|
|
173 |
}
|
|
174 |
|
|
175 |
static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true;
|
|
176 |
|
|
177 |
static HttpHeaders headersOf(String... params) {
|
|
178 |
Map<String,List<String>> map = new HashMap<>();
|
|
179 |
requireNonNull(params);
|
|
180 |
if (params.length == 0 || params.length % 2 != 0) {
|
|
181 |
throw newIAE("wrong number, %d, of parameters", params.length);
|
|
182 |
}
|
|
183 |
for (int i = 0; i < params.length; i += 2) {
|
|
184 |
String name = params[i];
|
|
185 |
String value = params[i + 1];
|
|
186 |
map.put(name, List.of(value));
|
|
187 |
}
|
|
188 |
return HttpHeaders.of(map, ACCEPT_ALL);
|
|
189 |
}
|
49765
|
190 |
}
|