native_messaging 0.2.0

Cross-platform Rust native messaging host for browser extensions (Chrome & Firefox), with async helpers and manifest installer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use native_messaging::host::encode_message;
use serde_json::json;

#[tokio::test]
async fn test_encode_message() {
    let message = json!({ "key": "value" });
    let encoded = encode_message(&message).unwrap();

    // The first 4 bytes should be the length of the JSON content.
    let length_bytes = &encoded[0..4];
    let content_length = u32::from_ne_bytes(length_bytes.try_into().unwrap()) as usize;
    assert_eq!(content_length, encoded.len() - 4);

    // The rest of the bytes should be the JSON content.
    let content_bytes = &encoded[4..];
    let decoded_message: serde_json::Value = serde_json::from_slice(content_bytes).unwrap();
    assert_eq!(decoded_message, message);
}