From 3c697f5fb1b8dbad46eac8fa299ed7cebfb36159 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Tue, 9 Dec 2025 15:13:21 -0500 Subject: Factor push message publication out to its own helper component. The `Publisher` component handles the details of web push delivery. Callers must provide the subscription set, the current signer, and the message, while the publisher handles encoding and communication with web push endpoints. To facilitate testing, `Publisher` implements `Publish`, which is a new trait with the same interface. Components that might publish web push messages should rely on the trait where possible. The test suite now constructs an app with a dummy `Publish` impl, which captures push messages for examination. Note that the testing implementation of `Publish` is hand-crafted, and presently only acts to record the arguments it receives. The other alternative was to use a mocking library, such as `mockit`, and while I've used that approach before, I'm not super comfortable with the complexity in this situation. I think we can maintain a more reasonable testing `Publish` impl by hand, at least for now, and we can revisit that decision later if need be. Tests for the `ping` endpoint have been migrated to this endpoint. --- src/test/webpush.rs | 55 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) (limited to 'src/test/webpush.rs') diff --git a/src/test/webpush.rs b/src/test/webpush.rs index a611ad0..f33f03c 100644 --- a/src/test/webpush.rs +++ b/src/test/webpush.rs @@ -1,13 +1,16 @@ use std::{ + any::Any, mem, sync::{Arc, Mutex}, }; -use web_push::{WebPushClient, WebPushError, WebPushMessage}; +use web_push::{PartialVapidSignatureBuilder, SubscriptionInfo, WebPushError}; + +use crate::{error::failed::Failed, push::Publish}; #[derive(Clone)] pub struct Client { - sent: Arc>>, + sent: Arc>>, } impl Client { @@ -18,20 +21,48 @@ impl Client { } // Clears the list of sent messages (for all clones of this Client) when called, because we - // can't clone `WebPushMessage`s so we either need to move them or try to reconstruct them, - // either of which sucks but moving them sucks less. - pub fn sent(&self) -> Vec { + // can't clone `Publications`s, so we either need to move them or try to reconstruct them. + pub fn sent(&self) -> Vec { let mut sent = self.sent.lock().unwrap(); - mem::take(&mut *sent) + mem::take(&mut sent) } } -#[async_trait::async_trait] -impl WebPushClient for Client { - async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> { - let mut sent = self.sent.lock().unwrap(); - sent.push(message); +impl Publish for Client { + async fn publish( + &self, + message: M, + _: PartialVapidSignatureBuilder, + subscriptions: impl IntoIterator + Send, + ) -> Result, Failed> + where + M: Send + 'static, + { + let message: Box = Box::new(message); + let subscriptions = subscriptions.into_iter().collect(); + let publication = Publication { + message, + subscriptions, + }; + self.sent.lock().unwrap().push(publication); + + Ok(Vec::new()) + } +} + +pub struct Publication { + pub message: Box, + pub subscriptions: Vec, +} - Ok(()) +impl Publication { + pub fn message_eq(&self, candidate: &M) -> bool + where + M: PartialEq + 'static, + { + match self.message.downcast_ref::() { + None => false, + Some(message) => message == candidate, + } } } -- cgit v1.2.3