-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.rs
More file actions
193 lines (165 loc) · 6.18 KB
/
webhook.rs
File metadata and controls
193 lines (165 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! HTTP POST webhook executor.
//!
//! Stored action JSON shape:
//! ```json
//! {
//! "type": "webhook",
//! "url": "https://example.com/hook",
//! "headers": {"Authorization": "Bearer ..."},
//! "payload": {"event": "hello", "data": 1}
//! }
//! ```
//!
//! `headers` and `payload` are optional. The executor POSTs `payload` (or `{}`
//! when absent) as JSON and forwards any caller-supplied headers verbatim. Auth
//! is not modeled beyond passthrough — secret resolution lives one layer up.
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookAction {
pub url: String,
#[serde(default)]
pub headers: HashMap<String, String>,
#[serde(default)]
pub payload: Option<Value>,
}
#[derive(Debug)]
pub struct WebhookResult {
pub status: u16,
pub body: String,
}
#[derive(Debug)]
pub enum WebhookError {
InvalidHeaderName(String),
InvalidHeaderValue(String),
Transport(reqwest::Error),
NonSuccessStatus { status: u16, body: String },
}
impl std::fmt::Display for WebhookError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WebhookError::InvalidHeaderName(name) => write!(f, "invalid header name: {name}"),
WebhookError::InvalidHeaderValue(name) => {
write!(f, "invalid header value for {name}")
}
WebhookError::Transport(e) => write!(f, "webhook transport error: {e}"),
WebhookError::NonSuccessStatus { status, body } => {
write!(f, "webhook returned non-success status {status}: {body}")
}
}
}
}
impl std::error::Error for WebhookError {}
/// POST `action.payload` as JSON to `action.url`, returning the response status
/// and body on success or a structured error otherwise.
///
/// 2xx is success; anything else surfaces as `NonSuccessStatus` so a caller can
/// decide whether to retry without re-parsing the response.
pub async fn execute_webhook(
client: &reqwest::Client,
action: &WebhookAction,
) -> Result<WebhookResult, WebhookError> {
let mut request = client
.post(&action.url)
.json(action.payload.as_ref().unwrap_or(&json!({})));
for (name, value) in &action.headers {
let header_name = reqwest::header::HeaderName::from_bytes(name.as_bytes())
.map_err(|_| WebhookError::InvalidHeaderName(name.clone()))?;
let header_value = reqwest::header::HeaderValue::from_str(value)
.map_err(|_| WebhookError::InvalidHeaderValue(name.clone()))?;
request = request.header(header_name, header_value);
}
let response = request.send().await.map_err(WebhookError::Transport)?;
let status = response.status().as_u16();
let body = response.text().await.map_err(WebhookError::Transport)?;
if (200..300).contains(&status) {
Ok(WebhookResult { status, body })
} else {
Err(WebhookError::NonSuccessStatus { status, body })
}
}
#[cfg(test)]
mod tests {
use super::*;
use wiremock::matchers::{body_json, header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
#[tokio::test]
async fn posts_payload_and_forwards_headers() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/hook"))
.and(header("authorization", "Bearer abc123"))
.and(body_json(json!({"event": "hello", "n": 1})))
.respond_with(ResponseTemplate::new(200).set_body_string(r#"{"ok":true}"#))
.expect(1)
.mount(&server)
.await;
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer abc123".to_string());
let action = WebhookAction {
url: format!("{}/hook", server.uri()),
headers,
payload: Some(json!({"event": "hello", "n": 1})),
};
let client = reqwest::Client::new();
let result = execute_webhook(&client, &action).await.unwrap();
assert_eq!(result.status, 200);
assert_eq!(result.body, r#"{"ok":true}"#);
}
#[tokio::test]
async fn defaults_to_empty_object_payload() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/empty"))
.and(body_json(json!({})))
.respond_with(ResponseTemplate::new(204))
.expect(1)
.mount(&server)
.await;
let action = WebhookAction {
url: format!("{}/empty", server.uri()),
headers: HashMap::new(),
payload: None,
};
let client = reqwest::Client::new();
let result = execute_webhook(&client, &action).await.unwrap();
assert_eq!(result.status, 204);
}
#[tokio::test]
async fn surfaces_non_success_status_as_error() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/boom"))
.respond_with(ResponseTemplate::new(500).set_body_string("kaboom"))
.mount(&server)
.await;
let action = WebhookAction {
url: format!("{}/boom", server.uri()),
headers: HashMap::new(),
payload: Some(json!({})),
};
let client = reqwest::Client::new();
let err = execute_webhook(&client, &action).await.unwrap_err();
match err {
WebhookError::NonSuccessStatus { status, body } => {
assert_eq!(status, 500);
assert_eq!(body, "kaboom");
}
other => panic!("expected NonSuccessStatus, got {other:?}"),
}
}
#[tokio::test]
async fn rejects_invalid_header_name() {
let mut headers = HashMap::new();
headers.insert("bad header\n".to_string(), "value".to_string());
let action = WebhookAction {
url: "http://localhost:1/unused".to_string(),
headers,
payload: None,
};
let client = reqwest::Client::new();
let err = execute_webhook(&client, &action).await.unwrap_err();
assert!(matches!(err, WebhookError::InvalidHeaderName(_)));
}
}