-
Notifications
You must be signed in to change notification settings - Fork 134
chore: udpate guard websocket tests to include message sending #2320
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
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
How to use the Graphite Merge QueueAdd the label merge-queue to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
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.
PR Summary
These changes enhance the guard's WebSocket testing by validating echo functionality for text, binary, and ping/pong message types. Additionally, the TestServer utilities now support binding to a specific address via a new helper.
- Updated
/packages/edge/infra/guard/core/tests/websocket.rsto send and validate echo responses on text, binary, and ping messages. - Introduced
with_handler_and_addrin/packages/edge/infra/guard/core/tests/common/mod.rsfor binding TestServer to a specified address.
2 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
| // Create a TestServer with a specific server address and custom handler | ||
| pub async fn with_handler_and_addr<F, Fut>(addr: SocketAddr, handler: F) -> Self | ||
| where | ||
| F: Fn(Request<hyper::body::Incoming>, Arc<Mutex<Vec<TestRequest>>>) -> Fut | ||
| + Send | ||
| + 'static | ||
| + Clone, | ||
| Fut: Future<Output = Result<Response<Full<Bytes>>, std::convert::Infallible>> + Send, | ||
| { | ||
| // Create a server bound to the specific address | ||
| let listener = TcpListener::bind(addr).await.unwrap(); | ||
| let request_log = Arc::new(Mutex::new(Vec::new())); | ||
| let request_log_clone = request_log.clone(); | ||
|
|
||
| let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>(); | ||
|
|
||
| // Start the server with the custom handler | ||
| let handle = tokio::spawn(async move { | ||
| let mut shutdown_rx = shutdown_rx; | ||
|
|
||
| loop { | ||
| // Use select to check for shutdown signal | ||
| let accept_fut = listener.accept(); | ||
| let accept_or_shutdown = tokio::select! { | ||
| result = accept_fut => Some(result), | ||
| _ = &mut shutdown_rx => None, | ||
| }; | ||
|
|
||
| // Break the loop if shutdown was requested | ||
| let (stream, _) = match accept_or_shutdown { | ||
| Some(Ok(value)) => value, | ||
| Some(Err(_)) => break, | ||
| None => break, | ||
| }; | ||
|
|
||
| let io = TokioIo::new(stream); | ||
| let request_log = request_log_clone.clone(); | ||
| let handler = handler.clone(); | ||
|
|
||
| tokio::spawn(async move { | ||
| // Create a service function for this connection | ||
| let service = service_fn(move |req: Request<hyper::body::Incoming>| { | ||
| // Clone these for the async move block | ||
| let request_log = request_log.clone(); | ||
| let handler = handler.clone(); | ||
|
|
||
| async move { | ||
| // Capture request details | ||
| let method = req.method().to_string(); | ||
| let uri = req.uri().to_string(); | ||
|
|
||
| // Extract headers | ||
| let mut headers = HashMap::new(); | ||
| for (name, value) in req.headers() { | ||
| if let Ok(v) = value.to_str() { | ||
| headers.insert(name.to_string(), v.to_string()); | ||
| } | ||
| } | ||
|
|
||
| // Store request for later inspection | ||
| let test_req = TestRequest { | ||
| method, | ||
| uri, | ||
| headers, | ||
| body: Vec::new(), // Body will be consumed by handler | ||
| }; | ||
|
|
||
| request_log.lock().unwrap().push(test_req); | ||
|
|
||
| // Call the custom handler | ||
| handler(req, request_log.clone()).await | ||
| } | ||
| }); | ||
|
|
||
| if let Err(err) = hyper::server::conn::http1::Builder::new() | ||
| .serve_connection(io, service) | ||
| .await | ||
| { | ||
| eprintln!("Error serving connection: {:?}", err); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| Self { | ||
| addr, | ||
| request_log, | ||
| shutdown_tx: Some(shutdown_tx), | ||
| handle: Some(handle), | ||
| } | ||
| } |
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.
style: Duplicate logic; similar to with_addr. Consider consolidating common parts for reuse.
Deploying rivet-studio with
|
| Latest commit: |
b4f6e84
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d5c609ad.rivet-studio.pages.dev |
| Branch Preview URL: | https://04-07-chore-udpate-guard-web.rivet-studio.pages.dev |
Deploying rivet-hub with
|
| Latest commit: |
b4f6e84
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://88b34481.rivet-hub-7jb.pages.dev |
| Branch Preview URL: | https://04-07-chore-udpate-guard-web.rivet-hub-7jb.pages.dev |
Merge activity
|
<!-- Please make sure there is an issue that this PR is correlated to. --> ## Changes <!-- If there are frontend changes, please include screenshots. -->

Changes