Dodaj Origin dla WebSocket Reverb
This commit is contained in:
@@ -3,6 +3,7 @@ package pl.firmatpp.kierowca.sync
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import java.net.URI
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -49,9 +50,26 @@ class OkHttpLiveWebSocketFactory(
|
||||
private val client: OkHttpClient,
|
||||
) : LiveWebSocketFactory {
|
||||
override fun newWebSocket(url: String, listener: WebSocketListener): WebSocket =
|
||||
client.newWebSocket(Request.Builder().url(url).build(), listener)
|
||||
client.newWebSocket(
|
||||
Request.Builder()
|
||||
.url(url)
|
||||
.header("Origin", websocketOrigin(url))
|
||||
.build(),
|
||||
listener,
|
||||
)
|
||||
}
|
||||
|
||||
internal fun websocketOrigin(url: String): String =
|
||||
URI(
|
||||
url
|
||||
.replaceFirst("wss://", "https://")
|
||||
.replaceFirst("ws://", "http://")
|
||||
).let { uri ->
|
||||
val defaultPort = (uri.scheme == "https" && uri.port == 443) || (uri.scheme == "http" && uri.port == 80)
|
||||
val port = if (uri.port > 0 && !defaultPort) ":${uri.port}" else ""
|
||||
"${uri.scheme}://${uri.host}$port"
|
||||
}
|
||||
|
||||
enum class LiveSyncConnectionState {
|
||||
Stopped,
|
||||
WaitingForNetwork,
|
||||
|
||||
@@ -25,6 +25,13 @@ class DriverLiveSyncClientTest {
|
||||
reverbWsBaseUrl = "wss://example.test/app",
|
||||
)
|
||||
|
||||
@Test
|
||||
fun derivesBrowserCompatibleOriginFromWebSocketUrl() {
|
||||
assertEquals("https://api-intranet.firmatpp.pl", websocketOrigin("wss://api-intranet.firmatpp.pl/ws/app/app-key"))
|
||||
assertEquals("https://example.test:8443", websocketOrigin("wss://example.test:8443/app/app-key"))
|
||||
assertEquals("http://localhost:8080", websocketOrigin("ws://localhost:8080/app/app-key"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reconnectsWithBackoffAfterSocketFailure() = runTest {
|
||||
val factory = FakeWebSocketFactory()
|
||||
|
||||
Reference in New Issue
Block a user