-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ed39be
commit cec61ea
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use axum::extract::Json; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
pub struct WebhookData { | ||
pub event: String, | ||
pub data: serde_json::Value, | ||
} | ||
|
||
pub async fn handle_webhook(Json(payload): Json<WebhookData>) { | ||
println!("Received webhook: {:?}", payload); | ||
} | ||
|
||
#[cfg(test)] | ||
mod webhook_test { | ||
use super::handle_webhook; | ||
use axum::{ | ||
body::Body, | ||
http::{Request, StatusCode}, | ||
routing::post, | ||
Router, | ||
}; | ||
use serde_json::json; | ||
use tower::ServiceExt; | ||
|
||
#[tokio::test] | ||
async fn test_webhook() { | ||
// Create a new router with the webhook handler | ||
let app = Router::new().route("/webhook", post(handle_webhook)); | ||
|
||
// Create a JSON payload | ||
let payload = json!({ | ||
"event": "test_event", | ||
"data": { | ||
"key": "value" | ||
} | ||
}); | ||
|
||
// Create a request with the JSON payload | ||
let request = Request::builder() | ||
.method("POST") | ||
.uri("/webhook") | ||
.header("Content-Type", "application/json") | ||
.body(Body::from(serde_json::to_string(&payload).unwrap())) | ||
.unwrap(); | ||
|
||
// Send the request to the router | ||
let response = app.oneshot(request).await.unwrap(); | ||
|
||
// Assert that the response status is 200 OK | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
// You can add more assertions here if your webhook handler returns a specific response | ||
} | ||
} |