Commit f53ae9e9 authored by Ricki Hirner's avatar Ricki Hirner

Make sure DavException is Serializable

parent 83bad4d4
Pipeline #22718111 passed with stages
in 2 minutes and 11 seconds
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
package at.bitfire.dav4android package at.bitfire.dav4android
import java.io.Serializable
/** /**
* A WebDAV property. * A WebDAV property.
* *
...@@ -17,7 +19,7 @@ interface Property { ...@@ -17,7 +19,7 @@ interface Property {
class Name( class Name(
val namespace: String, val namespace: String,
val name: String val name: String
) { ): Serializable {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return if (other is Name) return if (other is Name)
......
...@@ -10,7 +10,6 @@ import at.bitfire.dav4android.Constants ...@@ -10,7 +10,6 @@ import at.bitfire.dav4android.Constants
import at.bitfire.dav4android.Property import at.bitfire.dav4android.Property
import at.bitfire.dav4android.XmlUtils import at.bitfire.dav4android.XmlUtils
import okhttp3.MediaType import okhttp3.MediaType
import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okio.Buffer import okio.Buffer
import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParser
...@@ -46,16 +45,16 @@ open class DavException @JvmOverloads constructor( ...@@ -46,16 +45,16 @@ open class DavException @JvmOverloads constructor(
} }
var request: Request? = null var request: String? = null
private set
var requestBody: String? = null
private set
/** /**
* Associated HTTP [Response]. Do not access [Response.body] because it will be closed. * Body excerpt of [request] (up to [MAX_EXCERPT_SIZE] characters). Only available
* Use [responseBody] instead. * if the HTTP request body was textual content and could be read again.
*/ */
val response: Response? var requestBody: String? = null
private set
val response: String?
/** /**
* Body excerpt of [response] (up to [MAX_EXCERPT_SIZE] characters). Only available * Body excerpt of [response] (up to [MAX_EXCERPT_SIZE] characters). Only available
...@@ -73,12 +72,12 @@ open class DavException @JvmOverloads constructor( ...@@ -73,12 +72,12 @@ open class DavException @JvmOverloads constructor(
init { init {
if (httpResponse != null) { if (httpResponse != null) {
response = httpResponse response = httpResponse.toString()
try { try {
request = httpResponse.request() request = httpResponse.request().toString()
request?.body()?.let { body -> httpResponse.request().body()?.let { body ->
body.contentType()?.let { body.contentType()?.let {
if (isPlainText(it)) { if (isPlainText(it)) {
val buffer = Buffer() val buffer = Buffer()
......
...@@ -17,6 +17,10 @@ import org.junit.After ...@@ -17,6 +17,10 @@ import org.junit.After
import org.junit.Assert.* import org.junit.Assert.*
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
class DavExceptionTest { class DavExceptionTest {
...@@ -83,6 +87,28 @@ class DavExceptionTest { ...@@ -83,6 +87,28 @@ class DavExceptionTest {
} }
} }
fun testSerialization() {
val url = sampleUrl()
val dav = DavResource(httpClient, url)
mockServer.enqueue(MockResponse()
.setResponseCode(500)
.setBody("12345"))
try {
dav.propfind(0, ResourceType.NAME).close()
fail("Expected DavException")
} catch (e: DavException) {
val baos = ByteArrayOutputStream()
val oos = ObjectOutputStream(baos)
oos.writeObject(e)
val ois = ObjectInputStream(ByteArrayInputStream(baos.toByteArray()))
val e2 = ois.readObject() as HttpException
assertEquals(500, e2.code)
assertTrue(e2.responseBody!!.contains("12345"))
}
}
/** /**
* Test precondition XML element (sample from RFC 4918 16) * Test precondition XML element (sample from RFC 4918 16)
*/ */
......
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