From: David ‘Bombe’ Roden Date: Thu, 5 Oct 2017 05:23:28 +0000 (+0200) Subject: Add tests for status texts, content types, and contents of JSON replies X-Git-Tag: 0.9.7^2~25 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ba8cf4e471423d30701ddac7a1f29cde42906f8a;p=Sone.git Add tests for status texts, content types, and contents of JSON replies --- 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 fd1af27..694f170 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageBaseTest.kt @@ -19,7 +19,7 @@ class JsonPageBaseTest : TestObjects() { private var needsFormPassword = false private val pageCallCounter = AtomicInteger() - private var pageResponse = { JsonReturnObject(true) } + private var pageResponse = { JsonReturnObject(true).put("foo", "bar") } private val outputStream = ByteArrayOutputStream() private val response = Response(outputStream) @@ -52,6 +52,9 @@ class JsonPageBaseTest : TestObjects() { core.preferences.isRequireFullAccess = true page.handleRequest(freenetRequest, response) 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\"}")) } @Test @@ -59,6 +62,9 @@ class JsonPageBaseTest : TestObjects() { needsFormPassword = true page.handleRequest(freenetRequest, response) 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\"}")) } @Test @@ -67,6 +73,9 @@ class JsonPageBaseTest : TestObjects() { addRequestParameter("formPassword", formPassword + "_false") page.handleRequest(freenetRequest, response) 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\"}")) } @Test @@ -75,6 +84,9 @@ class JsonPageBaseTest : TestObjects() { addRequestParameter("formPassword", formPassword) page.handleRequest(freenetRequest, response) 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\"}")) } @Test @@ -82,20 +94,28 @@ class JsonPageBaseTest : TestObjects() { unsetCurrentSone() page.handleRequest(freenetRequest, response) 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\"}")) } @Test fun `page returns content if login is required and current Sone is set`() { page.handleRequest(freenetRequest, response) - assertThat(response.statusCode, equalTo(200)) assertThat(pageCallCounter.get(), equalTo(1)) + 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\"}")) } @Test fun `page returns 500 if execution throws exception`() { - pageResponse = { throw IllegalStateException() } + pageResponse = { throw IllegalStateException("some error occured") } page.handleRequest(freenetRequest, response) assertThat(response.statusCode, equalTo(500)) + assertThat(response.statusText, equalTo("some error occured")) + assertThat(response.contentType, equalTo("text/plain")) } @Test