Skip to content

Commit

Permalink
Un-deprecate some setters for chaining from Java
Browse files Browse the repository at this point in the history
Closes: #5199
  • Loading branch information
swankjesse committed Dec 31, 2019
1 parent bd675a8 commit 9d2fe75
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
55 changes: 35 additions & 20 deletions mockwebserver/src/main/java/okhttp3/mockwebserver/MockResponse.kt
Expand Up @@ -105,10 +105,13 @@ class MockResponse : Cloneable {
level = DeprecationLevel.ERROR)
fun getStatus(): String = status

@Deprecated(
message = "moved to var. Replace setStatus(...) with status(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.status = status }"),
level = DeprecationLevel.WARNING)
/**
* Sets the status and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [status] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setStatus(status: String) = apply {
this.status = status
}
Expand Down Expand Up @@ -219,10 +222,13 @@ class MockResponse : Cloneable {
level = DeprecationLevel.ERROR)
fun getHeaders(): Headers = headers

@Deprecated(
message = "moved to var. Replace setHeaders(...) with headers(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.headers = headers }"),
level = DeprecationLevel.WARNING)
/**
* Sets the headers and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [headers] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setHeaders(headers: Headers) = apply { this.headers = headers }

@JvmName("-deprecated_getTrailers")
Expand All @@ -232,10 +238,13 @@ class MockResponse : Cloneable {
level = DeprecationLevel.ERROR)
fun getTrailers(): Headers = trailers

@Deprecated(
message = "moved to var. Replace setTrailers(...) with trailers(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.trailers = trailers }"),
level = DeprecationLevel.WARNING)
/**
* Sets the trailers and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [trailers] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setTrailers(trailers: Headers) = apply { this.trailers = trailers }

@JvmName("-deprecated_getSocketPolicy")
Expand All @@ -245,10 +254,13 @@ class MockResponse : Cloneable {
level = DeprecationLevel.ERROR)
fun getSocketPolicy() = socketPolicy

@Deprecated(
message = "moved to var. Replace setSocketPolicy(...) with socketPolicy(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.socketPolicy = socketPolicy }"),
level = DeprecationLevel.WARNING)
/**
* Sets the socket policy and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [socketPolicy] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setSocketPolicy(socketPolicy: SocketPolicy) = apply {
this.socketPolicy = socketPolicy
}
Expand All @@ -260,10 +272,13 @@ class MockResponse : Cloneable {
level = DeprecationLevel.ERROR)
fun getHttp2ErrorCode() = http2ErrorCode

@Deprecated(
message = "moved to var. Replace setHttp2ErrorCode(...) with http2ErrorCode(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.http2ErrorCode = http2ErrorCode }"),
level = DeprecationLevel.WARNING)
/**
* Sets the HTTP/2 error code and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [http2ErrorCode] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setHttp2ErrorCode(http2ErrorCode: Int) = apply {
this.http2ErrorCode = http2ErrorCode
}
Expand Down
Expand Up @@ -125,10 +125,13 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
headersToRedact = newHeadersToRedact
}

@Deprecated(
message = "Moved to var. Replace setLevel(...) with level(...) to fix Java",
replaceWith = ReplaceWith(expression = "apply { this.level = level }"),
level = DeprecationLevel.WARNING)
/**
* Sets the level and returns this.
*
* This was deprecated in OkHttp 4.0 in favor of the [level] val. In OkHttp 4.3 it is
* un-deprecated because Java callers can't chain when assigning Kotlin vals. (The getter remains
* deprecated).
*/
fun setLevel(level: Level) = apply {
this.level = level
}
Expand Down
19 changes: 19 additions & 0 deletions okhttp/src/test/java/okhttp3/CallKotlinTest.kt
Expand Up @@ -60,6 +60,25 @@ class CallKotlinTest {
assertThat("def").isEqualTo(response2.body!!.string())
}

@Test
fun socketPolicy() {
server.enqueue(MockResponse().setBody("abc"))
server.enqueue(MockResponse().setBody("def"))

val request = Request.Builder()
.url(server.url("/"))
.build()

val call = client.newCall(request)
val response1 = call.execute()

val cloned = call.clone()
val response2 = cloned.execute()

assertThat("abc").isEqualTo(response1.body!!.string())
assertThat("def").isEqualTo(response2.body!!.string())
}

@Test
fun testMockWebserverRequest() {
enableTls()
Expand Down

0 comments on commit 9d2fe75

Please sign in to comment.