Skip to content

Commit

Permalink
Fixes wrong wskey=null in RawSocketWebSocketClient + add test verifyi…
Browse files Browse the repository at this point in the history
…ng wrong length for wskey

Fixes #1146
  • Loading branch information
soywiz committed Dec 10, 2022
1 parent 1d3a1d7 commit 7983b54
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient(
url: String,
protocols: List<String>?,
origin: String?,
wskey: String?,
wskey: String,
debug: Boolean,
headers: Http.Headers,
dummy: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ suspend fun RawSocketWebSocketClient(
url: String,
protocols: List<String>? = null,
origin: String? = null,
wskey: String? = DEFAULT_WSKEY,
wskey: String = DEFAULT_WSKEY,
debug: Boolean = false,
connect: Boolean = true,
headers: Http.Headers = Http.Headers(),
Expand All @@ -41,7 +41,7 @@ suspend fun RawSocketWebSocketClient(
if (OS.isJsBrowserOrWorker) error("RawSocketWebSocketClient is not supported on JS browser. Use WebSocketClient instead")
val uri = URL(url)
val secure: Boolean = uri.isSecureScheme
return RawSocketWebSocketClient(coroutineContext, AsyncClient.create(secure = secure), uri, protocols, debug, origin, wskey ?: "mykey", headers, masked).also {
return RawSocketWebSocketClient(coroutineContext, AsyncClient.create(secure = secure), uri, protocols, debug, origin, wskey, headers, masked).also {
init(it)
if (connect) it.internalConnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ suspend fun WebSocketClient.readString() = onStringMessage.waitOneBase()
suspend fun WebSocketClient.readBinary() = onBinaryMessage.waitOneBase()

expect suspend fun WebSocketClient(
url: String,
protocols: List<String>?,
origin: String?,
wskey: String?,
debug: Boolean,
url: String,
protocols: List<String>?,
origin: String?,
wskey: String,
debug: Boolean,
headers: Http.Headers,
dummy: Boolean,
wsInit: WebSocketClient.() -> Unit = {},
Expand All @@ -67,7 +67,7 @@ suspend fun WebSocketClient(
url: String,
protocols: List<String>? = null,
origin: String? = null,
wskey: String? = DEFAULT_WSKEY,
wskey: String = DEFAULT_WSKEY,
debug: Boolean = false,
headers: Http.Headers = Http.Headers(),
wsInit: WebSocketClient.() -> Unit = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import com.soywiz.korio.stream.write16BE
import com.soywiz.korio.stream.writeBytes
import com.soywiz.korio.stream.writeString
import com.soywiz.krypto.encoding.hex
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

class RawRawSocketWebSocketClient {
@Test
Expand Down Expand Up @@ -127,4 +126,11 @@ class RawRawSocketWebSocketClient {
ws.close()
assertEquals("open,close[1002,testing!]", log.joinToString(","))
}

@Test
fun testWrongSizeWSKey() = suspendTestNoJs {
assertFailsWith<IllegalStateException> {
RawSocketWebSocketClient("ws://127.0.0.1/test", wskey = "...", connect = false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ actual suspend fun WebSocketClient(
url: String,
protocols: List<String>?,
origin: String?,
wskey: String?,
wskey: String,
debug: Boolean,
headers: Http.Headers,
dummy: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient(
url: String,
protocols: List<String>?,
origin: String?,
wskey: String?,
wskey: String,
debug: Boolean,
headers: Http.Headers,
dummy: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actual suspend fun WebSocketClient(
url: String,
protocols: List<String>?,
origin: String?,
wskey: String?,
wskey: String,
debug: Boolean,
headers: Http.Headers,
dummy: Boolean,
Expand Down

0 comments on commit 7983b54

Please sign in to comment.