Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with Firebase auth and FirebaseUrlFactory #2059

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")

package com.google.android.horologist.networks.okhttp.urlconnection

/*
Expand Down Expand Up @@ -47,8 +49,6 @@ import okhttp3.Protocol
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okhttp3.internal.notifyAll
import okhttp3.internal.wait
import okio.Buffer
import okio.BufferedSink
import okio.Pipe
Expand Down Expand Up @@ -381,9 +381,13 @@ public class FirebaseUrlFactory(private val client: Call.Factory) : URLStreamHan
return getResponse(true).code
}

override fun setRequestProperty(field: String, newValue: String) {
override fun setRequestProperty(field: String, newValue: String?) {
check(!connected) { "Cannot set request property after connection is made" }
requestHeaders[field] = newValue
if (newValue != null) {
requestHeaders[field] = newValue
} else {
requestHeaders.removeAll(field)
}
}

override fun setIfModifiedSince(newValue: Long) {
Expand Down Expand Up @@ -764,7 +768,7 @@ public class FirebaseUrlFactory(private val client: Call.Factory) : URLStreamHan
delegate.setIfModifiedSince(newValue)
}

override fun setRequestProperty(field: String, newValue: String) {
override fun setRequestProperty(field: String, newValue: String?) {
delegate.setRequestProperty(field, newValue)
}

Expand Down Expand Up @@ -933,3 +937,8 @@ public class FirebaseUrlFactory(private val client: Call.Factory) : URLStreamHan
}
}
}


fun Any.wait() = (this as Object).wait()

fun Any.notifyAll() = (this as Object).notifyAll()
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,29 @@ class FirebaseUrlFactoryTest {
assertThat(headers["Content-Type"]).isEqualTo("application/x-www-form-urlencoded")
assertThat(headers["Transfer-Encoding"]).isEqualTo("chunked")
}

@Test
fun clearProperty() {
server.enqueue(MockResponse().setBody("hello, world!"))

val conn = urlFactory.open(server.url("/").toUrl())

conn.setRequestProperty("a", "b")
conn.setRequestProperty("c", "d")
conn.setRequestProperty("a", null)

val text = conn.inputStream.bufferedReader().use {
it.readText()
}

assertThat(text).isEqualTo("hello, world!")
assertThat(conn.responseCode).isEqualTo(200)

val recordedRequest = server.takeRequest()
val headers = recordedRequest.headers

assertThat(headers["a"]).isNull()
assertThat(headers["c"]).isEqualTo("d")
assertThat(headers["Connection"]).isEqualTo("Keep-Alive")
}
}
Loading