test/jdk/java/net/httpclient/offline/OfflineTesting.java
branchhttp-client-branch
changeset 56619 57f17e890a40
parent 56451 9585061fdb04
child 56795 03ece2518428
equal deleted inserted replaced
56618:e4022357f852 56619:57f17e890a40
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @summary Demonstrates how to achieve testing without network connections
    26  * @summary Demonstrates how to achieve testing without network connections
    27  * @build FixedHttpHeaders DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient
    27  * @build DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient
    28  * @run testng/othervm OfflineTesting
    28  * @run testng/othervm OfflineTesting
    29  */
    29  */
    30 
    30 
    31 import java.io.IOException;
    31 import java.io.IOException;
    32 import java.net.URI;
    32 import java.net.URI;
    33 import java.net.http.HttpClient;
    33 import java.net.http.HttpClient;
       
    34 import java.net.http.HttpHeaders;
    34 import java.net.http.HttpRequest;
    35 import java.net.http.HttpRequest;
    35 import java.net.http.HttpRequest.BodyPublishers;
    36 import java.net.http.HttpRequest.BodyPublishers;
    36 import java.net.http.HttpResponse;
    37 import java.net.http.HttpResponse;
    37 import java.net.http.HttpResponse.BodyHandlers;
    38 import java.net.http.HttpResponse.BodyHandlers;
       
    39 import java.util.HashMap;
       
    40 import java.util.List;
       
    41 import java.util.Map;
       
    42 import java.util.function.BiPredicate;
    38 import org.testng.annotations.Test;
    43 import org.testng.annotations.Test;
       
    44 import static java.lang.String.format;
    39 import static java.nio.charset.StandardCharsets.UTF_8;
    45 import static java.nio.charset.StandardCharsets.UTF_8;
       
    46 import static java.util.Objects.requireNonNull;
    40 import static org.testng.Assert.assertEquals;
    47 import static org.testng.Assert.assertEquals;
    41 import static org.testng.Assert.assertTrue;
    48 import static org.testng.Assert.assertTrue;
    42 import static org.testng.Assert.fail;
    49 import static org.testng.Assert.fail;
    43 
    50 
    44 public class OfflineTesting {
    51 public class OfflineTesting {
    47         // be sure to return the appropriate client when testing
    54         // be sure to return the appropriate client when testing
    48         //return HttpClient.newHttpClient();
    55         //return HttpClient.newHttpClient();
    49         return FixedResponseHttpClient.createClientFrom(
    56         return FixedResponseHttpClient.createClientFrom(
    50                 HttpClient.newBuilder(),
    57                 HttpClient.newBuilder(),
    51                 200,
    58                 200,
    52                 FixedHttpHeaders.of("Server",  "nginx",
    59                 headersOf("Server", "nginx",
    53                                     "Content-Type", "text/html"),
    60                           "Content-Type", "text/html"),
    54                 "A response message");
    61                 "A response message");
    55     }
    62     }
    56 
    63 
    57     @Test
    64     @Test
    58     public void testResponseAsString() {
    65     public void testResponseAsString() {
    92     public void testFileNotFound() {
    99     public void testFileNotFound() {
    93         //HttpClient client = HttpClient.newHttpClient();
   100         //HttpClient client = HttpClient.newHttpClient();
    94         HttpClient client = FixedResponseHttpClient.createClientFrom(
   101         HttpClient client = FixedResponseHttpClient.createClientFrom(
    95                 HttpClient.newBuilder(),
   102                 HttpClient.newBuilder(),
    96                 404,
   103                 404,
    97                 FixedHttpHeaders.of("Connection",  "keep-alive",
   104                 headersOf("Connection",  "keep-alive",
    98                                     "Content-Length", "162",
   105                           "Content-Length", "162",
    99                                     "Content-Type", "text/html",
   106                           "Content-Type", "text/html",
   100                                     "Date", "Mon, 15 Jan 2018 15:01:16 GMT",
   107                           "Date", "Mon, 15 Jan 2018 15:01:16 GMT",
   101                                     "Server", "nginx"),
   108                           "Server", "nginx"),
   102                 "<html>\n" +
   109                 "<html>\n" +
   103                 "<head><title>404 Not Found</title></head>\n" +
   110                 "<head><title>404 Not Found</title></head>\n" +
   104                 "<body bgcolor=\"white\">\n" +
   111                 "<body bgcolor=\"white\">\n" +
   105                 "<center><h1>404 Not Found</h1></center>\n" +
   112                 "<center><h1>404 Not Found</h1></center>\n" +
   106                 "<hr><center>nginx</center>\n" +
   113                 "<hr><center>nginx</center>\n" +
   124     @Test
   131     @Test
   125     public void testEcho() {
   132     public void testEcho() {
   126         HttpClient client = FixedResponseHttpClient.createEchoClient(
   133         HttpClient client = FixedResponseHttpClient.createEchoClient(
   127                 HttpClient.newBuilder(),
   134                 HttpClient.newBuilder(),
   128                 200,
   135                 200,
   129                 FixedHttpHeaders.of("Connection",  "keep-alive"));
   136                 headersOf("Connection",  "keep-alive"));
   130 
   137 
   131         HttpRequest request = HttpRequest.newBuilder()
   138         HttpRequest request = HttpRequest.newBuilder()
   132                 .uri(URI.create("http://openjdk.java.net/echo"))
   139                 .uri(URI.create("http://openjdk.java.net/echo"))
   133                 .POST(BodyPublishers.ofString("Hello World"))
   140                 .POST(BodyPublishers.ofString("Hello World"))
   134                 .build();
   141                 .build();
   144     @Test
   151     @Test
   145     public void testEchoBlocking() throws IOException, InterruptedException {
   152     public void testEchoBlocking() throws IOException, InterruptedException {
   146         HttpClient client = FixedResponseHttpClient.createEchoClient(
   153         HttpClient client = FixedResponseHttpClient.createEchoClient(
   147                 HttpClient.newBuilder(),
   154                 HttpClient.newBuilder(),
   148                 200,
   155                 200,
   149                 FixedHttpHeaders.of("Connection",  "keep-alive"));
   156                 headersOf("Connection",  "keep-alive"));
   150 
   157 
   151         HttpRequest request = HttpRequest.newBuilder()
   158         HttpRequest request = HttpRequest.newBuilder()
   152                 .uri(URI.create("http://openjdk.java.net/echo"))
   159                 .uri(URI.create("http://openjdk.java.net/echo"))
   153                 .POST(BodyPublishers.ofString("Hello chegar!!"))
   160                 .POST(BodyPublishers.ofString("Hello chegar!!"))
   154                 .build();
   161                 .build();
   156         HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
   163         HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
   157         System.out.println("response: " + response);
   164         System.out.println("response: " + response);
   158         assertEquals(response.statusCode(), 200);
   165         assertEquals(response.statusCode(), 200);
   159         assertEquals(response.body(), "Hello chegar!!");
   166         assertEquals(response.body(), "Hello chegar!!");
   160     }
   167     }
       
   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     }
   161 }
   190 }