|
| 1 | +package com.baeldung.asynchttpclient; |
| 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 | + |
| 13 | +import org.asynchttpclient.AsyncCompletionHandler; |
| 14 | +import org.asynchttpclient.AsyncHandler; |
| 15 | +import org.asynchttpclient.AsyncHttpClient; |
| 16 | +import org.asynchttpclient.AsyncHttpClientConfig; |
| 17 | +import org.asynchttpclient.BoundRequestBuilder; |
| 18 | +import org.asynchttpclient.Dsl; |
| 19 | +import org.asynchttpclient.HttpResponseBodyPart; |
| 20 | +import org.asynchttpclient.HttpResponseStatus; |
| 21 | +import org.asynchttpclient.ListenableFuture; |
| 22 | +import org.asynchttpclient.Request; |
| 23 | +import org.asynchttpclient.Response; |
| 24 | +import org.asynchttpclient.ws.WebSocket; |
| 25 | +import org.asynchttpclient.ws.WebSocketListener; |
| 26 | +import org.asynchttpclient.ws.WebSocketUpgradeHandler; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import io.netty.handler.codec.http.HttpHeaders; |
| 31 | + |
| 32 | +public class AsyncHttpClientTestCase { |
| 33 | + |
| 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 | + } |
| 219 | +} |
0 commit comments