Fix tests by parsing the created JSON reply
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 5 Oct 2017 20:22:21 +0000 (22:22 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 5 Oct 2017 20:22:21 +0000 (22:22 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt

index 2e0eda3..75a0cd4 100644 (file)
@@ -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<String, Any>
+
 }
index a7e0a8a..1cbba3d 100644 (file)
@@ -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<WebInterface>()
        var formPassword = "form-password"
        val l10n = mock<BaseL10n>()