-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug causing headers to be omitted when connecting to websocket using K/JS (nodejs) (KTOR-1629) #2307
Conversation
…ing K/JS (nodejs) (KTOR-1629)
63035b0
to
6bdf821
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @DRSchlaubi, thanks for the PR.
Could you add a test that fails before and pass after the PR?
I don't really know-how. I thought I just copy the echo test, but echo.websocket.org does not send back the headers |
Sure, you can add the test here: https://github.com/ktorio/ktor/blob/master/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt similar to other tests. And here is the test server: https://github.com/ktorio/ktor/blob/master/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/tests/WebSockets.kt. It will start automatically on the test run. For instance, you can set up a server handler to collect header fields from a request and send them back in a text frame(and verify them on the client-side for sure :) ) |
this would work for the ws tests but what about the wss tests since the test server can't use https |
Could you tell me why do we need wss to test headers content? |
It's just that we test the connection to ws and wss. I don't really think this matters though |
Yep, let's just have a regular ws test |
I can't actually read the code style check so don't know what to change EDIT: I finally found the login as a guest button to teamcity and could read the linter checks, but there seem to be a lot in files i didn't even change |
ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/JsClientEngine.kt
Outdated
Show resolved
Hide resolved
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt
Outdated
Show resolved
Hide resolved
Hey there, this issue seems to be affecting me too. A host exposes a WSS endpoint that needs a custom header to be sent in the request for validation. Despite being specified in the HttpHeaders, it's not propagated up to the actual request. Glad to see a fix is already being worked on! Is there anything I can do to help out? |
Actually this should be done already not sure if the lot team wants me to fix anything |
@DRSchlaubi could you rebase your PR with the latest master, which refactored the JS engine? ktor/ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/JsClientEngine.kt Line 67 in e3b5f60
Eg: // JsClientEngine.kt
val properties: dynamic = object { }
headers.forEach { name, values ->
val value = values.joinToString(",")
properties.headers[name] = value
}
NodeWebsocket(urlString, properties) |
Oh you now separated between nodeWebsocket and JS WebSocket, right? I*ll try to update the PR |
# Conflicts: # ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/JsClientEngine.kt # ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt
I couldn't run the tests after merging in master. I let them run for about an hour and Intellij still displayed "instanceiating tests" |
@DRSchlaubi Did you use |
I used Intellij. Will try cli later. Wouldn't be the first time that K/JS test don't work in Intellij |
That is not even one of my tests or related to code I changed |
I know, the screenshot shows not your test. It is only an example. |
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt
Show resolved
Hide resolved
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt
Show resolved
Hide resolved
Yeah now i am stuck with this again
|
Tests are stuck at downloading yarn deps |
assertEquals(CUSTOM_HEADER_VALUE, header) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compile error, this bracket is false
assertEquals(listOf(CUSTOM_HEADER_VALUE, CUSTOM_HEADER_VALUE), header) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compile error, this bracket is false
@@ -7,7 +7,7 @@ package io.ktor.client.engine.js.node | |||
import io.ktor.client.engine.js.compatibility.* | |||
import org.w3c.dom.* | |||
import org.w3c.fetch.* | |||
import kotlin.js.* | |||
import kotlin.js.Promise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code style, use star import
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt
Show resolved
Hide resolved
@DRSchlaubi could you please rebase the branch? Is it ready for review at the moment? |
I can't import ktor into intellij due to the error above |
# Conflicts: # ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/JsClientEngine.kt # ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/node/NodeDefinitions.kt
I rebased, but i couldn't run the tests as all tests give me an iOS error and core test complains about no module being specified |
Subsystem
Websocket client on Kotlin/JS (nodejs)
Motivation
Because the browser websocket client does not support custom headers on the WS handshake the Ktor JS engine just ignores them completely however
the used NodeJS package (ws) supports setting custom headers
Issue: KTOR-1629
Solution
Change the JsClientEngine.createWebsocketFunction() to also acknowledge the headers on nodejs