Fix tests after refactoring URIRef (#116)

Signed-off-by: Jens Reimann <jreimann@redhat.com>
This commit is contained in:
Jens Reimann 2021-04-21 11:09:24 +02:00 committed by GitHub
parent 62ca1cd7da
commit dfe2bcce13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 74 additions and 48 deletions

View File

@ -144,12 +144,10 @@ mod private {
mod tests { mod tests {
use super::*; use super::*;
use actix_web::test; use actix_web::test;
use url::Url;
use chrono::Utc; use chrono::Utc;
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json; use serde_json::json;
use std::str::FromStr;
#[actix_rt::test] #[actix_rt::test]
async fn test_request() { async fn test_request() {
@ -186,7 +184,7 @@ mod tests {
let expected = EventBuilderV10::new() let expected = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
//TODO this is required now because the message deserializer implictly set default values //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) // As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
.time(time) .time(time)

View File

@ -107,21 +107,19 @@ mod private {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use url::Url;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::test; use actix_web::test;
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use futures::TryStreamExt; use futures::TryStreamExt;
use serde_json::json; use serde_json::json;
use std::str::FromStr;
#[actix_rt::test] #[actix_rt::test]
async fn test_response() { async fn test_response() {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost/").unwrap()) .source("http://localhost/")
.extension("someint", "10") .extension("someint", "10")
.build() .build()
.unwrap(); .unwrap();
@ -164,7 +162,7 @@ mod tests {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.data("application/json", j.clone()) .data("application/json", j.clone())
.extension("someint", "10") .extension("someint", "10")
.build() .build()
@ -193,7 +191,7 @@ mod tests {
); );
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()

View File

@ -95,7 +95,6 @@ mod tests {
use cloudevents::message::StructuredDeserializer; use cloudevents::message::StructuredDeserializer;
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json; use serde_json::json;
use url::Url;
#[tokio::test] #[tokio::test]
async fn test_request() { async fn test_request() {
@ -112,7 +111,7 @@ mod tests {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost/").unwrap()) .source("http://localhost/")
.extension("someint", "10") .extension("someint", "10")
.build() .build()
.unwrap(); .unwrap();
@ -147,7 +146,7 @@ mod tests {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost/")
.data("application/json", j.clone()) .data("application/json", j.clone())
.extension("someint", "10") .extension("someint", "10")
.build() .build()
@ -173,7 +172,7 @@ mod tests {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.data("application/json", j.clone()) .data("application/json", j.clone())
.extension("someint", "10") .extension("someint", "10")
.build() .build()

View File

@ -139,8 +139,6 @@ mod tests {
use chrono::Utc; use chrono::Utc;
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json; use serde_json::json;
use std::str::FromStr;
use url::Url;
#[tokio::test] #[tokio::test]
async fn test_response() { async fn test_response() {
@ -162,7 +160,7 @@ mod tests {
//TODO this is required now because the message deserializer implictly set default values //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) // As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
.time(time) .time(time)
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.extension("someint", "10") .extension("someint", "10")
.build() .build()
.unwrap(); .unwrap();
@ -204,7 +202,7 @@ mod tests {
//TODO this is required now because the message deserializer implictly set default values //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) // As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
.time(time) .time(time)
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost/")
.data("application/json", j.to_string().into_bytes()) .data("application/json", j.to_string().into_bytes())
.extension("someint", "10") .extension("someint", "10")
.build() .build()
@ -234,7 +232,7 @@ mod tests {
//TODO this is required now because the message deserializer implictly set default values //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) // As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
.time(time) .time(time)
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.data("application/json", j.clone()) .data("application/json", j.clone())
.extension("someint", "10") .extension("someint", "10")
.build() .build()

View File

@ -42,13 +42,11 @@ async fn create_event(headers: HeaderMap, body: bytes::Bytes) -> Result<Event, R
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::to_event; use super::to_event;
use url::Url;
use warp::test; use warp::test;
use chrono::Utc; use chrono::Utc;
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json; use serde_json::json;
use std::str::FromStr;
#[tokio::test] #[tokio::test]
async fn test_request() { async fn test_request() {
@ -110,7 +108,7 @@ mod tests {
let expected = EventBuilderV10::new() let expected = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.time(time) .time(time)
.data("application/json", j.to_string().into_bytes()) .data("application/json", j.to_string().into_bytes())
.extension("someint", "10") .extension("someint", "10")

View File

@ -31,7 +31,7 @@
//! let routes = warp::any().map(|| { //! let routes = warp::any().map(|| {
//! let event = EventBuilderV10::new() //! let event = EventBuilderV10::new()
//! .id("1") //! .id("1")
//! .source(url::Url::parse("url://example_response/").unwrap()) //! .source("url://example_response/")
//! .ty("example.ce") //! .ty("example.ce")
//! .data( //! .data(
//! mime::APPLICATION_JSON.to_string(), //! mime::APPLICATION_JSON.to_string(),

View File

@ -31,15 +31,13 @@ mod tests {
use cloudevents::{EventBuilder, EventBuilderV10}; use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json; use serde_json::json;
use std::str::FromStr;
use url::Url;
#[test] #[test]
fn test_response() { fn test_response() {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost/").unwrap()) .source("http://localhost/")
.extension("someint", "10") .extension("someint", "10")
.build() .build()
.unwrap(); .unwrap();
@ -79,7 +77,7 @@ mod tests {
let input = EventBuilderV10::new() let input = EventBuilderV10::new()
.id("0001") .id("0001")
.ty("example.test") .ty("example.test")
.source(Url::from_str("http://localhost").unwrap()) .source("http://localhost")
.data("application/json", j.clone()) .data("application/json", j.clone())
.extension("someint", "10") .extension("someint", "10")
.build() .build()
@ -105,7 +103,7 @@ mod tests {
); );
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()

View File

@ -44,7 +44,7 @@ pub enum Error {
source: chrono::ParseError, source: chrono::ParseError,
}, },
#[snafu(display( #[snafu(display(
"Error while setting attribute '{}' with uri/uriref type: {}", "Error while setting attribute '{}' with uri type: {}",
attribute_name, attribute_name,
source source
))] ))]
@ -52,4 +52,9 @@ pub enum Error {
attribute_name: &'static str, attribute_name: &'static str,
source: url::ParseError, source: url::ParseError,
}, },
#[snafu(display(
"Invalid value setting attribute '{}' with uriref type",
attribute_name,
))]
InvalidUriRefError { attribute_name: &'static str },
} }

View File

@ -251,7 +251,7 @@ mod tests {
assert_eq!( assert_eq!(
( (
"source", "source",
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap()) AttributeValue::URIRef(&"https://example.net".to_string())
), ),
b.next().unwrap() b.next().unwrap()
); );

View File

@ -31,7 +31,14 @@ impl EventBuilder {
} }
pub fn source(mut self, source: impl Into<String>) -> Self { pub fn source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into()); let source = source.into();
if source.is_empty() {
self.error = Some(EventBuilderError::InvalidUriRefError {
attribute_name: "source",
});
} else {
self.source = Some(source);
}
self self
} }

View File

@ -228,7 +228,7 @@ mod tests {
let a = Attributes { let a = Attributes {
id: String::from("1"), id: String::from("1"),
ty: String::from("someType"), ty: String::from("someType"),
source: Url::parse("https://example.net").unwrap(), source: "https://example.net".into(),
datacontenttype: None, datacontenttype: None,
dataschema: None, dataschema: None,
subject: None, subject: None,
@ -252,7 +252,7 @@ mod tests {
assert_eq!( assert_eq!(
( (
"source", "source",
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap()) AttributeValue::URIRef(&"https://example.net".to_string())
), ),
b.next().unwrap() b.next().unwrap()
); );

View File

@ -31,7 +31,14 @@ impl EventBuilder {
} }
pub fn source(mut self, source: impl Into<String>) -> Self { pub fn source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into()); let source = source.into();
if source.is_empty() {
self.error = Some(EventBuilderError::InvalidUriRefError {
attribute_name: "source",
});
} else {
self.source = Some(source);
}
self self
} }

View File

@ -12,7 +12,7 @@ use url::Url;
#[test] #[test]
fn build_event() { fn build_event() {
let id = "aaa"; let id = "aaa";
let source = Url::parse("http://localhost:8080").unwrap(); let source = "http://localhost:8080";
let ty = "bbb"; let ty = "bbb";
let subject = "francesco"; let subject = "francesco";
let time: DateTime<Utc> = Utc::now(); let time: DateTime<Utc> = Utc::now();
@ -52,6 +52,16 @@ fn build_event() {
assert_eq!(data, event_data); assert_eq!(data, event_data);
} }
#[test]
fn source_valid_relative_url() {
let res = EventBuilderV03::new()
.id("id1")
.source("/source") // relative URL
.ty("type")
.build();
assert_match_pattern!(res, Ok(_));
}
#[test] #[test]
fn build_missing_id() { fn build_missing_id() {
let res = EventBuilderV03::new() let res = EventBuilderV03::new()
@ -70,9 +80,8 @@ fn source_invalid_url() {
let res = EventBuilderV03::new().source("").build(); let res = EventBuilderV03::new().source("").build();
assert_match_pattern!( assert_match_pattern!(
res, res,
Err(EventBuilderError::ParseUrlError { Err(EventBuilderError::InvalidUriRefError {
attribute_name: "source", attribute_name: "source",
..
}) })
); );
} }

View File

@ -12,7 +12,7 @@ use url::Url;
#[test] #[test]
fn build_event() { fn build_event() {
let id = "aaa"; let id = "aaa";
let source = Url::parse("http://localhost:8080").unwrap(); let source = "http://localhost:8080";
let ty = "bbb"; let ty = "bbb";
let subject = "francesco"; let subject = "francesco";
let time: DateTime<Utc> = Utc::now(); let time: DateTime<Utc> = Utc::now();
@ -52,6 +52,16 @@ fn build_event() {
assert_eq!(data, event_data); assert_eq!(data, event_data);
} }
#[test]
fn source_valid_relative_url() {
let res = EventBuilderV10::new()
.id("id1")
.source("/source") // relative URL
.ty("type")
.build();
assert_match_pattern!(res, Ok(_));
}
#[test] #[test]
fn build_missing_id() { fn build_missing_id() {
let res = EventBuilderV10::new() let res = EventBuilderV10::new()
@ -70,9 +80,8 @@ fn source_invalid_url() {
let res = EventBuilderV10::new().source("").build(); let res = EventBuilderV10::new().source("").build();
assert_match_pattern!( assert_match_pattern!(
res, res,
Err(EventBuilderError::ParseUrlError { Err(EventBuilderError::InvalidUriRefError {
attribute_name: "source", attribute_name: "source",
..
}) })
); );
} }

View File

@ -7,7 +7,7 @@ use url::Url;
pub fn minimal() -> Event { pub fn minimal() -> Event {
EventBuilderV03::new() EventBuilderV03::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.build() .build()
.unwrap() .unwrap()
@ -29,7 +29,7 @@ pub fn full_no_data() -> Event {
EventBuilderV03::new() EventBuilderV03::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -65,7 +65,7 @@ pub fn full_json_data() -> Event {
EventBuilderV03::new() EventBuilderV03::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -131,7 +131,7 @@ pub fn full_xml_string_data() -> Event {
EventBuilderV03::new() EventBuilderV03::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -150,7 +150,7 @@ pub fn full_xml_binary_data() -> Event {
EventBuilderV03::new() EventBuilderV03::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())

View File

@ -6,7 +6,7 @@ use url::Url;
pub fn minimal() -> Event { pub fn minimal() -> Event {
EventBuilderV10::new() EventBuilderV10::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.build() .build()
.unwrap() .unwrap()
@ -28,7 +28,7 @@ pub fn full_no_data() -> Event {
EventBuilderV10::new() EventBuilderV10::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -64,7 +64,7 @@ pub fn full_json_data() -> Event {
EventBuilderV10::new() EventBuilderV10::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -129,7 +129,7 @@ pub fn full_xml_string_data() -> Event {
EventBuilderV10::new() EventBuilderV10::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())
@ -148,7 +148,7 @@ pub fn full_xml_binary_data() -> Event {
EventBuilderV10::new() EventBuilderV10::new()
.id(id()) .id(id())
.source(Url::parse(source().as_ref()).unwrap()) .source(source())
.ty(ty()) .ty(ty())
.subject(subject()) .subject(subject())
.time(time()) .time(time())