Skip to content

Commit

Permalink
[#629] Use shared HTTP client.
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Hudalla <kai.hudalla@bosch-si.com>
  • Loading branch information
Kai Hudalla committed Jun 12, 2018
1 parent 1873c57 commit 0956e41
Showing 1 changed file with 19 additions and 11 deletions.
Expand Up @@ -56,6 +56,8 @@
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
Expand Down Expand Up @@ -84,7 +86,7 @@ public class VertxBasedHttpProtocolAdapterTest {
public TestName testName = new TestName();

private static final Logger LOG = LoggerFactory.getLogger(VertxBasedHttpProtocolAdapterTest.class);
private static final String HOST = "localhost";
private static final String HOST = "127.0.0.1";

private static HonoClient tenantServiceClient;
private static HonoClient credentialsServiceClient;
Expand All @@ -96,6 +98,7 @@ public class VertxBasedHttpProtocolAdapterTest {
private static CommandConnection commandConnection;
private static Vertx vertx;
private static String deploymentId;
private static HttpClient httpClient;

/**
* Prepare the adapter by configuring it.
Expand Down Expand Up @@ -153,6 +156,7 @@ public static void deployAdapter(final TestContext ctx) {

config = new HttpProtocolAdapterProperties();
config.setInsecurePort(0);
config.setInsecurePortBindAddress(HOST);
config.setAuthenticationRequired(true);

httpAdapter = new VertxBasedHttpProtocolAdapter();
Expand All @@ -167,6 +171,10 @@ public static void deployAdapter(final TestContext ctx) {

vertx.deployVerticle(httpAdapter, ctx.asyncAssertSuccess(id -> {
deploymentId = id;
final HttpClientOptions options = new HttpClientOptions()
.setDefaultHost(HOST)
.setDefaultPort(httpAdapter.getInsecurePort());
httpClient = vertx.createHttpClient(options);
}));
}

Expand Down Expand Up @@ -229,7 +237,7 @@ public final void testPostTelemetryFailsForMissingBasicAuthHeader(final TestCont

final Async async = ctx.async();

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.handler(response -> {
ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, response.statusCode());
Expand All @@ -250,7 +258,7 @@ public final void testPostTelemetryFailsForInvalidCredentials(final TestContext
final Async async = ctx.async();
final String authHeader = getBasicAuth("testuser@DEFAULT_TENANT", "password123");

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.handler(response -> {
Expand All @@ -275,7 +283,7 @@ public final void testPostTelemetrySucceedsForValidCredentials(final TestContext
mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");
mockRegistrationAssertionFailsWith(HttpURLConnection.HTTP_NOT_FOUND);

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand All @@ -302,7 +310,7 @@ public final void testPutTelemetrySucceedsForValidCredentials(final TestContext
mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");
mockRegistrationAssertionFailsWith(HttpURLConnection.HTTP_NOT_FOUND);

vertx.createHttpClient().put(httpAdapter.getInsecurePort(), HOST, "/telemetry/DEFAULT_TENANT/device_1")
httpClient.put( "/telemetry/DEFAULT_TENANT/device_1")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand All @@ -326,7 +334,7 @@ public final void testPostTelemetryFailsForNotSupportedQoSLevel(final TestContex

mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(Constants.HEADER_QOS_LEVEL, String.valueOf(2))
Expand All @@ -351,7 +359,7 @@ public final void testPostTelemetrySucceedsForSupportedQoSLevel(final TestContex
mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");
mockRegistrationAssertionFailsWith(HttpURLConnection.HTTP_NOT_FOUND);

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(Constants.HEADER_QOS_LEVEL, String.valueOf(1))
Expand All @@ -378,7 +386,7 @@ public final void testPutEventSucceedsForValidCredentials(final TestContext ctx)
mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");
mockRegistrationAssertionFailsWith(HttpURLConnection.HTTP_NOT_FOUND);

vertx.createHttpClient().put(httpAdapter.getInsecurePort(), HOST, "/event/DEFAULT_TENANT/device_1")
httpClient.put("/event/DEFAULT_TENANT/device_1")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand All @@ -402,7 +410,7 @@ public final void testPostTelemetrySendsMessageDownstream(final TestContext ctx)

mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry")
httpClient.post("/telemetry")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand All @@ -425,7 +433,7 @@ public final void testPostEventSendsMessageDownstream(final TestContext ctx) {

mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/event")
httpClient.post("/event")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand All @@ -450,7 +458,7 @@ public final void testPostTelemetryWithTtdOpensCommandReceiver(final TestContext

mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");

vertx.createHttpClient().post(httpAdapter.getInsecurePort(), HOST, "/telemetry?hono-ttd=1")
httpClient.post("/telemetry?hono-ttd=1")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpUtils.CONTENT_TYPE_JSON)
.putHeader(HttpHeaders.AUTHORIZATION, authHeader)
.putHeader(HttpHeaders.ORIGIN, "hono.eclipse.org")
Expand Down

0 comments on commit 0956e41

Please sign in to comment.