use of io.vertx.ext.web.client.HttpResponse in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testPathMultiSimpleMatrix.
/**
* Test: path_multi_simple_matrix
* Expected parameters sent:
* color_simple: blue
* color_matrix: ;color=blue,black,brown
* Expected response: {"color_simple":"blue","color_matrix":["blue","black","brown"]}
* @throws Exception
*/
@Test
public void testPathMultiSimpleMatrix() throws Exception {
routerFactory.addHandlerByOperationId("path_multi_simple_matrix", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter colorSimple_path = params.pathParameter("color_simple");
assertNotNull(colorSimple_path);
assertTrue(colorSimple_path.isString());
assertEquals(colorSimple_path.getString(), "blue");
res.put("color_simple", colorSimple_path.getString());
RequestParameter colorMatrix_path = params.pathParameter("color_matrix");
assertNotNull(colorMatrix_path);
assertTrue(colorMatrix_path.isArray());
res.put("color_matrix", new JsonArray(colorMatrix_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
String colorSimple_path;
colorSimple_path = "blue";
List<Object> colorMatrix_path;
colorMatrix_path = new ArrayList<>();
colorMatrix_path.add("blue");
colorMatrix_path.add("black");
colorMatrix_path.add("brown");
startServer();
apiClient.pathMultiSimpleMatrix(colorSimple_path, colorMatrix_path, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
use of io.vertx.ext.web.client.HttpResponse in project vertx-web by vert-x3.
the class HttpContext method sendRequest.
private void sendRequest() {
Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
Context context = Vertx.currentContext();
if (ar.succeeded()) {
HttpClientResponse resp = ar.result();
Future<HttpResponse<Object>> fut = Future.future();
fut.setHandler(r -> {
// We are running on a context (the HTTP client mandates it)
context.runOnContext(v -> currentResponseHandler.handle(r));
});
resp.exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
resp.pause();
((BodyCodec<Object>) request.codec).create(ar2 -> {
resp.resume();
if (ar2.succeeded()) {
BodyStream<Object> stream = ar2.result();
stream.exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
resp.endHandler(v -> {
if (!fut.isComplete()) {
stream.end();
if (stream.result().succeeded()) {
fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
} else {
fut.fail(stream.result().cause());
}
}
});
Pump responsePump = Pump.pump(resp, stream);
responsePump.start();
} else {
currentResponseHandler.handle(Future.failedFuture(ar2.cause()));
}
});
} else {
currentResponseHandler.handle(Future.failedFuture(ar.cause()));
}
});
HttpClientRequest req;
String requestURI;
if (request.queryParams() != null && request.queryParams().size() > 0) {
QueryStringEncoder enc = new QueryStringEncoder(request.uri);
request.queryParams().forEach(param -> enc.addParam(param.getKey(), param.getValue()));
requestURI = enc.toString();
} else {
requestURI = request.uri;
}
int port = request.port;
String host = request.host;
if (request.ssl != request.options.isSsl()) {
req = request.client.client.request(request.method, new RequestOptions().setSsl(request.ssl).setHost(host).setPort(port).setURI(requestURI));
} else {
if (request.protocol != null && !request.protocol.equals("http") && !request.protocol.equals("https")) {
// we have to create an abs url again to parse it in HttpClient
try {
URI uri = new URI(request.protocol, null, host, port, requestURI, null, null);
req = request.client.client.requestAbs(request.method, uri.toString());
} catch (URISyntaxException ex) {
currentResponseHandler.handle(Future.failedFuture(ex));
return;
}
} else {
req = request.client.client.request(request.method, port, host, requestURI);
}
}
if (request.virtualHost != null) {
String virtalHost = request.virtualHost;
if (port != 80) {
virtalHost += ":" + port;
}
req.setHost(virtalHost);
}
req.setFollowRedirects(request.followRedirects);
if (request.headers != null) {
req.headers().addAll(request.headers);
}
req.handler(responseFuture::tryComplete);
if (request.timeout > 0) {
req.setTimeout(request.timeout);
}
if (body != null) {
if (contentType != null) {
String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
if (prev == null) {
req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
} else {
contentType = prev;
}
}
if (body instanceof ReadStream<?>) {
ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
if (request.headers == null || !request.headers.contains(HttpHeaders.CONTENT_LENGTH)) {
req.setChunked(true);
}
Pump pump = Pump.pump(stream, req);
req.exceptionHandler(err -> {
pump.stop();
stream.endHandler(null);
stream.resume();
responseFuture.tryFail(err);
});
stream.exceptionHandler(err -> {
req.reset();
responseFuture.tryFail(err);
});
stream.endHandler(v -> {
req.exceptionHandler(responseFuture::tryFail);
req.end();
pump.stop();
});
pump.start();
} else {
Buffer buffer;
if (body instanceof Buffer) {
buffer = (Buffer) body;
} else if (body instanceof MultiMap) {
try {
MultiMap attributes = (MultiMap) body;
boolean multipart = "multipart/form-data".equals(contentType);
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.POST, "/");
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
for (Map.Entry<String, String> attribute : attributes) {
encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
}
encoder.finalizeRequest();
for (String headerName : request.headers().names()) {
req.putHeader(headerName, request.headers().get(headerName));
}
if (encoder.isChunked()) {
buffer = Buffer.buffer();
while (true) {
HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
ByteBuf content = chunk.content();
if (content.readableBytes() == 0) {
break;
}
buffer.appendBuffer(Buffer.buffer(content));
}
} else {
ByteBuf content = request.content();
buffer = Buffer.buffer(content);
}
} catch (Exception e) {
throw new VertxException(e);
}
} else if (body instanceof JsonObject) {
buffer = Buffer.buffer(((JsonObject) body).encode());
} else {
buffer = Buffer.buffer(Json.encode(body));
}
req.exceptionHandler(responseFuture::tryFail);
req.end(buffer);
}
} else {
req.exceptionHandler(responseFuture::tryFail);
req.end();
}
}
use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.
the class HttpTestBase method testUploadMessagesWithTtdThatReplyWithCommand.
private void testUploadMessagesWithTtdThatReplyWithCommand(final HttpCommandEndpointConfiguration endpointConfig, final Tenant tenant, final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_TIME_TILL_DISCONNECT, "5");
final MultiMap cmdResponseRequestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_COMMAND_RESPONSE_STATUS, "200");
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
final String commandTargetDeviceId = endpointConfig.isSubscribeAsGateway() ? helper.setupGatewayDeviceBlocking(tenantId, deviceId, 5) : deviceId;
final String subscribingDeviceId = endpointConfig.isSubscribeAsGatewayForSingleDevice() ? commandTargetDeviceId : deviceId;
testUploadMessages(ctx, tenantId, msg -> {
return msg.getTimeUntilDisconnectNotification().map(notification -> {
logger.trace("received piggy backed message [ttd: {}]: {}", notification.getTtd(), msg);
ctx.verify(() -> {
assertThat(notification.getTenantId()).isEqualTo(tenantId);
assertThat(notification.getDeviceId()).isEqualTo(subscribingDeviceId);
});
// now ready to send a command
final JsonObject inputData = new JsonObject().put(COMMAND_JSON_KEY, (int) (Math.random() * 100));
return helper.sendCommand(tenantId, commandTargetDeviceId, COMMAND_TO_SEND, "application/json", inputData.toBuffer(), notification.getMillisecondsUntilExpiry()).map(response -> {
ctx.verify(() -> {
assertThat(response.getContentType()).isEqualTo("text/plain");
assertThat(response.getDeviceId()).isEqualTo(commandTargetDeviceId);
assertThat(response.getTenantId()).isEqualTo(tenantId);
assertThat(response.getCreationTime()).isNotNull();
});
return (Void) null;
});
}).orElseGet(Future::succeededFuture);
}, count -> {
final Buffer buffer = Buffer.buffer("hello " + count);
return sendHttpRequestForGatewayOrDevice(buffer, requestHeaders, endpointConfig, commandTargetDeviceId, true).map(httpResponse -> {
final String requestId = httpResponse.getHeader(Constants.HEADER_COMMAND_REQUEST_ID);
ctx.verify(() -> {
// assert that the response contains a command
assertWithMessage("response no. %s '%s' header", count, Constants.HEADER_COMMAND).that(httpResponse.getHeader(Constants.HEADER_COMMAND)).isNotNull();
assertThat(httpResponse.getHeader(Constants.HEADER_COMMAND)).isEqualTo(COMMAND_TO_SEND);
assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_TYPE.toString())).isEqualTo("application/json");
assertThat(requestId).isNotNull();
assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_LENGTH.toString())).isNotEqualTo("0");
});
return requestId;
}).compose(receivedCommandRequestId -> {
// send a response to the command now
final String responseUri = endpointConfig.getCommandResponseUri(tenantId, commandTargetDeviceId, receivedCommandRequestId);
logger.debug("sending response to command [uri: {}]", responseUri);
final Buffer body = Buffer.buffer("ok");
final Future<HttpResponse<Buffer>> result;
if (endpointConfig.isSubscribeAsGateway()) {
// GW uses PUT when acting on behalf of a device
result = httpClient.update(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
} else {
result = httpClient.create(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
}
return result.recover(thr -> {
// wrap exception, making clear it occurred when sending the command response, not the preceding telemetry/event message
final String msg = "Error sending command response: " + thr.getMessage();
return Future.failedFuture(new RuntimeException(msg, thr));
});
});
});
}
use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.
the class TelemetryHttpIT method testUploadQos1MessageFailsIfDeliveryStateNotUpdated.
/**
* Verifies that the upload of a QoS 1 telemetry message fails with a 503 status code
* when the consumer doesn't update the message delivery state and the
* <em>sendMessageTimeout</em> has elapsed.
*
* @param vertx The vert.x instance.
* @param ctx The test context
* @throws InterruptedException if test is interrupted while running.
*/
@Test
@AssumeMessagingSystem(type = MessagingType.amqp)
public void testUploadQos1MessageFailsIfDeliveryStateNotUpdated(final Vertx vertx, final VertxTestContext ctx) throws InterruptedException {
final AmqpApplicationClient amqpApplicationClient = (AmqpApplicationClient) helper.applicationClient;
// GIVEN a device and a north bound message consumer that doesn't update the message delivery state
final Tenant tenant = new Tenant();
final Checkpoint messageReceived = ctx.checkpoint();
final Checkpoint deliveryStateCheckDone = ctx.checkpoint();
final Checkpoint httpResponseReceived = ctx.checkpoint();
final VertxTestContext setup = new VertxTestContext();
final AtomicReference<ProtonDelivery> deliveryRef = new AtomicReference<>();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).compose(ok -> amqpApplicationClient.createTelemetryConsumer(tenantId, msg -> {
final Promise<Void> result = Promise.promise();
final var delivery = msg.getMessageContext().getDelivery();
deliveryRef.set(delivery);
logger.debug("received message: {}", msg.getMessageContext().getRawMessage());
ctx.verify(() -> {
assertThat(delivery.remotelySettled()).isFalse();
assertThat(delivery.getRemoteState()).isNull();
});
messageReceived.flag();
// don't update the delivery state here
return result.future();
}, remoteClose -> {
})).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
// WHEN the device tries to upload a telemetry message
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "binary/octet-stream").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_QOS_LEVEL, "1");
final Future<HttpResponse<Buffer>> httpResponseFuture = httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, // THEN the message gets rejected by the HTTP adapter with a 503
ResponsePredicate.status(HttpURLConnection.HTTP_UNAVAILABLE));
httpResponseFuture.onComplete(ctx.succeeding(response -> {
ctx.verify(() -> {
final var body = response.bodyAsJsonObject();
assertThat(body.getString(RequestResponseApiConstants.FIELD_ERROR)).isEqualTo(ServiceInvocationException.getLocalizedMessage(SendMessageTimeoutException.CLIENT_FACING_MESSAGE_KEY));
});
httpResponseReceived.flag();
// verify that the telemetry message delivery is remotely settled via the timeout handling in the adapter
vertx.setTimer(50, tid -> {
ctx.verify(() -> {
final ProtonDelivery delivery = deliveryRef.get();
assertThat(delivery).isNotNull();
assertThat(delivery.remotelySettled()).isTrue();
assertThat(delivery.getRemoteState()).isNotNull();
assertThat(delivery.getRemoteState().getType()).isEqualTo(DeliveryState.DeliveryStateType.Released);
});
deliveryStateCheckDone.flag();
});
}));
}
use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapCommandSucceeds.
/**
* Verifies that the result returned by the upstream mapping service contains the
* mapped payload.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@SuppressWarnings("unchecked")
@Test
public void testMapCommandSucceeds(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final HttpRequest<Buffer> httpRequest = mock(HttpRequest.class, withSettings().defaultAnswer(RETURNS_SELF));
final Buffer payload = Buffer.buffer("payload");
final Buffer responseBody = Buffer.buffer("changed");
final HttpResponse<Buffer> httpResponse = mock(HttpResponse.class);
when(httpResponse.bodyAsBuffer()).thenReturn(responseBody);
when(httpResponse.statusCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(mapperWebClient.post(anyInt(), anyString(), anyString())).thenReturn(httpRequest);
final Command command = mock(Command.class);
when(command.getPayload()).thenReturn(payload);
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setUpstreamMessageMapper("mapper");
messageMapping.mapUpstreamMessage(assertion, command).onComplete(ctx.succeeding(mappedBuffer -> {
ctx.verify(() -> {
assertThat(mappedBuffer).isEqualTo(responseBody);
verify(mapperWebClient, times(1)).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
final ArgumentCaptor<Handler<AsyncResult<HttpResponse<Buffer>>>> handleCaptor = VertxMockSupport.argumentCaptorHandler();
verify(httpRequest).sendBuffer(any(Buffer.class), handleCaptor.capture());
handleCaptor.getValue().handle(Future.succeededFuture(httpResponse));
}
Aggregations