Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cloudevents-sdk-actix-web/src/server_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions cloudevents-sdk-actix-web/src/server_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions cloudevents-sdk-reqwest/src/client_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions cloudevents-sdk-reqwest/src/client_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions cloudevents-sdk-warp/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cloudevents-sdk-warp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 3 additions & 5 deletions cloudevents-sdk-warp/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion src/event/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ 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
))]
ParseUrlError {
attribute_name: &'static str,
source: url::ParseError,
},
#[snafu(display(
"Invalid value setting attribute '{}' with uriref type",
attribute_name,
))]
InvalidUriRefError { attribute_name: &'static str },
}
2 changes: 1 addition & 1 deletion src/event/v03/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
9 changes: 8 additions & 1 deletion src/event/v03/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/event/v10/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
);
Expand Down
9 changes: 8 additions & 1 deletion src/event/v10/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 12 additions & 3 deletions tests/builder_v03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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",
..
})
);
}
Expand Down
15 changes: 12 additions & 3 deletions tests/builder_v10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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",
..
})
);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/test_data/v03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
Loading