Skip to content

Commit cd98c67

Browse files
authored
Add Actix structured test and clean up a bit (#141)
Signed-off-by: Dejan Bosanac <[email protected]>
1 parent f6b45d1 commit cd98c67

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/actix/server_request.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl actix_web::FromRequest for Event {
126126
}
127127
}
128128

129-
/// Extention Trait for [`HttpRequest`] which acts as a wrapper for the function [`request_to_event()`].
129+
/// Extension Trait for [`HttpRequest`] which acts as a wrapper for the function [`request_to_event()`].
130130
///
131131
/// This trait is sealed and cannot be implemented for types outside of this crate.
132132
#[async_trait(?Send)]
@@ -160,19 +160,14 @@ mod tests {
160160
use actix_web::test;
161161

162162
use crate::{EventBuilder, EventBuilderV10};
163-
use chrono::Utc;
164163
use serde_json::json;
165164

166165
#[actix_rt::test]
167166
async fn test_request() {
168-
let time = Utc::now();
169167
let expected = EventBuilderV10::new()
170168
.id("0001")
171169
.ty("example.test")
172170
.source("http://localhost/")
173-
//TODO this is required now because the message deserializer implictly set default values
174-
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
175-
.time(time)
176171
.extension("someint", "10")
177172
.build()
178173
.unwrap();
@@ -183,7 +178,6 @@ mod tests {
183178
.header("ce-type", "example.test")
184179
.header("ce-source", "http://localhost/")
185180
.header("ce-someint", "10")
186-
.header("ce-time", time.to_rfc3339())
187181
.to_http_parts();
188182

189183
let resp = req.to_event(web::Payload(payload)).await.unwrap();
@@ -192,16 +186,12 @@ mod tests {
192186

193187
#[actix_rt::test]
194188
async fn test_request_with_full_data() {
195-
let time = Utc::now();
196189
let j = json!({"hello": "world"});
197190

198191
let expected = EventBuilderV10::new()
199192
.id("0001")
200193
.ty("example.test")
201194
.source("http://localhost")
202-
//TODO this is required now because the message deserializer implictly set default values
203-
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
204-
.time(time)
205195
.data("application/json", j.to_string().into_bytes())
206196
.extension("someint", "10")
207197
.build()
@@ -213,12 +203,43 @@ mod tests {
213203
.header("ce-type", "example.test")
214204
.header("ce-source", "http://localhost")
215205
.header("ce-someint", "10")
216-
.header("ce-time", time.to_rfc3339())
217206
.header("content-type", "application/json")
218207
.set_json(&j)
219208
.to_http_parts();
220209

221210
let resp = req.to_event(web::Payload(payload)).await.unwrap();
222211
assert_eq!(expected, resp);
223212
}
213+
214+
#[actix_rt::test]
215+
async fn test_structured_request_with_full_data() {
216+
let j = json!({"hello": "world"});
217+
let payload = json!({
218+
"specversion": "1.0",
219+
"id": "0001",
220+
"type": "example.test",
221+
"source": "http://localhost",
222+
"someint": "10",
223+
"datacontenttype": "application/json",
224+
"data": j
225+
});
226+
let bytes = serde_json::to_string(&payload).expect("Failed to serialize test data to json");
227+
228+
let expected = EventBuilderV10::new()
229+
.id("0001")
230+
.ty("example.test")
231+
.source("http://localhost")
232+
.data("application/json", j)
233+
.extension("someint", "10")
234+
.build()
235+
.unwrap();
236+
237+
let (req, payload) = test::TestRequest::post()
238+
.header("content-type", "application/cloudevents+json")
239+
.set_payload(bytes)
240+
.to_http_parts();
241+
242+
let resp = req.to_event(web::Payload(payload)).await.unwrap();
243+
assert_eq!(expected, resp);
244+
}
224245
}

0 commit comments

Comments
 (0)