Commit 92e56c2f authored by Ricki Hirner's avatar Ricki Hirner

Fix HttpException.toString() + test

parent 4c0aed08
Pipeline #9079073 passed with stage
in 6 minutes and 16 seconds
buildscript {
ext.kotlin_version = '1.1.2-4'
ext.kotlin_version = '1.1.2-5'
repositories {
jcenter()
......
......@@ -11,6 +11,7 @@ package at.bitfire.dav4android.exception
import at.bitfire.dav4android.Constants
import okhttp3.Response
import okio.Buffer
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.Serializable
......@@ -51,11 +52,12 @@ open class HttpException: Exception, Serializable {
formatted.append(name).append(": ").append(value).append("\n")
request.body()?.let {
try {
formatted.append("\n")
val buffer = Buffer()
it.writeTo(buffer)
val baos = ByteArrayOutputStream()
while (!buffer.exhausted())
appendByte(formatted, buffer.readByte())
appendByte(baos, buffer.readByte())
formatted.append("\n").append(baos.toString())
} catch (e: IOException) {
Constants.log.warning("Couldn't read request body")
}
......@@ -69,11 +71,13 @@ open class HttpException: Exception, Serializable {
for (name in headers.names())
for (value in headers.values(name))
formatted.append(name).append(": ").append(value).append("\n")
response.body()?.use {
try {
formatted.append("\n")
val baos = ByteArrayOutputStream()
for (b in it.bytes())
appendByte(formatted, b)
appendByte(baos, b)
formatted.append("\n").append(baos.toString())
} catch(e: IOException) {
Constants.log.warning("Couldn't read response body")
}
......@@ -81,15 +85,12 @@ open class HttpException: Exception, Serializable {
this.response = formatted.toString()
}
private fun appendByte(formatted: StringBuilder, b: Byte) {
private fun appendByte(stream: ByteArrayOutputStream, b: Byte) {
when (b) {
'\r'.toByte() ->
formatted.append("[CR]")
'\n'.toByte() ->
formatted.append("[LF]\n")
in 0x20..0x7E -> // printable ASCII
formatted.append(b)
else -> formatted.append("[${String.format("%02x", b)}]")
'\r'.toByte() -> stream.write("[CR]".toByteArray())
'\n'.toByte() -> stream.write("[LF]\n".toByteArray())
in 0x20..0x7E -> stream.write(b.toInt()) // printable ASCII
else -> stream.write("[${String.format("%02x", b)}]".toByteArray())
}
}
......
/*
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.dav4android.exception;
import org.junit.Test;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
@SuppressWarnings("ThrowableInstanceNeverThrown")
public class HttpExceptionTest {
final static String responseMessage = "Unknown error";
@Test
public void testHttpFormatting() {
Request request = new Request.Builder()
.post(RequestBody.create(null, "REQUEST\nBODY" + (char)5))
.url("http://example.com")
.build();
Response response = new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_2)
.code(500)
.message(responseMessage)
.body(ResponseBody.create(null, (char)0x99 + "SERVER\r\nRESPONSE"))
.build();
HttpException e = new HttpException(response);
assertTrue(e.getMessage().contains("500"));
assertTrue(e.getMessage().contains(responseMessage));
assertTrue(e.getRequest().contains("REQUEST[LF]\nBODY[05]"));
assertTrue(e.getResponse().contains("[99]SERVER[CR][LF]\nRESPONSE"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment