-
Notifications
You must be signed in to change notification settings - Fork 557
/
HttpClientTest.java
85 lines (70 loc) · 3.01 KB
/
HttpClientTest.java
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
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.webserver.tyrus;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.websocket.server.ServerEndpointConfig;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
class HttpClientTest extends TyrusSupportBaseTest {
@BeforeAll
static void startServer() throws ExecutionException, InterruptedException, TimeoutException {
ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(
EchoEndpoint.class, "/");
webServer(true, builder.build());
}
@Test
void testJdkClient() throws ExecutionException, InterruptedException, TimeoutException {
URI uri = URI.create("ws://localhost:" + webServer().port() + "/tyrus/echo");
ClientListener listener = new ClientListener();
WebSocket webSocket = HttpClient.newHttpClient()
.newWebSocketBuilder()
.buildAsync(uri, listener)
.get();
webSocket.sendText("message", true);
webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "finished");
String response = listener.await();
assertThat(response, is("message"));
}
private static class ClientListener implements WebSocket.Listener {
private final CompletableFuture<String> future = new CompletableFuture<>();
@Override
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
future.complete(data.toString());
return CompletableFuture.completedFuture(data);
}
@Override
public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) {
future.complete(reason);
return CompletableFuture.completedFuture(reason);
}
@Override
public void onError(WebSocket webSocket, Throwable error) {
future.completeExceptionally(error);
}
public String await() throws ExecutionException, InterruptedException, TimeoutException {
return future.get(10, TimeUnit.SECONDS);
}
}
}