Skip to content

Commit 8af5ae3

Browse files
authored
Send Sec-WebSocket-Protocol (#5948)
1 parent 5546f4e commit 8af5ae3

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/websocket/internal/SubscribableWebSocket.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ internal class SubscribableWebSocket(
6565
private var pending = mutableListOf<ApolloRequest<*>>()
6666
private var _lastActiveMillis: Long = 0
6767

68-
private var webSocket: WebSocket = webSocketEngine.newWebSocket(serverUrl, httpHeaders, this)
68+
private var webSocket: WebSocket
69+
init {
70+
val headers = if (httpHeaders.any { it.name.lowercase() == "sec-websocket-protocol" }) {
71+
httpHeaders
72+
} else {
73+
httpHeaders + HttpHeader("Sec-WebSocket-Protocol", wsProtocol.name)
74+
}
75+
webSocket = webSocketEngine.newWebSocket(serverUrl, headers, this)
76+
}
6977

7078
val lastActiveMillis: Long
7179
get() = lock.withLock {

libraries/apollo-runtime/src/commonTest/kotlin/test/network/WebSocketNetworkTransportTest.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ import com.apollographql.apollo3.exception.ApolloWebSocketClosedException
88
import com.apollographql.apollo3.exception.DefaultApolloException
99
import com.apollographql.apollo3.exception.SubscriptionOperationException
1010
import com.apollographql.apollo3.interceptor.addRetryOnErrorInterceptor
11-
import com.apollographql.mockserver.CloseFrame
12-
import com.apollographql.mockserver.MockServer
13-
import com.apollographql.mockserver.TextMessage
14-
import com.apollographql.mockserver.WebSocketBody
15-
import com.apollographql.mockserver.WebsocketMockRequest
16-
import com.apollographql.mockserver.awaitWebSocketRequest
17-
import com.apollographql.mockserver.enqueueWebSocket
1811
import com.apollographql.apollo3.mpp.Platform
1912
import com.apollographql.apollo3.mpp.platform
2013
import com.apollographql.apollo3.network.websocket.WebSocketNetworkTransport
@@ -25,6 +18,14 @@ import com.apollographql.apollo3.testing.FooSubscription.Companion.errorMessage
2518
import com.apollographql.apollo3.testing.FooSubscription.Companion.nextMessage
2619
import com.apollographql.apollo3.testing.connectionAckMessage
2720
import com.apollographql.apollo3.testing.internal.runTest
21+
import com.apollographql.mockserver.CloseFrame
22+
import com.apollographql.mockserver.MockServer
23+
import com.apollographql.mockserver.TextMessage
24+
import com.apollographql.mockserver.WebSocketBody
25+
import com.apollographql.mockserver.WebsocketMockRequest
26+
import com.apollographql.mockserver.awaitWebSocketRequest
27+
import com.apollographql.mockserver.enqueueWebSocket
28+
import com.apollographql.mockserver.headerValueOf
2829
import kotlinx.coroutines.CoroutineScope
2930
import kotlinx.coroutines.delay
3031
import kotlinx.coroutines.flow.merge
@@ -41,6 +42,9 @@ class WebSocketNetworkTransportTest {
4142
.toFlow()
4243
.test {
4344
awaitConnectionInit()
45+
46+
assertEquals("graphql-transport-ws", serverReader.headers.headerValueOf("sec-websocket-protocol"))
47+
4448
val operationId = serverReader.awaitSubscribe()
4549
repeat(5) {
4650
serverWriter.enqueueMessage(nextMessage(operationId, it))

0 commit comments

Comments
 (0)