From 5b4354c03075ab6ae6c6f301cd4b1595bc992943 Mon Sep 17 00:00:00 2001 From: WM Date: Wed, 24 Jan 2018 12:49:37 +0900 Subject: [PATCH 1/7] Add guessContentType() --- .../github/kittinunf/fuel/core/Response.kt | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt index ac2f08f36..896c6d30c 100644 --- a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt +++ b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt @@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream import java.io.IOException import java.io.InputStream import java.net.URL +import java.net.URLConnection class Response( val url: URL, @@ -33,17 +34,48 @@ class Response( } } - override fun toString(): String = buildString { - appendln("<-- $statusCode ($url)") - appendln("Response : $responseMessage") - appendln("Length : $contentLength") - appendln("Body : ${if (data.isNotEmpty()) String(data) else "(empty)"}") - appendln("Headers : (${headers.size})") - for ((key, value) in headers) { - appendln("$key : $value") +// override fun toString(): String = buildString { +// appendln("<-- $statusCode ($url)") +// appendln("Response : $responseMessage") +// appendln("Length : $contentLength") +// appendln("Body : ${if (data.isNotEmpty()) String(data) else "(empty)"}") +// appendln("Headers : (${headers.size})") +// for ((key, value) in headers) { +// appendln("$key : $value") +// } +// } + + override fun toString(): String { + var dataString = "(empty)" + val contentType = guessContentType() + if (contentType.isNotEmpty() && !contentType.contains("text/")) { + dataString = "${data.size} bytes of ${guessContentType()}" + } else if (data.isNotEmpty()) { + dataString = String(data) + } + + return buildString { + appendln("<-- $statusCode ($url)") + appendln("Response : $responseMessage") + appendln("Length : $contentLength") + appendln("Body : ($dataString)") + appendln("Headers : (${headers.size})") + for ((key, value) in headers) { + appendln("$key : $value") + } } } + fun guessContentType(): String { + val contentTypeFromHeaders = headers["Content-Type"]?.first() + if (contentTypeFromHeaders is String) { + return contentTypeFromHeaders + } + + val contentTypeFromStream = URLConnection.guessContentTypeFromStream(ByteArrayInputStream(data)) + return if(contentTypeFromStream.isNullOrEmpty()) "unknown" else contentTypeFromStream + } + companion object { fun error(): Response = Response(URL("http://.")) } From d9213a7f90d4c3d8bdc1386b416d6ed0401b8ce5 Mon Sep 17 00:00:00 2001 From: WM Date: Wed, 24 Jan 2018 13:31:25 +0900 Subject: [PATCH 2/7] Make response.toString() more flexible --- .../github/kittinunf/fuel/core/Response.kt | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt index 896c6d30c..6df76672a 100644 --- a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt +++ b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt @@ -34,22 +34,15 @@ class Response( } } -// override fun toString(): String = buildString { -// appendln("<-- $statusCode ($url)") -// appendln("Response : $responseMessage") -// appendln("Length : $contentLength") -// appendln("Body : ${if (data.isNotEmpty()) String(data) else "(empty)"}") -// appendln("Headers : (${headers.size})") -// for ((key, value) in headers) { -// appendln("$key : $value") -// } -// } - override fun toString(): String { var dataString = "(empty)" val contentType = guessContentType() - if (contentType.isNotEmpty() && !contentType.contains("text/")) { - dataString = "${data.size} bytes of ${guessContentType()}" + + if (contentType.isNotEmpty() && + (contentType.contains("image/") || + contentType.contains("application/octet-stream") + )) { + dataString = "$contentLength bytes of ${guessContentType()}" } else if (data.isNotEmpty()) { dataString = String(data) } @@ -73,7 +66,7 @@ class Response( } val contentTypeFromStream = URLConnection.guessContentTypeFromStream(ByteArrayInputStream(data)) - return if(contentTypeFromStream.isNullOrEmpty()) "unknown" else contentTypeFromStream + return if(contentTypeFromStream.isNullOrEmpty()) "(unknown)" else contentTypeFromStream } companion object { From 1bc4cec8e4a879a52a68d5c5bdd9f14a8c31d49c Mon Sep 17 00:00:00 2001 From: WM Date: Wed, 24 Jan 2018 13:32:44 +0900 Subject: [PATCH 3/7] Add guessContentType test --- .../com/github/kittinunf/fuel/RequestTest.kt | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt index ff07a1df8..613c9bb4e 100644 --- a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt +++ b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt @@ -105,6 +105,7 @@ class RequestTest : BaseTestCase() { assertThat(request, notNullValue()) assertThat(response, notNullValue()) + assertThat(response?.guessContentType(), isEqualTo("application/json")) assertThat(error, nullValue()) assertThat(data, notNullValue()) @@ -112,6 +113,60 @@ class RequestTest : BaseTestCase() { assertThat(response?.statusCode, isEqualTo(statusCode)) } + @Test + fun httpGetRequestWithImageResponse() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/image/png").responseString { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + assertThat(data, notNullValue()) + + assertThat(response?.guessContentType(), isEqualTo("image/png")) + + val statusCode = HttpURLConnection.HTTP_OK + assertThat(response?.statusCode, isEqualTo(statusCode)) + } + + @Test + fun httpGetRequestWithBytesResponse() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/bytes/555").responseString { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + assertThat(data, notNullValue()) + + assertThat(response?.guessContentType(), isEqualTo("application/octet-stream")) + + val statusCode = HttpURLConnection.HTTP_OK + assertThat(response?.statusCode, isEqualTo(statusCode)) + } + @Test fun httpGetRequestWithParameters() { var request: Request? = null @@ -539,5 +594,6 @@ class RequestTest : BaseTestCase() { assertThat(request.cUrlString(), isEqualTo("$ curl -i -X POST -H \"Authentication:Bearer xxx\" http://httpbin.org/post")) } + } From d0b972b6af120de2ac7aa64002f783aabf60c64d Mon Sep 17 00:00:00 2001 From: WM Date: Wed, 24 Jan 2018 18:16:14 +0900 Subject: [PATCH 4/7] Add new toString() test statement --- fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt index 613c9bb4e..ac8581d89 100644 --- a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt +++ b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt @@ -162,6 +162,7 @@ class RequestTest : BaseTestCase() { assertThat(data, notNullValue()) assertThat(response?.guessContentType(), isEqualTo("application/octet-stream")) + assertThat(response?.toString(), containsString("Body : (555 bytes of application/octet-stream)")) val statusCode = HttpURLConnection.HTTP_OK assertThat(response?.statusCode, isEqualTo(statusCode)) From 5760f733417363ff15da93c59a08d185485fbc6c Mon Sep 17 00:00:00 2001 From: Woramet Muangsiri Date: Sun, 28 Jan 2018 15:23:58 +0900 Subject: [PATCH 5/7] Late init dataString --- .../com/github/kittinunf/fuel/core/Response.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt index 6df76672a..38e3292e1 100644 --- a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt +++ b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt @@ -35,16 +35,16 @@ class Response( } override fun toString(): String { - var dataString = "(empty)" val contentType = guessContentType() - if (contentType.isNotEmpty() && - (contentType.contains("image/") || - contentType.contains("application/octet-stream") - )) { - dataString = "$contentLength bytes of ${guessContentType()}" + val dataString = if (contentType.isNotEmpty() && + (contentType.contains("image/") || + contentType.contains("application/octet-stream"))) { + "$contentLength bytes of ${guessContentType()}" } else if (data.isNotEmpty()) { - dataString = String(data) + String(data) + } else { + "(empty)" } return buildString { @@ -66,7 +66,7 @@ class Response( } val contentTypeFromStream = URLConnection.guessContentTypeFromStream(ByteArrayInputStream(data)) - return if(contentTypeFromStream.isNullOrEmpty()) "(unknown)" else contentTypeFromStream + return if (contentTypeFromStream.isNullOrEmpty()) "(unknown)" else contentTypeFromStream } companion object { From b6b9e475be57ec96f2726cf851adf59bb4d4ef70 Mon Sep 17 00:00:00 2001 From: WM Date: Mon, 29 Jan 2018 11:45:12 +0900 Subject: [PATCH 6/7] :white_check_mark: Add more response.guessContentType test --- .../github/kittinunf/fuel/core/Response.kt | 31 ++++---- .../com/github/kittinunf/fuel/RequestTest.kt | 77 ++++++++++++++++++- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt index 38e3292e1..e930a910f 100644 --- a/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt +++ b/fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Response.kt @@ -34,18 +34,9 @@ class Response( } } - override fun toString(): String { - val contentType = guessContentType() - - val dataString = if (contentType.isNotEmpty() && - (contentType.contains("image/") || - contentType.contains("application/octet-stream"))) { - "$contentLength bytes of ${guessContentType()}" - } else if (data.isNotEmpty()) { - String(data) - } else { - "(empty)" - } + override fun toString(): String { + val contentType = guessContentType(headers) + val dataString = processBody(contentType, data) return buildString { appendln("<-- $statusCode ($url)") @@ -59,9 +50,9 @@ class Response( } } - fun guessContentType(): String { + internal fun guessContentType(headers: Map>): String { val contentTypeFromHeaders = headers["Content-Type"]?.first() - if (contentTypeFromHeaders is String) { + if (contentTypeFromHeaders is String && !contentTypeFromHeaders.isNullOrEmpty()) { return contentTypeFromHeaders } @@ -69,6 +60,18 @@ class Response( return if (contentTypeFromStream.isNullOrEmpty()) "(unknown)" else contentTypeFromStream } + internal fun processBody(contentType: String, bodyData: ByteArray): String { + return if (contentType.isNotEmpty() && + (contentType.contains("image/") || + contentType.contains("application/octet-stream"))) { + "$contentLength bytes of ${guessContentType(headers)}" + } else if (bodyData.isNotEmpty()) { + String(bodyData) + } else { + "(empty)" + } + } + companion object { fun error(): Response = Response(URL("http://.")) } diff --git a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt index ac8581d89..f6f24c894 100644 --- a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt +++ b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt @@ -105,7 +105,6 @@ class RequestTest : BaseTestCase() { assertThat(request, notNullValue()) assertThat(response, notNullValue()) - assertThat(response?.guessContentType(), isEqualTo("application/json")) assertThat(error, nullValue()) assertThat(data, notNullValue()) @@ -134,7 +133,7 @@ class RequestTest : BaseTestCase() { assertThat(error, nullValue()) assertThat(data, notNullValue()) - assertThat(response?.guessContentType(), isEqualTo("image/png")) + assertThat(response?.toString(), containsString("bytes of image/png")) val statusCode = HttpURLConnection.HTTP_OK assertThat(response?.statusCode, isEqualTo(statusCode)) @@ -161,13 +160,85 @@ class RequestTest : BaseTestCase() { assertThat(error, nullValue()) assertThat(data, notNullValue()) - assertThat(response?.guessContentType(), isEqualTo("application/octet-stream")) assertThat(response?.toString(), containsString("Body : (555 bytes of application/octet-stream)")) val statusCode = HttpURLConnection.HTTP_OK assertThat(response?.statusCode, isEqualTo(statusCode)) } + @Test + fun testProcessBodyWithUnknownContentTypeAndNoData() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/bytes/555").responseString { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + + assertThat(response?.processBody("(unknown)", ByteArray(0)), isEqualTo("(empty)")) + } + + @Test + fun testGuessContentTypeWithNoContentTypeInHeaders() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/bytes/555").responseString { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + assertThat(data, notNullValue()) + + val headers: Map> = mapOf(Pair("Content-Type", listOf(""))) + assertThat(response?.guessContentType(headers), isEqualTo("(unknown)")) + } + + @Test + fun testGuessContentTypeWithNoHeaders() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/image/png").responseString { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + assertThat(data, notNullValue()) + + val headers: Map> = mapOf(Pair("Content-Type", listOf(""))) + assertThat(response?.guessContentType(headers), isEqualTo("image/png")) + } + @Test fun httpGetRequestWithParameters() { var request: Request? = null From 49e534e46f4427d7f6ff526863b672925b090ed0 Mon Sep 17 00:00:00 2001 From: WM Date: Mon, 29 Jan 2018 12:15:33 +0900 Subject: [PATCH 7/7] :white_check_mark: Add response.url test --- .../com/github/kittinunf/fuel/RequestTest.kt | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt index f6f24c894..17534fdde 100644 --- a/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt +++ b/fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt @@ -62,6 +62,32 @@ class RequestTest : BaseTestCase() { }() } + @Test + fun testResponseURLShouldSameWithRequestURL() { + var request: Request? = null + var response: Response? = null + var data: Any? = null + var error: FuelError? = null + + manager.request(Method.GET, "http://httpbin.org/get").response { req, res, result -> + request = req + response = res + + val (d, err) = result + data = d + error = err + } + + assertThat(request, notNullValue()) + assertThat(response, notNullValue()) + assertThat(error, nullValue()) + assertThat(data, notNullValue()) + + assertThat(request?.url, notNullValue()) + assertThat(response?.url, notNullValue()) + assertThat(request?.url, isEqualTo(response?.url)) + } + @Test fun httpGetRequestWithDataResponse() { var request: Request? = null @@ -170,7 +196,6 @@ class RequestTest : BaseTestCase() { fun testProcessBodyWithUnknownContentTypeAndNoData() { var request: Request? = null var response: Response? = null - var data: Any? = null var error: FuelError? = null manager.request(Method.GET, "http://httpbin.org/bytes/555").responseString { req, res, result -> @@ -178,7 +203,6 @@ class RequestTest : BaseTestCase() { response = res val (d, err) = result - data = d error = err }