|
1 | 1 | package com.baeldung.asynchttpclient; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertNotNull; |
5 | | -import static org.junit.Assert.assertTrue; |
6 | | - |
7 | | -import java.util.concurrent.ExecutionException; |
8 | | -import java.util.concurrent.Executors; |
9 | | -import java.util.concurrent.Future; |
10 | | -import java.util.concurrent.TimeUnit; |
11 | | -import java.util.concurrent.TimeoutException; |
12 | | - |
| 3 | +import io.netty.handler.codec.http.HttpHeaders; |
13 | 4 | import org.asynchttpclient.AsyncCompletionHandler; |
14 | 5 | import org.asynchttpclient.AsyncHandler; |
15 | 6 | import org.asynchttpclient.AsyncHttpClient; |
|
27 | 18 | import org.junit.Before; |
28 | 19 | import org.junit.Test; |
29 | 20 |
|
30 | | -import io.netty.handler.codec.http.HttpHeaders; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.Future; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.TimeoutException; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertEquals; |
| 28 | +import static org.junit.Assert.assertNotNull; |
| 29 | +import static org.junit.Assert.assertTrue; |
31 | 30 |
|
32 | 31 | public class AsyncHttpClientTestCase { |
33 | 32 |
|
34 | | - private static AsyncHttpClient HTTP_CLIENT; |
35 | | - |
36 | | - @Before |
37 | | - public void setup() { |
38 | | - |
39 | | - AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); |
40 | | - HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); |
41 | | - } |
42 | | - |
43 | | - @Test |
44 | | - public void givenHttpClient_executeSyncGetRequest() { |
45 | | - |
46 | | - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); |
47 | | - |
48 | | - Future<Response> responseFuture = boundGetRequest.execute(); |
49 | | - try { |
50 | | - Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); |
51 | | - assertNotNull(response); |
52 | | - assertEquals(200, response.getStatusCode()); |
53 | | - |
54 | | - } catch (InterruptedException e) { |
55 | | - e.printStackTrace(); |
56 | | - } catch (ExecutionException e) { |
57 | | - e.printStackTrace(); |
58 | | - } catch (TimeoutException e) { |
59 | | - e.printStackTrace(); |
60 | | - } |
61 | | - |
62 | | - try { |
63 | | - Thread.sleep(5000); |
64 | | - } catch (InterruptedException e) { |
65 | | - e.printStackTrace(); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - @Test |
70 | | - public void givenHttpClient_executeAsyncGetRequest() { |
71 | | - |
72 | | - // execute an unbound GET request |
73 | | - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
74 | | - |
75 | | - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler<Integer>() { |
76 | | - @Override |
77 | | - public Integer onCompleted(Response response) throws Exception { |
78 | | - |
79 | | - int resposeStatusCode = response.getStatusCode(); |
80 | | - assertEquals(200, resposeStatusCode); |
81 | | - return resposeStatusCode; |
82 | | - } |
83 | | - }); |
84 | | - |
85 | | - // execute a bound GET request |
86 | | - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); |
87 | | - |
88 | | - boundGetRequest.execute(new AsyncCompletionHandler<Integer>() { |
89 | | - @Override |
90 | | - public Integer onCompleted(Response response) throws Exception { |
91 | | - |
92 | | - int resposeStatusCode = response.getStatusCode(); |
93 | | - assertEquals(200, resposeStatusCode); |
94 | | - return resposeStatusCode; |
95 | | - } |
96 | | - }); |
97 | | - |
98 | | - try { |
99 | | - Thread.sleep(5000); |
100 | | - } catch (InterruptedException e) { |
101 | | - e.printStackTrace(); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - @Test |
106 | | - public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { |
107 | | - |
108 | | - // execute an unbound GET request |
109 | | - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
110 | | - |
111 | | - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler<Integer>() { |
112 | | - |
113 | | - int responseStatusCode = -1; |
114 | | - |
115 | | - @Override |
116 | | - public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception { |
117 | | - responseStatusCode = responseStatus.getStatusCode(); |
118 | | - return State.CONTINUE; |
119 | | - } |
120 | | - |
121 | | - @Override |
122 | | - public State onHeadersReceived(HttpHeaders headers) throws Exception { |
123 | | - return State.CONTINUE; |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { |
128 | | - return State.CONTINUE; |
129 | | - } |
130 | | - |
131 | | - @Override |
132 | | - public void onThrowable(Throwable t) { |
133 | | - |
134 | | - } |
135 | | - |
136 | | - @Override |
137 | | - public Integer onCompleted() throws Exception { |
138 | | - assertEquals(200, responseStatusCode); |
139 | | - return responseStatusCode; |
140 | | - } |
141 | | - }); |
142 | | - |
143 | | - try { |
144 | | - Thread.sleep(5000); |
145 | | - } catch (InterruptedException e) { |
146 | | - e.printStackTrace(); |
147 | | - } |
148 | | - } |
149 | | - |
150 | | - @Test |
151 | | - public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { |
152 | | - // execute an unbound GET request |
153 | | - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
154 | | - |
155 | | - ListenableFuture<Response> listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); |
156 | | - listenableFuture.addListener(() -> { |
157 | | - Response response; |
158 | | - try { |
159 | | - response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); |
160 | | - assertEquals(200, response.getStatusCode()); |
161 | | - } catch (InterruptedException | ExecutionException | TimeoutException e) { |
162 | | - e.printStackTrace(); |
163 | | - } |
164 | | - |
165 | | - }, Executors.newCachedThreadPool()); |
166 | | - |
167 | | - try { |
168 | | - Thread.sleep(5000); |
169 | | - } catch (InterruptedException e) { |
170 | | - e.printStackTrace(); |
171 | | - } |
172 | | - } |
173 | | - |
174 | | - @Test |
175 | | - public void givenWebSocketClient_tryToConnect() { |
176 | | - |
177 | | - WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); |
178 | | - WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { |
179 | | - @Override |
180 | | - public void onOpen(WebSocket websocket) { |
181 | | - // WebSocket connection opened |
182 | | - } |
183 | | - |
184 | | - @Override |
185 | | - public void onClose(WebSocket websocket, int code, String reason) { |
186 | | - // WebSocket connection closed |
187 | | - } |
188 | | - |
189 | | - @Override |
190 | | - public void onError(Throwable t) { |
191 | | - // WebSocket connection error |
192 | | - assertTrue(t.getMessage().contains("Request timeout")); |
193 | | - } |
194 | | - }).build(); |
195 | | - |
196 | | - WebSocket WEBSOCKET_CLIENT = null; |
197 | | - try { |
198 | | - WEBSOCKET_CLIENT = Dsl.asyncHttpClient() |
199 | | - .prepareGet("ws://localhost:5590/websocket") |
200 | | - .addHeader("header_name", "header_value") |
201 | | - .addQueryParam("key", "value") |
202 | | - .setRequestTimeout(5000) |
203 | | - .execute(wsHandler).get(); |
204 | | - |
205 | | - if (WEBSOCKET_CLIENT.isOpen()) { |
206 | | - WEBSOCKET_CLIENT.sendPingFrame(); |
207 | | - WEBSOCKET_CLIENT.sendTextFrame("test message"); |
208 | | - WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' }); |
209 | | - } |
210 | | - |
211 | | - } catch (InterruptedException | ExecutionException e) { |
212 | | - e.printStackTrace(); |
213 | | - } finally { |
214 | | - if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { |
215 | | - WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); |
216 | | - } |
217 | | - } |
218 | | - } |
| 33 | + private static AsyncHttpClient HTTP_CLIENT; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setup() { |
| 37 | + AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); |
| 38 | + HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenHttpClient_executeSyncGetRequest() { |
| 43 | + |
| 44 | + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); |
| 45 | + |
| 46 | + Future<Response> responseFuture = boundGetRequest.execute(); |
| 47 | + try { |
| 48 | + Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); |
| 49 | + assertNotNull(response); |
| 50 | + assertEquals(200, response.getStatusCode()); |
| 51 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + Thread.sleep(5000); |
| 57 | + } catch (InterruptedException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenHttpClient_executeAsyncGetRequest() { |
| 64 | + |
| 65 | + // execute an unbound GET request |
| 66 | + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
| 67 | + |
| 68 | + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler<Integer>() { |
| 69 | + @Override |
| 70 | + public Integer onCompleted(Response response) { |
| 71 | + |
| 72 | + int resposeStatusCode = response.getStatusCode(); |
| 73 | + assertEquals(200, resposeStatusCode); |
| 74 | + return resposeStatusCode; |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // execute a bound GET request |
| 79 | + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); |
| 80 | + |
| 81 | + boundGetRequest.execute(new AsyncCompletionHandler<Integer>() { |
| 82 | + @Override |
| 83 | + public Integer onCompleted(Response response) { |
| 84 | + int resposeStatusCode = response.getStatusCode(); |
| 85 | + assertEquals(200, resposeStatusCode); |
| 86 | + return resposeStatusCode; |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + try { |
| 91 | + Thread.sleep(5000); |
| 92 | + } catch (InterruptedException e) { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { |
| 99 | + |
| 100 | + // execute an unbound GET request |
| 101 | + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
| 102 | + |
| 103 | + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler<Integer>() { |
| 104 | + |
| 105 | + int responseStatusCode = -1; |
| 106 | + |
| 107 | + @Override |
| 108 | + public State onStatusReceived(HttpResponseStatus responseStatus) { |
| 109 | + responseStatusCode = responseStatus.getStatusCode(); |
| 110 | + return State.CONTINUE; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public State onHeadersReceived(HttpHeaders headers) { |
| 115 | + return State.CONTINUE; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) { |
| 120 | + return State.CONTINUE; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onThrowable(Throwable t) { |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Integer onCompleted() { |
| 130 | + assertEquals(200, responseStatusCode); |
| 131 | + return responseStatusCode; |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + try { |
| 136 | + Thread.sleep(5000); |
| 137 | + } catch (InterruptedException e) { |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { |
| 144 | + // execute an unbound GET request |
| 145 | + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); |
| 146 | + |
| 147 | + ListenableFuture<Response> listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); |
| 148 | + listenableFuture.addListener(() -> { |
| 149 | + Response response; |
| 150 | + try { |
| 151 | + response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); |
| 152 | + assertEquals(200, response.getStatusCode()); |
| 153 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + }, Executors.newCachedThreadPool()); |
| 157 | + |
| 158 | + try { |
| 159 | + Thread.sleep(5000); |
| 160 | + } catch (InterruptedException e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void givenWebSocketClient_tryToConnect() { |
| 167 | + |
| 168 | + WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); |
| 169 | + WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { |
| 170 | + @Override |
| 171 | + public void onOpen(WebSocket websocket) { |
| 172 | + // WebSocket connection opened |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void onClose(WebSocket websocket, int code, String reason) { |
| 177 | + // WebSocket connection closed |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onError(Throwable t) { |
| 182 | + // WebSocket connection error |
| 183 | + assertTrue(t.getMessage().contains("Request timeout")); |
| 184 | + } |
| 185 | + }).build(); |
| 186 | + |
| 187 | + WebSocket WEBSOCKET_CLIENT = null; |
| 188 | + try { |
| 189 | + WEBSOCKET_CLIENT = Dsl.asyncHttpClient() |
| 190 | + .prepareGet("ws://localhost:5590/websocket") |
| 191 | + .addHeader("header_name", "header_value") |
| 192 | + .addQueryParam("key", "value") |
| 193 | + .setRequestTimeout(5000) |
| 194 | + .execute(wsHandler).get(); |
| 195 | + |
| 196 | + if (WEBSOCKET_CLIENT.isOpen()) { |
| 197 | + WEBSOCKET_CLIENT.sendPingFrame(); |
| 198 | + WEBSOCKET_CLIENT.sendTextFrame("test message"); |
| 199 | + WEBSOCKET_CLIENT.sendBinaryFrame(new byte[]{'t', 'e', 's', 't'}); |
| 200 | + } |
| 201 | + } catch (InterruptedException | ExecutionException e) { |
| 202 | + e.printStackTrace(); |
| 203 | + } finally { |
| 204 | + if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { |
| 205 | + WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
219 | 209 | } |
0 commit comments