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 {
use super::*;
use actix_web::test;
use url::Url;
use chrono::Utc;
use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json;
use std::str::FromStr;
#[actix_rt::test]
async fn test_request() {
@ -186,7 +184,7 @@ mod tests {
let expected = EventBuilderV10::new()
.id("0001")
.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
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
.time(time)

View File

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

View File

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

View File

@ -139,8 +139,6 @@ mod tests {
use chrono::Utc;
use cloudevents::{EventBuilder, EventBuilderV10};
use serde_json::json;
use std::str::FromStr;
use url::Url;
#[tokio::test]
async fn test_response() {
@ -162,7 +160,7 @@ mod tests {
//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(Url::from_str("http://localhost").unwrap())
.source("http://localhost")
.extension("someint", "10")
.build()
.unwrap();
@ -204,7 +202,7 @@ mod tests {
//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(Url::from_str("http://localhost").unwrap())
.source("http://localhost/")
.data("application/json", j.to_string().into_bytes())
.extension("someint", "10")
.build()
@ -234,7 +232,7 @@ mod tests {
//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(Url::from_str("http://localhost").unwrap())
.source("http://localhost")
.data("application/json", j.clone())
.extension("someint", "10")
.build()

View File

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

View File

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

View File

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

View File

@ -44,7 +44,7 @@ pub enum Error {
source: chrono::ParseError,
},
#[snafu(display(
"Error while setting attribute '{}' with uri/uriref type: {}",
"Error while setting attribute '{}' with uri type: {}",
attribute_name,
source
))]
@ -52,4 +52,9 @@ pub enum Error {
attribute_name: &'static str,
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!(
(
"source",
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
AttributeValue::URIRef(&"https://example.net".to_string())
),
b.next().unwrap()
);

View File

@ -31,7 +31,14 @@ impl EventBuilder {
}
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
}

View File

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

View File

@ -31,7 +31,14 @@ impl EventBuilder {
}
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
}

View File

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

View File

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

View File

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

View File

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