From 7eeea9765371c94199ac8aa3b531cfb4688ba236 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Tue, 25 Oct 2016 14:23:18 +0100 Subject: [PATCH] Added new test utilities for HTTP requests. --- test-utils/build.gradle | 3 ++ .../com/r3corda/testing/http/HttpApi.kt | 17 +++++++++ .../com/r3corda/testing/http/HttpUtils.kt | 37 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt create mode 100644 test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt diff --git a/test-utils/build.gradle b/test-utils/build.gradle index c4eed4c13a..3c063219f7 100644 --- a/test-utils/build.gradle +++ b/test-utils/build.gradle @@ -44,6 +44,9 @@ dependencies { // Guava: Google test library (collections test suite) compile "com.google.guava:guava-testlib:19.0" + + // OkHTTP: Simple HTTP library. + compile 'com.squareup.okhttp3:okhttp:3.3.1' } quasarScan.dependsOn('classes', ':core:classes', ':contracts:classes') diff --git a/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt new file mode 100644 index 0000000000..f7758b86e8 --- /dev/null +++ b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt @@ -0,0 +1,17 @@ +package com.r3corda.testing.http + +import com.fasterxml.jackson.databind.ObjectMapper +import com.google.common.net.HostAndPort +import java.net.URL + +class HttpApi(val root: URL) { + fun putJson(path: String, data: Any) = HttpUtils.putJson(URL(root, path), toJson(data)) + fun postJson(path: String, data: Any) = HttpUtils.postJson(URL(root, path), toJson(data)) + + private fun toJson(any: Any) = ObjectMapper().writeValueAsString(any) + + companion object { + fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi + = HttpApi(URL("$protocol://$hostAndPort/$base/")) + } +} \ No newline at end of file diff --git a/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt new file mode 100644 index 0000000000..f2e91b8e4f --- /dev/null +++ b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt @@ -0,0 +1,37 @@ +package com.r3corda.testing.http + +import com.r3corda.core.utilities.loggerFor +import okhttp3.* +import java.net.URL +import java.util.concurrent.TimeUnit + +/** + * A small set of utilities for making HttpCalls, aimed at demos and tests. + */ +object HttpUtils { + private val logger = loggerFor() + private val client by lazy { + OkHttpClient.Builder() + .connectTimeout(5, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS).build() + } + + fun putJson(url: URL, data: String) : Boolean { + val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data) + return makeRequest(Request.Builder().url(url).put(body).build()) + } + + fun postJson(url: URL, data: String) : Boolean { + val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data) + return makeRequest(Request.Builder().url(url).post(body).build()) + } + + private fun makeRequest(request: Request): Boolean { + val response = client.newCall(request).execute() + + if (!response.isSuccessful) { + logger.error("Could not fulfill HTTP request. Status Code: ${response.code()}. Message: ${response.body().string()}") + } + return response.isSuccessful + } +} \ No newline at end of file