Add Actix structured test and clean up a bit
Signed-off-by: Dejan Bosanac <dejan@sensatic.net>
This commit is contained in:
parent
f6b45d1af7
commit
9f430a442c
|
|
@ -126,7 +126,7 @@ impl actix_web::FromRequest for Event {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extention Trait for [`HttpRequest`] which acts as a wrapper for the function [`request_to_event()`].
|
/// Extension Trait for [`HttpRequest`] which acts as a wrapper for the function [`request_to_event()`].
|
||||||
///
|
///
|
||||||
/// This trait is sealed and cannot be implemented for types outside of this crate.
|
/// This trait is sealed and cannot be implemented for types outside of this crate.
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
|
|
@ -160,19 +160,14 @@ mod tests {
|
||||||
use actix_web::test;
|
use actix_web::test;
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::{EventBuilder, EventBuilderV10};
|
||||||
use chrono::Utc;
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request() {
|
async fn test_request() {
|
||||||
let time = Utc::now();
|
|
||||||
let expected = EventBuilderV10::new()
|
let expected = EventBuilderV10::new()
|
||||||
.id("0001")
|
.id("0001")
|
||||||
.ty("example.test")
|
.ty("example.test")
|
||||||
.source("http://localhost/")
|
.source("http://localhost/")
|
||||||
//TODO this is required now because the message deserializer implictly set default values
|
|
||||||
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
|
|
||||||
.time(time)
|
|
||||||
.extension("someint", "10")
|
.extension("someint", "10")
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -183,7 +178,6 @@ mod tests {
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "example.test")
|
||||||
.header("ce-source", "http://localhost/")
|
.header("ce-source", "http://localhost/")
|
||||||
.header("ce-someint", "10")
|
.header("ce-someint", "10")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
let resp = req.to_event(web::Payload(payload)).await.unwrap();
|
let resp = req.to_event(web::Payload(payload)).await.unwrap();
|
||||||
|
|
@ -192,16 +186,12 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request_with_full_data() {
|
async fn test_request_with_full_data() {
|
||||||
let time = Utc::now();
|
|
||||||
let j = json!({"hello": "world"});
|
let j = json!({"hello": "world"});
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
let expected = EventBuilderV10::new()
|
||||||
.id("0001")
|
.id("0001")
|
||||||
.ty("example.test")
|
.ty("example.test")
|
||||||
.source("http://localhost")
|
.source("http://localhost")
|
||||||
//TODO this is required now because the message deserializer implictly set default values
|
|
||||||
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
|
|
||||||
.time(time)
|
|
||||||
.data("application/json", j.to_string().into_bytes())
|
.data("application/json", j.to_string().into_bytes())
|
||||||
.extension("someint", "10")
|
.extension("someint", "10")
|
||||||
.build()
|
.build()
|
||||||
|
|
@ -213,7 +203,6 @@ mod tests {
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "example.test")
|
||||||
.header("ce-source", "http://localhost")
|
.header("ce-source", "http://localhost")
|
||||||
.header("ce-someint", "10")
|
.header("ce-someint", "10")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.header("content-type", "application/json")
|
.header("content-type", "application/json")
|
||||||
.set_json(&j)
|
.set_json(&j)
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
@ -221,4 +210,36 @@ mod tests {
|
||||||
let resp = req.to_event(web::Payload(payload)).await.unwrap();
|
let resp = req.to_event(web::Payload(payload)).await.unwrap();
|
||||||
assert_eq!(expected, resp);
|
assert_eq!(expected, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_structured_request_with_full_data() {
|
||||||
|
let j = json!({"hello": "world"});
|
||||||
|
let payload = json!({
|
||||||
|
"specversion": "1.0",
|
||||||
|
"id": "0001",
|
||||||
|
"type": "example.test",
|
||||||
|
"source": "http://localhost",
|
||||||
|
"someint": "10",
|
||||||
|
"datacontenttype": "application/json",
|
||||||
|
"data": j
|
||||||
|
});
|
||||||
|
let bytes = serde_json::to_string(&payload).expect("Failed to serialize test data to json");
|
||||||
|
|
||||||
|
let expected = EventBuilderV10::new()
|
||||||
|
.id("0001")
|
||||||
|
.ty("example.test")
|
||||||
|
.source("http://localhost")
|
||||||
|
.data("application/json", j)
|
||||||
|
.extension("someint", "10")
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let (req, payload) = test::TestRequest::post()
|
||||||
|
.header("content-type", "application/cloudevents+json")
|
||||||
|
.set_payload(bytes)
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let resp = req.to_event(web::Payload(payload)).await.unwrap();
|
||||||
|
assert_eq!(expected, resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue