laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Tests of `broadcast::reverb` that need an application.
use crate::broadcast::grant;
use crate::broadcast::reverb::Event;
use crate::broadcast::reverb::publish;
use crate::broadcast::reverb::signed_query;
use serde_json::json;

use crate::config::broadcasting::Reverb;
use crate::http::client::{self};

#[test]
fn signs_the_official_rest_api_example() {
    let settings = Reverb::new("3", "278d425bdf160c739803", "7ad3773142a6692b25b8");
    let channels = vec!["project-3".into()];
    let body = serde_json::to_vec(&Event {
        name: "foo",
        channels: &channels,
        data: r#"{"some":"data"}"#,
        socket_id: None,
    })
    .unwrap();

    assert_eq!(
        body,
        br#"{"name":"foo","channels":["project-3"],"data":"{\"some\":\"data\"}"}"#
    );
    let query = signed_query(&settings, "/apps/3/events", &body, 1_353_088_179);
    assert_eq!(query[3], ("body_md5", "ec365a775a4cd0599faeb73354201b6f".into()));
    assert_eq!(
        query[4],
        (
            "auth_signature",
            "da454824c97ba181a32ccc17a72625ba02771f50b50e1e7430e47a1f3f457e6c".into()
        )
    );
}

#[test]
fn grants_the_official_channel_authorization_examples() {
    let settings = Reverb::new("3", "278d425bdf160c739803", "7ad3773142a6692b25b8");

    assert_eq!(
        grant(&settings, "1234.1234", "private-foobar", None),
        json!({"auth": "278d425bdf160c739803:58df8b0c36d6982b82c3ecf6b4662e34fe8c25bba48f5369f135bf843651c3a4"})
    );
    let data = r#"{"user_id":10,"user_info":{"name":"Mr. Channel"}}"#;
    assert_eq!(
        grant(&settings, "1234.1234", "presence-foobar", Some(data.into())),
        json!({
            "auth": "278d425bdf160c739803:fc92c2263fd5b72721e20dd1a06a900e6b3c4fbfac6cb9b098d68eb7911498a2",
            "channel_data": data,
        })
    );
}

#[tokio::test]
async fn failures_name_reverb_and_its_reason_and_only_refusals_skip_retries() {
    crate::Application::configure()
        .replace_env(crate::config::Env::default())
        .create()
        .scope(async || {
            let settings = Reverb::new("app", "key", "secret");
            let message = crate::broadcast::Message::new("Updated".into(), vec!["orders".into()], json!({})).unwrap();
            for (response, permanent, reason) in [
                (
                    client::response().status(401).body("Authentication signature invalid."),
                    true,
                    "Authentication signature invalid.",
                ),
                (
                    client::response().status(404).body("No matching application for ID [app]."),
                    true,
                    "No matching application for ID [app].",
                ),
                (client::response().status(503), false, "503 Service Unavailable"),
                (client::response().status(429), false, "429 Too Many Requests"),
            ] {
                let _http = client::fake().with("localhost:8080/*", response);

                let error = publish(&settings, &message).await.unwrap_err();

                let report = format!("{error:#}");
                assert!(report.starts_with("Reverb refused the broadcast"), "{report}");
                assert!(report.contains(reason), "{report}");
                assert_eq!(crate::queue::delivery::refused(&error), permanent, "{report}");
            }

            let _http = client::fake().with("localhost:8080/*", client::failed_connection());
            let error = publish(&settings, &message).await.unwrap_err();
            let report = format!("{error:#}");
            assert!(
                report.starts_with("couldn't send the broadcast to Reverb: the HTTP request to http://localhost:8080/apps/app/events failed"),
                "{report}"
            );
            assert!(!crate::queue::delivery::refused(&error));
        })
        .await;
}