Skip to content

Commit dd2d61f

Browse files
author
Daniel Rees
authored
Added support for SSLException (#50)
1 parent 2733ed5 commit dd2d61f

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/main/kotlin/org/phoenixframework/Socket.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ const val WS_CLOSE_NORMAL = 1000
5454
/** The socket was closed due to a SocketException. Likely the client lost connectivity */
5555
const val WS_CLOSE_SOCKET_EXCEPTION = 4000
5656

57+
/** The socket was closed due to an SSLException. Likely the client lost connectivity */
58+
const val WS_CLOSE_SSL_EXCEPTION = 4001
59+
5760
/** The socket was closed due to an EOFException. Likely the server abruptly closed */
58-
const val WS_CLOSE_EOF_EXCEPTION = 4001
61+
const val WS_CLOSE_EOF_EXCEPTION = 4002
5962

6063
/**
6164
* Connects to a Phoenix Server

src/main/kotlin/org/phoenixframework/Transport.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import okhttp3.Response
2828
import okhttp3.WebSocket
2929
import okhttp3.WebSocketListener
3030
import java.io.EOFException
31-
import java.net.ConnectException
3231
import java.net.SocketException
3332
import java.net.URL
33+
import javax.net.ssl.SSLException
3434

3535
/**
3636
* Interface that defines different types of Transport layers. A default {@link WebSocketTransport}
@@ -137,9 +137,11 @@ class WebSocketTransport(
137137

138138
// Check if the socket was closed for some recoverable reason
139139
if (t is SocketException) {
140-
this.onClosed(webSocket, WS_CLOSE_SOCKET_EXCEPTION, "Socket Exception")
140+
this.onClosed(webSocket, WS_CLOSE_SOCKET_EXCEPTION, "SocketException")
141+
} else if (t is SSLException) {
142+
this.onClosed(webSocket, WS_CLOSE_SSL_EXCEPTION, "SSLException")
141143
} else if (t is EOFException) {
142-
this.onClosed(webSocket, WS_CLOSE_EOF_EXCEPTION, "EOF Exception")
144+
this.onClosed(webSocket, WS_CLOSE_EOF_EXCEPTION, "EOFException")
143145
}
144146
}
145147

src/test/kotlin/org/phoenixframework/WebSocketTransportTest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.mockito.MockitoAnnotations
1515
import java.io.EOFException
1616
import java.net.SocketException
1717
import java.net.URL
18+
import javax.net.ssl.SSLException
1819

1920
class WebSocketTransportTest {
2021

@@ -96,6 +97,19 @@ class WebSocketTransportTest {
9697
verify(mockOnClose).invoke(4000)
9798
}
9899

100+
@Test
101+
fun `onFailure also triggers onClose for SSLException`() {
102+
val mockOnError = mock<(Throwable, Response?) -> Unit>()
103+
val mockOnClose = mock<(Int) -> Unit>()
104+
transport.onClose = mockOnClose
105+
transport.onError = mockOnError
106+
107+
val throwable = SSLException("t")
108+
transport.onFailure(mockWebSocket, throwable, mockResponse)
109+
verify(mockOnError).invoke(throwable, mockResponse)
110+
verify(mockOnClose).invoke(4001)
111+
}
112+
99113
@Test
100114
fun `onFailure also triggers onClose for EOFException`() {
101115
val mockOnError = mock<(Throwable, Response?) -> Unit>()
@@ -106,7 +120,7 @@ class WebSocketTransportTest {
106120
val throwable = EOFException()
107121
transport.onFailure(mockWebSocket, throwable, mockResponse)
108122
verify(mockOnError).invoke(throwable, mockResponse)
109-
verify(mockOnClose).invoke(4001)
123+
verify(mockOnClose).invoke(4002)
110124
}
111125

112126
@Test

0 commit comments

Comments
 (0)