From 959d2b0a4c4636c73d531d1097f062bf5f8e3edf Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 5 Oct 2017 22:22:21 +0200 Subject: [PATCH] Fix tests by parsing the created JSON reply --- .../net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt | 14 ++++++++------ .../kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt | 3 +++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt index 2e0eda3..75a0cd4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt @@ -54,7 +54,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(403)) assertThat(response.statusText, equalTo("Forbidden")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":false,\"error\":\"auth-required\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to false, "error" to "auth-required"))) } @Test @@ -64,7 +64,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(403)) assertThat(response.statusText, equalTo("Forbidden")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":false,\"error\":\"auth-required\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to false, "error" to "auth-required"))) } @Test @@ -75,7 +75,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(403)) assertThat(response.statusText, equalTo("Forbidden")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":false,\"error\":\"auth-required\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to false, "error" to "auth-required"))) } @Test @@ -86,7 +86,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(200)) assertThat(response.statusText, equalTo("OK")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":true,\"foo\":\"bar\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to true, "foo" to "bar"))) } @Test @@ -96,7 +96,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(403)) assertThat(response.statusText, equalTo("Forbidden")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":false,\"error\":\"auth-required\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to false, "error" to "auth-required"))) } @Test @@ -106,7 +106,7 @@ class JsonPageBaseTest : TestObjects() { assertThat(response.statusCode, equalTo(200)) assertThat(response.statusText, equalTo("OK")) assertThat(response.contentType, equalTo("application/json")) - assertThat(outputStream.toString("UTF-8"), equalTo("{\"success\":true,\"foo\":\"bar\"}")) + assertThat(outputStream.toString("UTF-8").asJson(), equalTo(mapOf("success" to true, "foo" to "bar"))) } @Test @@ -130,4 +130,6 @@ class JsonPageBaseTest : TestObjects() { assertThat(page.isPrefixPage, equalTo(false)) } + private fun String.asJson() = objectMapper.readValue(this, Map::class.java) as Map + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt index a7e0a8a..1cbba3d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web.ajax +import com.fasterxml.jackson.databind.ObjectMapper import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext import freenet.l10n.BaseL10n @@ -39,6 +40,8 @@ import javax.naming.SizeLimitExceededException */ open class TestObjects { + val objectMapper = ObjectMapper() + val webInterface = mock() var formPassword = "form-password" val l10n = mock() -- 2.7.4