Skip to content
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

WebSocket support for Mock engine #1413

Closed
RiccardoM opened this issue Oct 24, 2019 · 8 comments
Closed

WebSocket support for Mock engine #1413

RiccardoM opened this issue Oct 24, 2019 · 8 comments
Assignees
Labels

Comments

@RiccardoM
Copy link

Subsystem
Client (Mock engine), WebSocket

Is your feature request related to a problem? Please describe.
Currently there is no WebSocket support for the Mock engine on the client part. This could be particularly useful when testing clients that interact with WebSockets.

Describe the solution you'd like
I think there should be a method inside the Mock engine that allows to setup a proper WebSocket mock. This could be something like

private val client = HttpClient(MockEngine) {
    engine {
        addWebSocketHandler(host = "localhost", port = 8080, path = "/ws") { incoming, outgoing ->

        }
    }
}

Motivation to include to ktor
This feature could be useful to all the people that want to test a client that interacts with a websocket without having to spin one up.

@e5l e5l self-assigned this Oct 24, 2019
@bbaldino
Copy link

I bumped into this as well, it would be great to have. I started taking a look at writing something for this, but ran into a couple things:

  1. At the time of interception in the MockEngine, HttpRequestData still has headers in 2 places: The request's headers, and the headers stored in the "body" (which is of type OutgoingContent at that point). In a real engine these headers are all merged together, but the MockEngine doesn't go that far, should the MockEngine change to merge all the headers? Or should handlers just have to check in both places?

  2. It would be nice to re-use a lot of the WebSocket logic in ktor-websockets, but that code appears to assume that it will be dealing with an ApplicationCall, so it's not easy to plumb into the MockEngine, should the core logic there be extracted out so it isn't tied to ApplicationCall so much? Or maybe TestApplicationCall should be used? (I'm mainly referring to io.ktor.websocket.WebSocketUpgrade, but only that's because that was the first thing I ran into).

@oleg-larshin
Copy link

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

@e5l
Copy link
Member

e5l commented Dec 27, 2021

Supported with testApplication API in 2.0.0.

@e5l e5l closed this as completed Dec 27, 2021
@shaunek
Copy link

shaunek commented Mar 19, 2022

@e5l This issue seems to be about websocket client testing, and you closed this issue out mentioning it is supported with the withTestApplication function... I could be missing something but withTestApplication seems to be pretty focused on the ktor server testing as it requires an Application.method to be provided. I looked and couldn't find any client-only focused mock engines that support websockets. The current docs (v1.6) still recommend using MockEngine with HttpClient, but MockEngine doesn't seem to support websockets as far as I can tell.

Are you aware of any client-only websocket testing library besides OkHTTP's MockWebServer? If so can you please point me in the right direction? For context, I have JVM-hosted websocket client that needs to consume a third party service over websockets and I'm not finding helpful examples that are unit testable. I was hoping to find something that doesn't require running a server to test the client code - something similar to ktor's HttpClient+MockEngine that makes it possible to test client code without running a server.

If you aren't aware of any libraries fitting my description I wonder if it could be helpful to reopen this issue?

@osrl
Copy link

osrl commented Apr 11, 2023

Hi @shaunek , I totally agree with you. I was going to use testApplication but it only works on JVM environment. I can't run tests on iOS. I've checked ktor repository for this and it looks like they use a gradle plugin to start an embedded server. Did you find any better alternative like you mentioned?

@shaunek
Copy link

shaunek commented Apr 13, 2023

@osrl Many cycles have passed since my post so admittedly I don't really remember detail, but I don't think I found any good alternatives so I used OkHTTP's MockWebServer which was kinda okay, but I'm not sure that will be super helpful to you in iOS/cross platform land.

@Anigif
Copy link

Anigif commented Apr 18, 2023

@shaunek Stumbled across this issue yesterday as I was going to test my websocket client and was a bit confused as well. I have made a comment in the YouTrack issue, not sure where it's best to comment though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants