1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Demonstrates how to achieve testing without network connections
* @build DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient
* @run testng/othervm OfflineTesting
*/
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import org.testng.annotations.Test;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
public class OfflineTesting {
private static HttpClient getClient() {
// be sure to return the appropriate client when testing
//return HttpClient.newHttpClient();
return FixedResponseHttpClient.createClientFrom(
HttpClient.newBuilder(),
200,
headersOf("Server", "nginx",
"Content-Type", "text/html"),
"A response message");
}
@Test
public void testResponseAsString() {
HttpClient client = getClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenAccept(response -> {
System.out.println("response: " + response);
assertEquals(response.statusCode(), 200);
assertTrue(response.headers().firstValue("Server").isPresent());
assertEquals(response.body(), "A response message"); } )
.join();
}
@Test
public void testResponseAsByteArray() {
HttpClient client = getClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/"))
.build();
client.sendAsync(request, BodyHandlers.ofByteArray())
.thenAccept(response -> {
System.out.println("response: " + response);
assertEquals(response.statusCode(), 200);
assertTrue(response.headers().firstValue("Content-Type").isPresent());
assertEquals(response.body(), "A response message".getBytes(UTF_8)); } )
.join();
}
@Test
public void testFileNotFound() {
//HttpClient client = HttpClient.newHttpClient();
HttpClient client = FixedResponseHttpClient.createClientFrom(
HttpClient.newBuilder(),
404,
headersOf("Connection", "keep-alive",
"Content-Length", "162",
"Content-Type", "text/html",
"Date", "Mon, 15 Jan 2018 15:01:16 GMT",
"Server", "nginx"),
"<html>\n" +
"<head><title>404 Not Found</title></head>\n" +
"<body bgcolor=\"white\">\n" +
"<center><h1>404 Not Found</h1></center>\n" +
"<hr><center>nginx</center>\n" +
"</body>\n" +
"</html>");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/notFound"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenAccept(response -> {
assertEquals(response.statusCode(), 404);
response.headers().firstValue("Content-Type")
.ifPresentOrElse(type -> assertEquals(type, "text/html"),
() -> fail("Content-Type not present"));
assertTrue(response.body().contains("404 Not Found")); } )
.join();
}
@Test
public void testEcho() {
HttpClient client = FixedResponseHttpClient.createEchoClient(
HttpClient.newBuilder(),
200,
headersOf("Connection", "keep-alive"));
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/echo"))
.POST(BodyPublishers.ofString("Hello World"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenAccept(response -> {
System.out.println("response: " + response);
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), "Hello World"); } )
.join();
}
@Test
public void testEchoBlocking() throws IOException, InterruptedException {
HttpClient client = FixedResponseHttpClient.createEchoClient(
HttpClient.newBuilder(),
200,
headersOf("Connection", "keep-alive"));
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/echo"))
.POST(BodyPublishers.ofString("Hello chegar!!"))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println("response: " + response);
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), "Hello chegar!!");
}
// ---
public static IllegalArgumentException newIAE(String message, Object... args) {
return new IllegalArgumentException(format(message, args));
}
static final BiPredicate<String,String> ACCEPT_ALL = (x, y) -> true;
static HttpHeaders headersOf(String... params) {
Map<String,List<String>> map = new HashMap<>();
requireNonNull(params);
if (params.length == 0 || params.length % 2 != 0) {
throw newIAE("wrong number, %d, of parameters", params.length);
}
for (int i = 0; i < params.length; i += 2) {
String name = params[i];
String value = params[i + 1];
map.put(name, List.of(value));
}
return HttpHeaders.of(map, ACCEPT_ALL);
}
}
|