|
| 1 | +package com.baeldung.java9.httpclient; |
| 2 | + |
| 3 | +import jdk.incubator.http.HttpClient; |
| 4 | +import jdk.incubator.http.HttpRequest; |
| 5 | +import jdk.incubator.http.HttpResponse; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.*; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +import static org.hamcrest.CoreMatchers.containsString; |
| 18 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 19 | +import static org.hamcrest.collection.IsEmptyCollection.empty; |
| 20 | +import static org.hamcrest.core.IsNot.not; |
| 21 | +import static org.junit.Assert.assertThat; |
| 22 | + |
| 23 | +/** |
| 24 | + * Created by adam. |
| 25 | + */ |
| 26 | +public class HttpClientTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { |
| 30 | + HttpRequest request = HttpRequest.newBuilder() |
| 31 | + .uri(new URI("https://postman-echo.com/post")) |
| 32 | + .headers("Content-Type", "text/plain;charset=UTF-8") |
| 33 | + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) |
| 34 | + .build(); |
| 35 | + |
| 36 | + HttpResponse<String> response = HttpClient.newBuilder() |
| 37 | + .proxy(ProxySelector.getDefault()) |
| 38 | + .build() |
| 39 | + .send(request, HttpResponse.BodyHandler.asString()); |
| 40 | + |
| 41 | + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 42 | + assertThat(response.body(), containsString("Sample body")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException { |
| 47 | + HttpRequest request = HttpRequest.newBuilder() |
| 48 | + .uri(new URI("http://stackoverflow.com")) |
| 49 | + .version(HttpClient.Version.HTTP_1_1) |
| 50 | + .GET() |
| 51 | + .build(); |
| 52 | + HttpResponse<String> response = HttpClient.newBuilder() |
| 53 | + .build() |
| 54 | + .send(request, HttpResponse.BodyHandler.asString()); |
| 55 | + |
| 56 | + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); |
| 57 | + assertThat(response.body(), containsString("https://stackoverflow.com/")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException { |
| 62 | + HttpRequest request = HttpRequest.newBuilder() |
| 63 | + .uri(new URI("http://stackoverflow.com")) |
| 64 | + .version(HttpClient.Version.HTTP_1_1) |
| 65 | + .GET() |
| 66 | + .build(); |
| 67 | + HttpResponse<String> response = HttpClient.newBuilder() |
| 68 | + .followRedirects(HttpClient.Redirect.ALWAYS) |
| 69 | + .build() |
| 70 | + .send(request, HttpResponse.BodyHandler.asString()); |
| 71 | + |
| 72 | + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 73 | + assertThat(response.finalRequest() |
| 74 | + .uri() |
| 75 | + .toString(), equalTo("https://stackoverflow.com/")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException { |
| 80 | + HttpRequest request = HttpRequest.newBuilder() |
| 81 | + .uri(new URI("https://postman-echo.com/basic-auth")) |
| 82 | + .GET() |
| 83 | + .build(); |
| 84 | + HttpResponse<String> response = HttpClient.newBuilder() |
| 85 | + .authenticator(new Authenticator() { |
| 86 | + @Override |
| 87 | + protected PasswordAuthentication getPasswordAuthentication() { |
| 88 | + return new PasswordAuthentication("postman", "password".toCharArray()); |
| 89 | + } |
| 90 | + }) |
| 91 | + .build() |
| 92 | + .send(request, HttpResponse.BodyHandler.asString()); |
| 93 | + |
| 94 | + assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException { |
| 99 | + HttpRequest request = HttpRequest.newBuilder() |
| 100 | + .uri(new URI("https://postman-echo.com/post")) |
| 101 | + .headers("Content-Type", "text/plain;charset=UTF-8") |
| 102 | + .POST(HttpRequest.BodyProcessor.fromString("Sample body")) |
| 103 | + .build(); |
| 104 | + CompletableFuture<HttpResponse<String>> response = HttpClient.newBuilder() |
| 105 | + .build() |
| 106 | + .sendAsync(request, HttpResponse.BodyHandler.asString()); |
| 107 | + |
| 108 | + assertThat(response.get() |
| 109 | + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException { |
| 114 | + HttpRequest request = HttpRequest.newBuilder() |
| 115 | + .uri(new URI("https://postman-echo.com/get")) |
| 116 | + .GET() |
| 117 | + .build(); |
| 118 | + |
| 119 | + CompletableFuture<HttpResponse<String>> response1 = HttpClient.newBuilder() |
| 120 | + .executor(Executors.newFixedThreadPool(2)) |
| 121 | + .build() |
| 122 | + .sendAsync(request, HttpResponse.BodyHandler.asString()); |
| 123 | + |
| 124 | + CompletableFuture<HttpResponse<String>> response2 = HttpClient.newBuilder() |
| 125 | + .executor(Executors.newFixedThreadPool(2)) |
| 126 | + .build() |
| 127 | + .sendAsync(request, HttpResponse.BodyHandler.asString()); |
| 128 | + |
| 129 | + CompletableFuture<HttpResponse<String>> response3 = HttpClient.newBuilder() |
| 130 | + .executor(Executors.newFixedThreadPool(2)) |
| 131 | + .build() |
| 132 | + .sendAsync(request, HttpResponse.BodyHandler.asString()); |
| 133 | + |
| 134 | + CompletableFuture.allOf(response1, response2, response3) |
| 135 | + .join(); |
| 136 | + |
| 137 | + assertThat(response1.get() |
| 138 | + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 139 | + assertThat(response2.get() |
| 140 | + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 141 | + assertThat(response3.get() |
| 142 | + .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException { |
| 147 | + HttpRequest request = HttpRequest.newBuilder() |
| 148 | + .uri(new URI("https://postman-echo.com/get")) |
| 149 | + .GET() |
| 150 | + .build(); |
| 151 | + |
| 152 | + HttpClient httpClient = HttpClient.newBuilder() |
| 153 | + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE)) |
| 154 | + .build(); |
| 155 | + |
| 156 | + httpClient.send(request, HttpResponse.BodyHandler.asString()); |
| 157 | + |
| 158 | + assertThat(httpClient.cookieManager() |
| 159 | + .get() |
| 160 | + .getCookieStore() |
| 161 | + .getCookies(), empty()); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException { |
| 166 | + HttpRequest request = HttpRequest.newBuilder() |
| 167 | + .uri(new URI("https://postman-echo.com/get")) |
| 168 | + .GET() |
| 169 | + .build(); |
| 170 | + |
| 171 | + HttpClient httpClient = HttpClient.newBuilder() |
| 172 | + .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) |
| 173 | + .build(); |
| 174 | + |
| 175 | + httpClient.send(request, HttpResponse.BodyHandler.asString()); |
| 176 | + |
| 177 | + assertThat(httpClient.cookieManager() |
| 178 | + .get() |
| 179 | + .getCookieStore() |
| 180 | + .getCookies(), not(empty())); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException { |
| 185 | + List<URI> targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2")); |
| 186 | + |
| 187 | + HttpClient client = HttpClient.newHttpClient(); |
| 188 | + |
| 189 | + List<CompletableFuture<String>> futures = targets.stream() |
| 190 | + .map(target -> client.sendAsync(HttpRequest.newBuilder(target) |
| 191 | + .GET() |
| 192 | + .build(), HttpResponse.BodyHandler.asString()) |
| 193 | + .thenApply(response -> response.body())) |
| 194 | + .collect(Collectors.toList()); |
| 195 | + |
| 196 | + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) |
| 197 | + .join(); |
| 198 | + |
| 199 | + if (futures.get(0) |
| 200 | + .get() |
| 201 | + .contains("foo1")) { |
| 202 | + assertThat(futures.get(0) |
| 203 | + .get(), containsString("bar1")); |
| 204 | + assertThat(futures.get(1) |
| 205 | + .get(), containsString("bar2")); |
| 206 | + } else { |
| 207 | + assertThat(futures.get(1) |
| 208 | + .get(), containsString("bar2")); |
| 209 | + assertThat(futures.get(1) |
| 210 | + .get(), containsString("bar1")); |
| 211 | + } |
| 212 | + |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments