From 5ac679dd0e8d559671cf0c9d01e910e07de37089 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Fri, 4 Nov 2016 15:59:56 +0000 Subject: [PATCH 1/2] String payloads to the HttpApi are assumed to be valid JSON and sent as is, previous behaviour would escape the string. --- .../main/kotlin/com/r3corda/testing/http/HttpApi.kt | 12 +++++++++++- .../kotlin/com/r3corda/testing/http/HttpUtils.kt | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) 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 index d771937a19..b3a010782a 100644 --- a/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt +++ b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpApi.kt @@ -5,10 +5,20 @@ import com.google.common.net.HostAndPort import java.net.URL class HttpApi(val root: URL) { + /** + * Send a PUT with a payload to the path on the API specified. + * + * @param data String values are assumed to be valid JSON. All other values will be mapped to JSON. + */ fun putJson(path: String, data: Any = Unit) = HttpUtils.putJson(URL(root, path), toJson(data)) + /** + * Send a POST with a payload to the path on the API specified. + * + * @param data String values are assumed to be valid JSON. All other values will be mapped to JSON. + */ fun postJson(path: String, data: Any = Unit) = HttpUtils.postJson(URL(root, path), toJson(data)) - private fun toJson(any: Any) = ObjectMapper().writeValueAsString(any) + private fun toJson(any: Any) = if (any is String) any else ObjectMapper().writeValueAsString(any) companion object { fun fromHostAndPort(hostAndPort: HostAndPort, base: String, protocol: String = "http"): HttpApi 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 index b183242d2f..4ba29c883c 100644 --- a/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt +++ b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt @@ -18,12 +18,12 @@ object HttpUtils { 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()) + return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").post(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()) + return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").post(body).build()) } private fun makeRequest(request: Request): Boolean { From 9fcdf70b33eb0dfd6168c323ea79265bfba3366b Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Fri, 4 Nov 2016 16:23:56 +0000 Subject: [PATCH 2/2] Fixed putJson actually posting Json. --- .../src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 4ba29c883c..11c66046af 100644 --- a/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt +++ b/test-utils/src/main/kotlin/com/r3corda/testing/http/HttpUtils.kt @@ -18,7 +18,7 @@ object HttpUtils { 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).header("Content-Type", "application/json").post(body).build()) + return makeRequest(Request.Builder().url(url).header("Content-Type", "application/json").put(body).build()) } fun postJson(url: URL, data: String) : Boolean {