-
Notifications
You must be signed in to change notification settings - Fork 73
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
Consider support for mocking websockets #113
Comments
I'd like to understand the design before going through a PR! |
Cool! Not entirely sure on design myself, but here's one possibility (for the user-facing part at least): use wiremock::ws::*;
Mock::given(ws::Upgrade) // new kind of matcher
.and(path("/hello_ws"))
// this could simply echo ws messages back to client. could also have another case which takes a closure
.respond_with_ws(ws::EchoResponder)
.mount(&mock_server)
.await; |
What does |
I would imagine you want a separate #[async_trait]
pub trait StreamingRespond: Send + Sync {
// Required method
async fn respond(&self, request: &Request, rx: Receiver<Bytes>, tx: Sender<Bytes>) -> ResponseTemplate;
} This might then be generic enough to allow for mocking grpc servers as well (exercise left to reader). I figured normal The choice of channels to use and whether you use/expose |
I figured something similar, but WebSockets specific (e.g. |
Yeah, I think for other things such as websockets or grpc you would need to bring in a library like tungstenite and tonic for the transport stuff that's not as visible to user (i.e. websocket handshaking etc). So they'd have to be optional dependencies (no need to add bloat for people). I guess that means adding some sort of transport style middleware to handle serializing/deserializing to things like websocket TCP frames and also doing any handshaking etc that has to be done. Maybe it would look a bit like: use wiremock::ws::*;
#[async_trait]
impl StreamingRespond for Streamer {
// left as an exercise to the reader
}
Mock::given(ws::Upgrade) // new kind of matcher
.and(path("/hello_ws"))
.transport(transport::Websocket) // Specify what transport will be used to send the bytes over
.streaming_respond_with(Streamer::default()) // Specify how data received from transport will be handled
.mount(&mock_server)
.await; |
An advantage of forgoing the generic approach would be handlers/implementors getting typed messages (e.g. |
I lean towards having the interface being specific to websockets—we can always generalize later if/when we decide to support something like gRPC (which hasn't been the case for the past ~3 years). |
Sure thing, I was running ahead to testing one of our gRPC services as well as the websocket ones 😅 I was also thinking from the generics on StreamingResponse how it would turn into a tower template soup so probably the best approach tbh |
I can help if you need early adopters to test and contribute based upon some familiarity with WebSockets as well as using (Currently working on a project that could use this, and we're already using Edit: fixed typo. |
Would this addition be welcome? I could look into writing a PR for this.
The text was updated successfully, but these errors were encountered: