Skip to content

Commit

Permalink
Mark darwin ktor engine as not supporting status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Feb 25, 2024
1 parent 83490b7 commit 7f15f0f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
package org.hildan.krossbow.websocket.ktor

import io.ktor.client.engine.darwin.DarwinHttpRequestException
import kotlinx.cinterop.*
import platform.Foundation.*
internal actual fun extractHandshakeFailureDetails(handshakeException: Exception): HandshakeFailureDetails =
genericFailureDetails(handshakeException)

/*
io.ktor.client.engine.darwin.DarwinHttpRequestException:
Exception in http request: Error Domain=NSURLErrorDomain Code=-1011 "There was a bad response from the server."
UserInfo={
NSErrorFailingURLStringKey=ws://localhost:49347/failHandshakeWithStatusCode/200,
NSErrorFailingURLKey=ws://localhost:49347/failHandshakeWithStatusCode/200,
_NSURLErrorWebSocketHandshakeFailureReasonKey=0,
_NSURLErrorRelatedURLSessionTaskErrorKey=("LocalWebSocketTask <26F4D5BA-7104-4506-A521-DBC19B1CC2B0>.<1>"),
_NSURLErrorFailingURLSessionTaskErrorKey=LocalWebSocketTask <26F4D5BA-7104-4506-A521-DBC19B1CC2B0>.<1>,
NSLocalizedDescription=There was a bad response from the server.
}
*/
We cannot extract any response code from the exception.
internal actual fun extractHandshakeFailureDetails(handshakeException: Exception): HandshakeFailureDetails = when (handshakeException) {
is DarwinHttpRequestException -> extractHandshakeFailureDetails(handshakeException.origin)
else -> genericFailureDetails(handshakeException)
}
The original error is almost the same for all response codes:
io.ktor.client.engine.darwin.DarwinHttpRequestException: Exception in http request: Error Domain=NSURLErrorDomain Code=-1011 "There was a bad response from the server."
// FIXME clean this up after investigation
@OptIn(UnsafeNumber::class)
private fun extractHandshakeFailureDetails(originError: NSError): HandshakeFailureDetails = HandshakeFailureDetails(
statusCode = null, // TODO find out if we can get it from somewhere
additionalInfo = """
domain=${originError.domain}
code=${originError.code}
description=${originError.description}
userInfo=${originError.userInfo}
underlyingErrors=${originError.underlyingErrors}
localizedFailureReason=${originError.localizedFailureReason}
localizedRecoveryOptions=${originError.localizedRecoveryOptions}
helpAnchor=${originError.helpAnchor}
recoveryAttempter=${originError.recoveryAttempter}
originError to string: $originError
""".trimIndent(),
)
NSError object attached to the DarwinHttpRequestException:
{
code = -1011
description = Error Domain=NSURLErrorDomain Code=-1011 "There was a bad response from the server." UserInfo={NSErrorFailingURLStringKey=ws://localhost:49504/failHandshakeWithStatusCode/200, NSErrorFailingURLKey=ws://localhost:49504/failHandshakeWithStatusCode/200, _NSURLErrorWebSocketHandshakeFailureReasonKey=0, NSLocalizedDescription=There was a bad response from the server.}
userInfo = {
NSErrorFailingURLStringKey = ws://localhost:49347/failHandshakeWithStatusCode/200,
NSErrorFailingURLKey = ws://localhost:49347/failHandshakeWithStatusCode/200,
_NSURLErrorWebSocketHandshakeFailureReasonKey = 0, // sometimes different for tvOS
_NSURLErrorRelatedURLSessionTaskErrorKey = ("LocalWebSocketTask <26F4D5BA-7104-4506-A521-DBC19B1CC2B0>.<1>"),
_NSURLErrorFailingURLSessionTaskErrorKey = LocalWebSocketTask <26F4D5BA-7104-4506-A521-DBC19B1CC2B0>.<1>,
NSLocalizedDescription=There was a bad response from the server.
}
underlyingErrors = []
localizedFailureReason = null
localizedRecoveryOptions = null
helpAnchor = null
recoveryAttempter = null
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.hildan.krossbow.websocket.ktor
import io.ktor.client.engine.*
import io.ktor.client.engine.darwin.*

class KtorDarwinWebSocketClientTest : KtorClientTestSuite() {
class KtorDarwinWebSocketClientTest : KtorClientTestSuite(supportsStatusCodes = false) {

override fun provideEngine(): HttpClientEngineFactory<*> = Darwin
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import io.ktor.client.plugins.websocket.*
import org.hildan.krossbow.websocket.*
import org.hildan.krossbow.websocket.test.*

abstract class KtorClientTestSuite : WebSocketClientTestSuite() {
abstract class KtorClientTestSuite(
supportsStatusCodes: Boolean = true,
supportsCustomHeaders: Boolean = true,
) : WebSocketClientTestSuite(supportsStatusCodes, supportsCustomHeaders) {

override fun provideClient(): WebSocketClient = KtorWebSocketClient(
HttpClient(provideEngine()) { install(WebSockets) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.hildan.krossbow.websocket.ktor
import org.hildan.krossbow.websocket.WebSocketClient
import org.hildan.krossbow.websocket.test.*

// TODO should rather be based on Ktor engines, not platforms
// Currently the other platforms use CIO because of classpath order, and CIO supports status codes.
private val Platform.supportsStatusCodes: Boolean
get() = this !is Platform.Windows && this !is Platform.Js.Browser

Expand Down

0 comments on commit 7f15f0f

Please sign in to comment.