test/jdk/java/net/httpclient/offline/OfflineTesting.java
author chegar
Wed, 07 Feb 2018 14:17:24 +0000
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56023 fa36a61f4cbf
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: move to standard package and module name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     1
/*
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     4
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     8
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    13
 * accompanied this code).
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    14
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    18
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    21
 * questions.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    22
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    23
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    24
/*
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    25
 * @test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    26
 * @summary Demonstrates how to achieve testing without network connections
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    27
 * @build FixedHttpHeaders DelegatingHttpClient FixedHttpResponse FixedResponseHttpClient
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    28
 * @run testng/othervm OfflineTesting
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    29
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    30
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    31
import java.io.IOException;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    32
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    33
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    34
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    35
import java.net.http.HttpResponse;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    36
import org.testng.annotations.Test;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    37
import static java.nio.charset.StandardCharsets.UTF_8;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    38
import static java.net.http.HttpRequest.BodyPublisher.fromString;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    39
import static java.net.http.HttpResponse.BodyHandler.asByteArray;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    40
import static java.net.http.HttpResponse.BodyHandler.asString;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    41
import static org.testng.Assert.assertEquals;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    42
import static org.testng.Assert.assertTrue;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    43
import static org.testng.Assert.fail;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    44
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    45
public class OfflineTesting {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    46
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    47
    private static HttpClient getClient() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    48
        // be sure to return the appropriate client when testing
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    49
        //return HttpClient.newHttpClient();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    50
        return FixedResponseHttpClient.createClientFrom(
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    51
                HttpClient.newBuilder(),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    52
                200,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    53
                FixedHttpHeaders.of("Server",  "nginx",
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    54
                                    "Content-Type", "text/html"),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    55
                "A response message");
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    56
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    57
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    58
    @Test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    59
    public void testResponseAsString() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    60
        HttpClient client = getClient();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    61
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    62
        HttpRequest request = HttpRequest.newBuilder()
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    63
                .uri(URI.create("http://openjdk.java.net/"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    64
                .build();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    65
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    66
        client.sendAsync(request, asString())
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    67
                .thenAccept(response -> {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    68
                    System.out.println("response: " + response);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    69
                    assertEquals(response.statusCode(), 200);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    70
                    assertTrue(response.headers().firstValue("Server").isPresent());
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    71
                    assertEquals(response.body(), "A response message"); } )
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    72
                .join();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    73
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    74
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    75
    @Test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    76
    public void testResponseAsByteArray() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    77
        HttpClient client = getClient();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    78
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    79
        HttpRequest request = HttpRequest.newBuilder()
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    80
                .uri(URI.create("http://openjdk.java.net/"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    81
                .build();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    82
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    83
        client.sendAsync(request, asByteArray())
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    84
                .thenAccept(response -> {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    85
                    System.out.println("response: " + response);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    86
                    assertEquals(response.statusCode(), 200);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    87
                    assertTrue(response.headers().firstValue("Content-Type").isPresent());
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    88
                    assertEquals(response.body(), "A response message".getBytes(UTF_8)); } )
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    89
                .join();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    90
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    91
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    92
    @Test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    93
    public void testFileNotFound() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    94
        //HttpClient client = HttpClient.newHttpClient();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    95
        HttpClient client = FixedResponseHttpClient.createClientFrom(
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    96
                HttpClient.newBuilder(),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    97
                404,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    98
                FixedHttpHeaders.of("Connection",  "keep-alive",
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    99
                                    "Content-Length", "162",
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   100
                                    "Content-Type", "text/html",
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   101
                                    "Date", "Mon, 15 Jan 2018 15:01:16 GMT",
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   102
                                    "Server", "nginx"),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   103
                "<html>\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   104
                "<head><title>404 Not Found</title></head>\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   105
                "<body bgcolor=\"white\">\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   106
                "<center><h1>404 Not Found</h1></center>\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   107
                "<hr><center>nginx</center>\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   108
                "</body>\n" +
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   109
                "</html>");
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   110
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   111
        HttpRequest request = HttpRequest.newBuilder()
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   112
                .uri(URI.create("http://openjdk.java.net/notFound"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   113
                .build();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   114
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   115
        client.sendAsync(request, asString())
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   116
                .thenAccept(response -> {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   117
                    assertEquals(response.statusCode(), 404);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   118
                    response.headers().firstValue("Content-Type")
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   119
                            .ifPresentOrElse(type -> assertEquals(type, "text/html"),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   120
                                             () -> fail("Content-Type not present"));
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   121
                    assertTrue(response.body().contains("404 Not Found")); } )
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   122
                .join();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   123
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   124
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   125
    @Test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   126
    public void testEcho() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   127
        HttpClient client = FixedResponseHttpClient.createEchoClient(
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   128
                HttpClient.newBuilder(),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   129
                200,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   130
                FixedHttpHeaders.of("Connection",  "keep-alive"));
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   131
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   132
        HttpRequest request = HttpRequest.newBuilder()
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   133
                .uri(URI.create("http://openjdk.java.net/echo"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   134
                .POST(fromString("Hello World"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   135
                .build();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   136
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   137
        client.sendAsync(request, asString())
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   138
                .thenAccept(response -> {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   139
                    System.out.println("response: " + response);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   140
                    assertEquals(response.statusCode(), 200);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   141
                    assertEquals(response.body(), "Hello World"); } )
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   142
                .join();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   143
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   144
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   145
    @Test
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   146
    public void testEchoBlocking() throws IOException, InterruptedException {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   147
        HttpClient client = FixedResponseHttpClient.createEchoClient(
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   148
                HttpClient.newBuilder(),
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   149
                200,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   150
                FixedHttpHeaders.of("Connection",  "keep-alive"));
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   151
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   152
        HttpRequest request = HttpRequest.newBuilder()
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   153
                .uri(URI.create("http://openjdk.java.net/echo"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   154
                .POST(fromString("Hello chegar!!"))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   155
                .build();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   156
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   157
        HttpResponse<String> response = client.send(request, asString());
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   158
        System.out.println("response: " + response);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   159
        assertEquals(response.statusCode(), 200);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   160
        assertEquals(response.body(), "Hello chegar!!");
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   161
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   162
}