Use defined fixtures in tests
Signed-off-by: Dejan Bosanac <dejan@sensatic.net>
This commit is contained in:
parent
733d568591
commit
5cc2fddddd
|
@ -77,12 +77,10 @@ mod tests {
|
||||||
use actix_web::test;
|
use actix_web::test;
|
||||||
|
|
||||||
use crate::test::fixtures;
|
use crate::test::fixtures;
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request() {
|
async fn test_request() {
|
||||||
let mut expected = fixtures::v10::minimal();
|
let expected = fixtures::v10::minimal_string_extension();
|
||||||
expected.set_extension("someint", "10");
|
|
||||||
|
|
||||||
let (req, payload) = test::TestRequest::post()
|
let (req, payload) = test::TestRequest::post()
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
|
@ -98,25 +96,20 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_request_with_full_data() {
|
async fn test_request_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
let expected = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.to_string().into_bytes())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (req, payload) = test::TestRequest::post()
|
let (req, payload) = test::TestRequest::post()
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
.header("ce-id", "0001")
|
.header("ce-id", "0001")
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "test_event.test_application")
|
||||||
.header("ce-source", "http://localhost")
|
.header("ce-subject", "cloudevents-sdk")
|
||||||
.header("ce-someint", "10")
|
.header("ce-source", "http://localhost/")
|
||||||
|
.header("ce-time", fixtures::time().to_rfc3339())
|
||||||
|
.header("ce-string_ex", "val")
|
||||||
|
.header("ce-int_ex", "10")
|
||||||
|
.header("ce-bool_ex", "true")
|
||||||
.header("content-type", "application/json")
|
.header("content-type", "application/json")
|
||||||
.set_json(&j)
|
.set_json(&fixtures::json_data())
|
||||||
.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();
|
||||||
|
@ -125,26 +118,22 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_structured_request_with_full_data() {
|
async fn test_structured_request_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
let payload = json!({
|
let payload = json!({
|
||||||
"specversion": "1.0",
|
"specversion": "1.0",
|
||||||
"id": "0001",
|
"id": "0001",
|
||||||
"type": "example.test",
|
"type": "test_event.test_application",
|
||||||
"source": "http://localhost",
|
"subject": "cloudevents-sdk",
|
||||||
"someint": "10",
|
"source": "http://localhost/",
|
||||||
|
"time": fixtures::time().to_rfc3339(),
|
||||||
|
"string_ex": "val",
|
||||||
|
"int_ex": "10",
|
||||||
|
"bool_ex": "true",
|
||||||
"datacontenttype": "application/json",
|
"datacontenttype": "application/json",
|
||||||
"data": j
|
"data": fixtures::json_data()
|
||||||
});
|
});
|
||||||
let bytes = serde_json::to_string(&payload).expect("Failed to serialize test data to json");
|
let bytes = serde_json::to_string(&payload).expect("Failed to serialize test data to json");
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
let expected = fixtures::v10::full_json_data_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j)
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (req, payload) = test::TestRequest::post()
|
let (req, payload) = test::TestRequest::post()
|
||||||
.header("content-type", "application/cloudevents+json")
|
.header("content-type", "application/cloudevents+json")
|
||||||
|
|
|
@ -71,21 +71,14 @@ mod private {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::test;
|
use actix_web::test;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_response() {
|
async fn test_response() {
|
||||||
let input = EventBuilderV10::new()
|
let input = fixtures::v10::minimal_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
let resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
.event(input)
|
.event(input)
|
||||||
|
@ -106,7 +99,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
|
@ -120,16 +113,7 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_response_with_full_data() {
|
async fn test_response_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
let input = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut resp = HttpResponseBuilder::new(StatusCode::OK)
|
let mut resp = HttpResponseBuilder::new(StatusCode::OK)
|
||||||
.event(input)
|
.event(input)
|
||||||
|
@ -150,11 +134,11 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
"http://localhost"
|
"http://localhost/"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers()
|
resp.headers()
|
||||||
|
@ -165,13 +149,13 @@ mod tests {
|
||||||
"application/json"
|
"application/json"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
|
||||||
"10"
|
"10"
|
||||||
);
|
);
|
||||||
|
|
||||||
let bytes = test::load_stream(resp.take_body().into_stream())
|
let bytes = test::load_stream(resp.take_body().into_stream())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(j.to_string().as_bytes(), bytes.as_ref())
|
assert_eq!(fixtures::json_data_binary(), bytes.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,32 +47,21 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use axum::body::Body;
|
use axum::body::Body;
|
||||||
use axum::http::{self, Request, StatusCode};
|
use axum::http::{self, Request, StatusCode};
|
||||||
use chrono::Utc;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn axum_test_request() {
|
async fn axum_test_request() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::minimal_string_extension();
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.time(time)
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut request = RequestParts::new(
|
let mut request = RequestParts::new(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.method(http::Method::POST)
|
.method(http::Method::POST)
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
.header("ce-id", "0001")
|
.header("ce-id", "0001")
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "test_event.test_application")
|
||||||
.header("ce-source", "http://localhost/")
|
.header("ce-source", "http://localhost/")
|
||||||
.header("ce-someint", "10")
|
.header("ce-someint", "10")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
@ -84,8 +73,6 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn axum_test_bad_request() {
|
async fn axum_test_bad_request() {
|
||||||
let time = Utc::now();
|
|
||||||
|
|
||||||
let mut request = RequestParts::new(
|
let mut request = RequestParts::new(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.method(http::Method::POST)
|
.method(http::Method::POST)
|
||||||
|
@ -94,7 +81,7 @@ 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("ce-time", fixtures::time().to_rfc3339())
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
@ -109,30 +96,22 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn axum_test_request_with_full_data() {
|
async fn axum_test_request_with_full_data() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.time(time)
|
|
||||||
.data("application/json", j.to_string().into_bytes())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut request = RequestParts::new(
|
let mut request = RequestParts::new(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.method(http::Method::POST)
|
.method(http::Method::POST)
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
.header("ce-id", "0001")
|
.header("ce-id", "0001")
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "test_event.test_application")
|
||||||
.header("ce-source", "http://localhost")
|
.header("ce-source", "http://localhost/")
|
||||||
.header("ce-someint", "10")
|
.header("ce-subject", "cloudevents-sdk")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.header("content-type", "application/json")
|
.header("content-type", "application/json")
|
||||||
.body(Body::from(serde_json::to_vec(&j).unwrap()))
|
.header("ce-string_ex", "val")
|
||||||
|
.header("ce-int_ex", "10")
|
||||||
|
.header("ce-bool_ex", "true")
|
||||||
|
.header("ce-time", &fixtures::time().to_rfc3339())
|
||||||
|
.body(Body::from(fixtures::json_data_binary()))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -25,19 +25,12 @@ impl IntoResponse for Event {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn axum_test_response() {
|
fn axum_test_response() {
|
||||||
let input = EventBuilderV10::new()
|
let input = fixtures::v10::minimal_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let resp = input.into_response();
|
let resp = input.into_response();
|
||||||
|
|
||||||
|
@ -55,7 +48,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
|
@ -69,16 +62,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn axum_test_response_with_full_data() {
|
async fn axum_test_response_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
let input = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let resp = input.into_response();
|
let resp = input.into_response();
|
||||||
|
|
||||||
|
@ -96,11 +80,11 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
"http://localhost"
|
"http://localhost/"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers()
|
resp.headers()
|
||||||
|
@ -111,13 +95,13 @@ mod tests {
|
||||||
"application/json"
|
"application/json"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
|
||||||
"10"
|
"10"
|
||||||
);
|
);
|
||||||
|
|
||||||
let (_, body) = resp.into_parts();
|
let (_, body) = resp.into_parts();
|
||||||
let body = hyper::body::to_bytes(body).await.unwrap();
|
let body = hyper::body::to_bytes(body).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(j.to_string().as_bytes(), body);
|
assert_eq!(fixtures::json_data_binary(), body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,22 +173,12 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::binding::rdkafka::kafka_producer_record::MessageRecord;
|
use crate::binding::rdkafka::kafka_producer_record::MessageRecord;
|
||||||
|
|
||||||
|
use crate::test::fixtures;
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::{EventBuilder, EventBuilderV10};
|
||||||
use chrono::Utc;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_record() {
|
fn test_binary_record() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::minimal_string_extension();
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.time(time)
|
|
||||||
.source("http://localhost")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Since there is neither a way provided by rust-rdkafka to convert FutureProducer back into
|
// Since there is neither a way provided by rust-rdkafka to convert FutureProducer back into
|
||||||
// OwnedMessage or BorrowedMessage, nor is there a way to create a BorrowedMessage struct,
|
// OwnedMessage or BorrowedMessage, nor is there a way to create a BorrowedMessage struct,
|
||||||
|
@ -198,9 +188,8 @@ mod tests {
|
||||||
let message_record = MessageRecord::from_event(
|
let message_record = MessageRecord::from_event(
|
||||||
EventBuilderV10::new()
|
EventBuilderV10::new()
|
||||||
.id("0001")
|
.id("0001")
|
||||||
.ty("example.test")
|
.ty("test_event.test_application")
|
||||||
.time(time)
|
.source("http://localhost/")
|
||||||
.source("http://localhost")
|
|
||||||
.extension("someint", "10")
|
.extension("someint", "10")
|
||||||
.build()
|
.build()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
@ -222,30 +211,14 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_structured_record() {
|
fn test_structured_record() {
|
||||||
let j = json!({"hello": "world"});
|
let expected = fixtures::v10::full_json_data_string_extension();
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Since there is neither a way provided by rust-rdkafka to convert FutureProducer back into
|
// Since there is neither a way provided by rust-rdkafka to convert FutureProducer back into
|
||||||
// OwnedMessage or BorrowedMessage, nor is there a way to create a BorrowedMessage struct,
|
// OwnedMessage or BorrowedMessage, nor is there a way to create a BorrowedMessage struct,
|
||||||
// the test uses OwnedMessage instead, which consumes the message instead of borrowing it like
|
// the test uses OwnedMessage instead, which consumes the message instead of borrowing it like
|
||||||
// in the case of BorrowedMessage
|
// in the case of BorrowedMessage
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
let input = expected.clone();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j)
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let serialized_event =
|
let serialized_event =
|
||||||
StructuredDeserializer::deserialize_structured(input, MessageRecord::new()).unwrap();
|
StructuredDeserializer::deserialize_structured(input, MessageRecord::new()).unwrap();
|
||||||
|
|
|
@ -101,8 +101,7 @@ mod tests {
|
||||||
use reqwest_lib as reqwest;
|
use reqwest_lib as reqwest;
|
||||||
|
|
||||||
use crate::message::StructuredDeserializer;
|
use crate::message::StructuredDeserializer;
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_request() {
|
async fn test_request() {
|
||||||
|
@ -110,19 +109,13 @@ mod tests {
|
||||||
let m = mock("POST", "/")
|
let m = mock("POST", "/")
|
||||||
.match_header("ce-specversion", "1.0")
|
.match_header("ce-specversion", "1.0")
|
||||||
.match_header("ce-id", "0001")
|
.match_header("ce-id", "0001")
|
||||||
.match_header("ce-type", "example.test")
|
.match_header("ce-type", "test_event.test_application")
|
||||||
.match_header("ce-source", "http://localhost/")
|
.match_header("ce-source", "http://localhost/")
|
||||||
.match_header("ce-someint", "10")
|
.match_header("ce-someint", "10")
|
||||||
.match_body(Matcher::Missing)
|
.match_body(Matcher::Missing)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
let input = fixtures::v10::minimal_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
client
|
client
|
||||||
|
@ -138,27 +131,22 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_request_with_full_data() {
|
async fn test_request_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
|
|
||||||
let url = mockito::server_url();
|
let url = mockito::server_url();
|
||||||
let m = mock("POST", "/")
|
let m = mock("POST", "/")
|
||||||
.match_header("ce-specversion", "1.0")
|
.match_header("ce-specversion", "1.0")
|
||||||
.match_header("ce-id", "0001")
|
.match_header("ce-id", "0001")
|
||||||
.match_header("ce-type", "example.test")
|
.with_header("ce-type", "test_event.test_application")
|
||||||
.match_header("ce-source", "http://localhost/")
|
.with_header("ce-source", "http://localhost/")
|
||||||
.match_header("content-type", "application/json")
|
.with_header("ce-subject", "cloudevents-sdk")
|
||||||
.match_header("ce-someint", "10")
|
.with_header("content-type", "application/json")
|
||||||
.match_body(Matcher::Exact(j.to_string()))
|
.with_header("ce-string_ex", "val")
|
||||||
|
.with_header("ce-int_ex", "10")
|
||||||
|
.with_header("ce-bool_ex", "true")
|
||||||
|
.with_header("ce-time", &fixtures::time().to_rfc3339())
|
||||||
|
.match_body(Matcher::Exact(fixtures::json_data().to_string()))
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
let input = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
@ -175,16 +163,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_structured_request_with_full_data() {
|
async fn test_structured_request_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
let input = fixtures::v10::full_json_data_string_extension();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let url = mockito::server_url();
|
let url = mockito::server_url();
|
||||||
let m = mock("POST", "/")
|
let m = mock("POST", "/")
|
||||||
|
|
|
@ -45,34 +45,21 @@ mod tests {
|
||||||
use mockito::mock;
|
use mockito::mock;
|
||||||
use reqwest_lib as reqwest;
|
use reqwest_lib as reqwest;
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
use chrono::Utc;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_response() {
|
async fn test_response() {
|
||||||
let time = Utc::now();
|
|
||||||
let url = mockito::server_url();
|
let url = mockito::server_url();
|
||||||
let _m = mock("GET", "/")
|
let _m = mock("GET", "/")
|
||||||
.with_status(200)
|
.with_status(200)
|
||||||
.with_header("ce-specversion", "1.0")
|
.with_header("ce-specversion", "1.0")
|
||||||
.with_header("ce-id", "0001")
|
.with_header("ce-id", "0001")
|
||||||
.with_header("ce-type", "example.test")
|
.with_header("ce-type", "test_event.test_application")
|
||||||
.with_header("ce-source", "http://localhost")
|
.with_header("ce-source", "http://localhost/")
|
||||||
.with_header("ce-someint", "10")
|
.with_header("ce-someint", "10")
|
||||||
.with_header("ce-time", &time.to_rfc3339())
|
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
let expected = fixtures::v10::minimal_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
//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)
|
|
||||||
.source("http://localhost")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
|
@ -89,33 +76,23 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_response_with_full_data() {
|
async fn test_response_with_full_data() {
|
||||||
let time = Utc::now();
|
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
|
|
||||||
let url = mockito::server_url();
|
let url = mockito::server_url();
|
||||||
let _m = mock("GET", "/")
|
let _m = mock("GET", "/")
|
||||||
.with_status(200)
|
.with_status(200)
|
||||||
.with_header("ce-specversion", "1.0")
|
.with_header("ce-specversion", "1.0")
|
||||||
.with_header("ce-id", "0001")
|
.with_header("ce-id", "0001")
|
||||||
.with_header("ce-type", "example.test")
|
.with_header("ce-type", "test_event.test_application")
|
||||||
.with_header("ce-source", "http://localhost/")
|
.with_header("ce-source", "http://localhost/")
|
||||||
|
.with_header("ce-subject", "cloudevents-sdk")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("ce-someint", "10")
|
.with_header("ce-string_ex", "val")
|
||||||
.with_header("ce-time", &time.to_rfc3339())
|
.with_header("ce-int_ex", "10")
|
||||||
.with_body(j.to_string())
|
.with_header("ce-bool_ex", "true")
|
||||||
|
.with_header("ce-time", &fixtures::time().to_rfc3339())
|
||||||
|
.with_body(fixtures::json_data().to_string())
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
let expected = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
//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)
|
|
||||||
.source("http://localhost/")
|
|
||||||
.data("application/json", j.to_string().into_bytes())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
|
@ -132,20 +109,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_structured_response_with_full_data() {
|
async fn test_structured_response_with_full_data() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::full_json_data_string_extension();
|
||||||
|
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
//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)
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let url = mockito::server_url();
|
let url = mockito::server_url();
|
||||||
let _m = mock("GET", "/")
|
let _m = mock("GET", "/")
|
||||||
|
|
|
@ -49,30 +49,19 @@ mod tests {
|
||||||
use super::to_event;
|
use super::to_event;
|
||||||
use warp::test;
|
use warp::test;
|
||||||
|
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
use chrono::Utc;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_request() {
|
async fn test_request() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::minimal_string_extension();
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.time(time)
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let result = test::request()
|
let result = test::request()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
.header("ce-id", "0001")
|
.header("ce-id", "0001")
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "test_event.test_application")
|
||||||
.header("ce-source", "http://localhost/")
|
.header("ce-source", "http://localhost/")
|
||||||
.header("ce-someint", "10")
|
.header("ce-someint", "10")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.filter(&to_event())
|
.filter(&to_event())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -82,8 +71,6 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_bad_request() {
|
async fn test_bad_request() {
|
||||||
let time = Utc::now();
|
|
||||||
|
|
||||||
let result = test::request()
|
let result = test::request()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
.header("ce-specversion", "BAD SPECIFICATION")
|
.header("ce-specversion", "BAD SPECIFICATION")
|
||||||
|
@ -91,7 +78,7 @@ 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("ce-time", fixtures::time().to_rfc3339())
|
||||||
.filter(&to_event())
|
.filter(&to_event())
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -107,29 +94,21 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_request_with_full_data() {
|
async fn test_request_with_full_data() {
|
||||||
let time = Utc::now();
|
let expected = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
let j = json!({"hello": "world"});
|
|
||||||
|
|
||||||
let expected = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.time(time)
|
|
||||||
.data("application/json", j.to_string().into_bytes())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let result = test::request()
|
let result = test::request()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
.header("ce-specversion", "1.0")
|
.header("ce-specversion", "1.0")
|
||||||
.header("ce-id", "0001")
|
.header("ce-id", "0001")
|
||||||
.header("ce-type", "example.test")
|
.header("ce-type", "test_event.test_application")
|
||||||
.header("ce-source", "http://localhost")
|
.header("ce-source", "http://localhost/")
|
||||||
.header("ce-someint", "10")
|
.header("ce-subject", "cloudevents-sdk")
|
||||||
.header("ce-time", time.to_rfc3339())
|
|
||||||
.header("content-type", "application/json")
|
.header("content-type", "application/json")
|
||||||
.json(&j)
|
.header("ce-string_ex", "val")
|
||||||
|
.header("ce-int_ex", "10")
|
||||||
|
.header("ce-bool_ex", "true")
|
||||||
|
.header("ce-time", &fixtures::time().to_rfc3339())
|
||||||
|
.json(&fixtures::json_data())
|
||||||
.filter(&to_event())
|
.filter(&to_event())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -31,18 +31,11 @@ pub fn from_event(event: Event) -> Response {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{EventBuilder, EventBuilderV10};
|
use crate::test::fixtures;
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_response() {
|
fn test_response() {
|
||||||
let input = EventBuilderV10::new()
|
let input = fixtures::v10::minimal_string_extension();
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost/")
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let resp = super::from_event(input);
|
let resp = super::from_event(input);
|
||||||
|
|
||||||
|
@ -60,7 +53,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
|
@ -74,16 +67,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_response_with_full_data() {
|
async fn test_response_with_full_data() {
|
||||||
let j = json!({"hello": "world"});
|
let input = fixtures::v10::full_binary_json_data_string_extension();
|
||||||
|
|
||||||
let input = EventBuilderV10::new()
|
|
||||||
.id("0001")
|
|
||||||
.ty("example.test")
|
|
||||||
.source("http://localhost")
|
|
||||||
.data("application/json", j.clone())
|
|
||||||
.extension("someint", "10")
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let resp = super::from_event(input);
|
let resp = super::from_event(input);
|
||||||
|
|
||||||
|
@ -101,11 +85,11 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
|
||||||
"example.test"
|
"test_event.test_application"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
|
||||||
"http://localhost"
|
"http://localhost/"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers()
|
resp.headers()
|
||||||
|
@ -116,13 +100,13 @@ mod tests {
|
||||||
"application/json"
|
"application/json"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
|
resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
|
||||||
"10"
|
"10"
|
||||||
);
|
);
|
||||||
|
|
||||||
let (_, body) = resp.into_parts();
|
let (_, body) = resp.into_parts();
|
||||||
let body = hyper::body::to_bytes(body).await.unwrap();
|
let body = hyper::body::to_bytes(body).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(j.to_string().as_bytes(), body);
|
assert_eq!(fixtures::json_data_binary(), body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,16 @@ pub fn minimal() -> Event {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn minimal_string_extension() -> Event {
|
||||||
|
EventBuilderV10::new()
|
||||||
|
.id(id())
|
||||||
|
.source(source())
|
||||||
|
.ty(ty())
|
||||||
|
.extension("someint", "10")
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn minimal_json() -> Value {
|
pub fn minimal_json() -> Value {
|
||||||
json!({
|
json!({
|
||||||
"specversion": "1.0",
|
"specversion": "1.0",
|
||||||
|
@ -80,6 +90,44 @@ pub fn full_json_data() -> Event {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn full_json_data_string_extension() -> Event {
|
||||||
|
let (string_ext_name, string_ext_value) = string_extension();
|
||||||
|
let (bool_ext_name, bool_ext_value) = bool_extension();
|
||||||
|
let (int_ext_name, int_ext_value) = int_extension();
|
||||||
|
|
||||||
|
EventBuilderV10::new()
|
||||||
|
.id(id())
|
||||||
|
.source(source())
|
||||||
|
.ty(ty())
|
||||||
|
.subject(subject())
|
||||||
|
.time(time())
|
||||||
|
.extension(&string_ext_name, string_ext_value)
|
||||||
|
.extension(&bool_ext_name, bool_ext_value.to_string())
|
||||||
|
.extension(&int_ext_name, int_ext_value.to_string())
|
||||||
|
.data(json_datacontenttype(), json_data())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn full_binary_json_data_string_extension() -> Event {
|
||||||
|
let (string_ext_name, string_ext_value) = string_extension();
|
||||||
|
let (bool_ext_name, bool_ext_value) = bool_extension();
|
||||||
|
let (int_ext_name, int_ext_value) = int_extension();
|
||||||
|
|
||||||
|
EventBuilderV10::new()
|
||||||
|
.id(id())
|
||||||
|
.source(source())
|
||||||
|
.ty(ty())
|
||||||
|
.subject(subject())
|
||||||
|
.time(time())
|
||||||
|
.extension(&string_ext_name, string_ext_value)
|
||||||
|
.extension(&bool_ext_name, bool_ext_value.to_string())
|
||||||
|
.extension(&int_ext_name, int_ext_value.to_string())
|
||||||
|
.data(json_datacontenttype(), json_data().to_string().into_bytes())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn full_json_data_json() -> Value {
|
pub fn full_json_data_json() -> Value {
|
||||||
let (string_ext_name, string_ext_value) = string_extension();
|
let (string_ext_name, string_ext_value) = string_extension();
|
||||||
let (bool_ext_name, bool_ext_value) = bool_extension();
|
let (bool_ext_name, bool_ext_value) = bool_extension();
|
||||||
|
|
Loading…
Reference in New Issue