Skip to content

Commit

Permalink
Fix data (#134)
Browse files Browse the repository at this point in the history
* Remove unused 'serayuzgur.crates' extension from devcontainer settings

* Refactor ExpoPushMessageBuilder test to use structured data for 'data' field

* Add test for invalid sound in ExpoPushMessage builder

* Update README.md to include detailed configuration example for ExpoPushMessage

* Update dependencies in Cargo.toml to latest versions

* Bump version to 0.5.1 in Cargo.toml

* Remove duplicate comment in README.md for ExpoPushMessage configuration

* Add assertion test for ExpoPushMessage builder output
  • Loading branch information
katayama8000 authored Nov 4, 2024
1 parent 8bf5ed3 commit 059761d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"ms-azuretools.vscode-docker",
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
"serayuzgur.crates",
"fill-labs.dependi"
],
"settings": {
Expand Down
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "expo_push_notification_client"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
readme = "README.md"
authors = ["katayama8000 <https://github.com/katayama8000>"]
Expand All @@ -20,19 +20,19 @@ default-tls = ["reqwest/default-tls"]
rustls-tls = ["reqwest/rustls-tls"]

[dependencies]
async-compression = { version = "0.4.15", features = ["gzip", "tokio"] }
regex = "1.11.0"
reqwest = { version = "0.12.8", default-features = false, features = [
async-compression = { version = "0.4.17", features = ["gzip", "tokio"] }
regex = "1.11.1"
reqwest = { version = "0.12.9", default-features = false, features = [
"json",
"gzip",
] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.132"
serde_with = "3.11.0"
thiserror = "1.0.64"
tokio = { version = "1.40.0", features = ["io-util"] }
thiserror = "1.0.67"
tokio = { version = "1.41.0", features = ["io-util"] }

[dev-dependencies]
anyhow = "1.0.89"
anyhow = "1.0.92"
mockito = "1.5.0"
tokio = { version = "1.36.0", features = ["full"] }
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ expo.get_push_notification_receipts(expo_push_ids).await;
Additionally, you can further customize the ExpoPushMessage by adding more options. Refer to the [docs](https://docs.expo.dev/push-notifications/sending-notifications/#formats) for more details.
```rust
// Build Expo Push Message with detailed configurations
#[derive(Serialize)]
struct Data {
data: String,
}

let expo_push_message = ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"])
.body("body")
.data(&[("data".to_string())])?
.data(&Data { data: "data".to_string()})?
.ttl(100)
.expiration(100)
.priority("high")
Expand Down
80 changes: 47 additions & 33 deletions src/object/expo_push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,23 @@ impl ExpoPushMessageBuilder {
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn test_expo_push_message_builder() -> Result<(), ValidationError> {
#[derive(Serialize)]
struct Data {
data: String,
}
let message = ExpoPushMessage::builder([
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
])
.body("body")
.data(&[("data".to_string())])?
.data(&Data {
data: "data".to_string(),
})?
.ttl(100)
.expiration(100)
.priority("high")
Expand All @@ -258,11 +265,11 @@ mod tests {
to: vec![
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]".to_string(),
"ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]".to_string(),
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".to_string()
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".to_string(),
],
title: Some("title".to_string()),
body: Some("body".to_string()),
data: Some(Value::Array(vec![Value::String("data".to_string())])),
data: Some(json!({ "data": "data" })),
ttl: Some(100),
expiration: Some(100),
priority: Some("high".to_string()),
Expand All @@ -276,36 +283,34 @@ mod tests {
}
);

println!(
"{}",
serde_json::to_string_pretty(&message).map_err(|_| ValidationError::InvalidData)?
);

assert_eq!(
serde_json::to_string_pretty(&message).map_err(|_| ValidationError::InvalidData)?,
r#"{
"to": [
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
],
"title": "title",
"body": "body",
"data": [
"data"
],
"ttl": 100,
"expiration": 100,
"priority": "high",
"subtitle": "subtitle",
"sound": "default",
"badge": 1,
"channelId": "channel_id",
"categoryId": "category_id",
"mutableContent": true,
"_contentAvailable": true
}"#
);
let expected_json = json!({
"to": [
"ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
],
"title": "title",
"body": "body",
"data": {
"data": "data"
},
"ttl": 100,
"expiration": 100,
"priority": "high",
"subtitle": "subtitle",
"sound": "default",
"badge": 1,
"channelId": "channel_id",
"categoryId": "category_id",
"mutableContent": true,
"_contentAvailable": true
});

let serialized_message =
serde_json::to_value(&message).map_err(|_| ValidationError::InvalidData)?;
let expected_message =
serde_json::to_value(&expected_json).map_err(|_| ValidationError::InvalidData)?;
assert_eq!(serialized_message, expected_message);
Ok(())
}

Expand All @@ -328,4 +333,13 @@ mod tests {

assert_eq!(message, Err(ValidationError::InvalidPriority));
}

#[test]
fn expo_push_message_builder_invalid_sound() {
let message = ExpoPushMessage::builder(["ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]"])
.sound("invalid_sound")
.build();

assert_eq!(message, Err(ValidationError::InvalidSound));
}
}

0 comments on commit 059761d

Please sign in to comment.