Skip to content

Commit

Permalink
Add webhook_test.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed Sep 13, 2024
1 parent 1ed39be commit cec61ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bin/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ mod read_json_test;
mod socket_test;
mod sqlite_test;
mod traits_in_action_test;
mod webhook_test;
mod write_json_tests;
55 changes: 55 additions & 0 deletions src/bin/tests/webhook_test.rs
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
}
}

0 comments on commit cec61ea

Please sign in to comment.