Skip to content

Commit 5cc2fdd

Browse files
dejanbjcrossley3
authored andcommitted
Use defined fixtures in tests
Signed-off-by: Dejan Bosanac <[email protected]>
1 parent 733d568 commit 5cc2fdd

File tree

10 files changed

+150
-287
lines changed

10 files changed

+150
-287
lines changed

src/binding/actix/server_request.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ mod tests {
7777
use actix_web::test;
7878

7979
use crate::test::fixtures;
80-
use crate::{EventBuilder, EventBuilderV10};
8180
use serde_json::json;
8281
#[actix_rt::test]
8382
async fn test_request() {
84-
let mut expected = fixtures::v10::minimal();
85-
expected.set_extension("someint", "10");
83+
let expected = fixtures::v10::minimal_string_extension();
8684

8785
let (req, payload) = test::TestRequest::post()
8886
.header("ce-specversion", "1.0")
@@ -98,25 +96,20 @@ mod tests {
9896

9997
#[actix_rt::test]
10098
async fn test_request_with_full_data() {
101-
let j = json!({"hello": "world"});
102-
103-
let expected = EventBuilderV10::new()
104-
.id("0001")
105-
.ty("example.test")
106-
.source("http://localhost")
107-
.data("application/json", j.to_string().into_bytes())
108-
.extension("someint", "10")
109-
.build()
110-
.unwrap();
99+
let expected = fixtures::v10::full_binary_json_data_string_extension();
111100

112101
let (req, payload) = test::TestRequest::post()
113102
.header("ce-specversion", "1.0")
114103
.header("ce-id", "0001")
115-
.header("ce-type", "example.test")
116-
.header("ce-source", "http://localhost")
117-
.header("ce-someint", "10")
104+
.header("ce-type", "test_event.test_application")
105+
.header("ce-subject", "cloudevents-sdk")
106+
.header("ce-source", "http://localhost/")
107+
.header("ce-time", fixtures::time().to_rfc3339())
108+
.header("ce-string_ex", "val")
109+
.header("ce-int_ex", "10")
110+
.header("ce-bool_ex", "true")
118111
.header("content-type", "application/json")
119-
.set_json(&j)
112+
.set_json(&fixtures::json_data())
120113
.to_http_parts();
121114

122115
let resp = req.to_event(web::Payload(payload)).await.unwrap();
@@ -125,26 +118,22 @@ mod tests {
125118

126119
#[actix_rt::test]
127120
async fn test_structured_request_with_full_data() {
128-
let j = json!({"hello": "world"});
129121
let payload = json!({
130122
"specversion": "1.0",
131123
"id": "0001",
132-
"type": "example.test",
133-
"source": "http://localhost",
134-
"someint": "10",
124+
"type": "test_event.test_application",
125+
"subject": "cloudevents-sdk",
126+
"source": "http://localhost/",
127+
"time": fixtures::time().to_rfc3339(),
128+
"string_ex": "val",
129+
"int_ex": "10",
130+
"bool_ex": "true",
135131
"datacontenttype": "application/json",
136-
"data": j
132+
"data": fixtures::json_data()
137133
});
138134
let bytes = serde_json::to_string(&payload).expect("Failed to serialize test data to json");
139135

140-
let expected = EventBuilderV10::new()
141-
.id("0001")
142-
.ty("example.test")
143-
.source("http://localhost")
144-
.data("application/json", j)
145-
.extension("someint", "10")
146-
.build()
147-
.unwrap();
136+
let expected = fixtures::v10::full_json_data_string_extension();
148137

149138
let (req, payload) = test::TestRequest::post()
150139
.header("content-type", "application/cloudevents+json")

src/binding/actix/server_response.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,14 @@ mod private {
7171
mod tests {
7272
use super::*;
7373

74-
use crate::{EventBuilder, EventBuilderV10};
74+
use crate::test::fixtures;
7575
use actix_web::http::StatusCode;
7676
use actix_web::test;
7777
use futures::TryStreamExt;
78-
use serde_json::json;
7978

8079
#[actix_rt::test]
8180
async fn test_response() {
82-
let input = EventBuilderV10::new()
83-
.id("0001")
84-
.ty("example.test")
85-
.source("http://localhost/")
86-
.extension("someint", "10")
87-
.build()
88-
.unwrap();
81+
let input = fixtures::v10::minimal_string_extension();
8982

9083
let resp = HttpResponseBuilder::new(StatusCode::OK)
9184
.event(input)
@@ -106,7 +99,7 @@ mod tests {
10699
);
107100
assert_eq!(
108101
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
109-
"example.test"
102+
"test_event.test_application"
110103
);
111104
assert_eq!(
112105
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
@@ -120,16 +113,7 @@ mod tests {
120113

121114
#[actix_rt::test]
122115
async fn test_response_with_full_data() {
123-
let j = json!({"hello": "world"});
124-
125-
let input = EventBuilderV10::new()
126-
.id("0001")
127-
.ty("example.test")
128-
.source("http://localhost")
129-
.data("application/json", j.clone())
130-
.extension("someint", "10")
131-
.build()
132-
.unwrap();
116+
let input = fixtures::v10::full_binary_json_data_string_extension();
133117

134118
let mut resp = HttpResponseBuilder::new(StatusCode::OK)
135119
.event(input)
@@ -150,11 +134,11 @@ mod tests {
150134
);
151135
assert_eq!(
152136
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
153-
"example.test"
137+
"test_event.test_application"
154138
);
155139
assert_eq!(
156140
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
157-
"http://localhost"
141+
"http://localhost/"
158142
);
159143
assert_eq!(
160144
resp.headers()
@@ -165,13 +149,13 @@ mod tests {
165149
"application/json"
166150
);
167151
assert_eq!(
168-
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
152+
resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
169153
"10"
170154
);
171155

172156
let bytes = test::load_stream(resp.take_body().into_stream())
173157
.await
174158
.unwrap();
175-
assert_eq!(j.to_string().as_bytes(), bytes.as_ref())
159+
assert_eq!(fixtures::json_data_binary(), bytes.as_ref())
176160
}
177161
}

src/binding/axum/extract.rs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,21 @@ mod tests {
4747
use super::*;
4848
use axum::body::Body;
4949
use axum::http::{self, Request, StatusCode};
50-
use chrono::Utc;
51-
use serde_json::json;
5250

53-
use crate::{EventBuilder, EventBuilderV10};
51+
use crate::test::fixtures;
5452

5553
#[tokio::test]
5654
async fn axum_test_request() {
57-
let time = Utc::now();
58-
let expected = EventBuilderV10::new()
59-
.id("0001")
60-
.ty("example.test")
61-
.source("http://localhost/")
62-
.time(time)
63-
.extension("someint", "10")
64-
.build()
65-
.unwrap();
55+
let expected = fixtures::v10::minimal_string_extension();
6656

6757
let mut request = RequestParts::new(
6858
Request::builder()
6959
.method(http::Method::POST)
7060
.header("ce-specversion", "1.0")
7161
.header("ce-id", "0001")
72-
.header("ce-type", "example.test")
62+
.header("ce-type", "test_event.test_application")
7363
.header("ce-source", "http://localhost/")
7464
.header("ce-someint", "10")
75-
.header("ce-time", time.to_rfc3339())
7665
.body(Body::empty())
7766
.unwrap(),
7867
);
@@ -84,8 +73,6 @@ mod tests {
8473

8574
#[tokio::test]
8675
async fn axum_test_bad_request() {
87-
let time = Utc::now();
88-
8976
let mut request = RequestParts::new(
9077
Request::builder()
9178
.method(http::Method::POST)
@@ -94,7 +81,7 @@ mod tests {
9481
.header("ce-type", "example.test")
9582
.header("ce-source", "http://localhost/")
9683
.header("ce-someint", "10")
97-
.header("ce-time", time.to_rfc3339())
84+
.header("ce-time", fixtures::time().to_rfc3339())
9885
.body(Body::empty())
9986
.unwrap(),
10087
);
@@ -109,30 +96,22 @@ mod tests {
10996

11097
#[tokio::test]
11198
async fn axum_test_request_with_full_data() {
112-
let time = Utc::now();
113-
let j = json!({"hello": "world"});
114-
115-
let expected = EventBuilderV10::new()
116-
.id("0001")
117-
.ty("example.test")
118-
.source("http://localhost")
119-
.time(time)
120-
.data("application/json", j.to_string().into_bytes())
121-
.extension("someint", "10")
122-
.build()
123-
.unwrap();
99+
let expected = fixtures::v10::full_binary_json_data_string_extension();
124100

125101
let mut request = RequestParts::new(
126102
Request::builder()
127103
.method(http::Method::POST)
128104
.header("ce-specversion", "1.0")
129105
.header("ce-id", "0001")
130-
.header("ce-type", "example.test")
131-
.header("ce-source", "http://localhost")
132-
.header("ce-someint", "10")
133-
.header("ce-time", time.to_rfc3339())
106+
.header("ce-type", "test_event.test_application")
107+
.header("ce-source", "http://localhost/")
108+
.header("ce-subject", "cloudevents-sdk")
134109
.header("content-type", "application/json")
135-
.body(Body::from(serde_json::to_vec(&j).unwrap()))
110+
.header("ce-string_ex", "val")
111+
.header("ce-int_ex", "10")
112+
.header("ce-bool_ex", "true")
113+
.header("ce-time", &fixtures::time().to_rfc3339())
114+
.body(Body::from(fixtures::json_data_binary()))
136115
.unwrap(),
137116
);
138117

src/binding/axum/response.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@ impl IntoResponse for Event {
2525
#[cfg(test)]
2626
mod tests {
2727
use super::*;
28-
use serde_json::json;
2928

30-
use crate::{EventBuilder, EventBuilderV10};
29+
use crate::test::fixtures;
3130

3231
#[test]
3332
fn axum_test_response() {
34-
let input = EventBuilderV10::new()
35-
.id("0001")
36-
.ty("example.test")
37-
.source("http://localhost/")
38-
.extension("someint", "10")
39-
.build()
40-
.unwrap();
33+
let input = fixtures::v10::minimal_string_extension();
4134

4235
let resp = input.into_response();
4336

@@ -55,7 +48,7 @@ mod tests {
5548
);
5649
assert_eq!(
5750
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
58-
"example.test"
51+
"test_event.test_application"
5952
);
6053
assert_eq!(
6154
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
@@ -69,16 +62,7 @@ mod tests {
6962

7063
#[tokio::test]
7164
async fn axum_test_response_with_full_data() {
72-
let j = json!({"hello": "world"});
73-
74-
let input = EventBuilderV10::new()
75-
.id("0001")
76-
.ty("example.test")
77-
.source("http://localhost")
78-
.data("application/json", j.clone())
79-
.extension("someint", "10")
80-
.build()
81-
.unwrap();
65+
let input = fixtures::v10::full_binary_json_data_string_extension();
8266

8367
let resp = input.into_response();
8468

@@ -96,11 +80,11 @@ mod tests {
9680
);
9781
assert_eq!(
9882
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
99-
"example.test"
83+
"test_event.test_application"
10084
);
10185
assert_eq!(
10286
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
103-
"http://localhost"
87+
"http://localhost/"
10488
);
10589
assert_eq!(
10690
resp.headers()
@@ -111,13 +95,13 @@ mod tests {
11195
"application/json"
11296
);
11397
assert_eq!(
114-
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
98+
resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
11599
"10"
116100
);
117101

118102
let (_, body) = resp.into_parts();
119103
let body = hyper::body::to_bytes(body).await.unwrap();
120104

121-
assert_eq!(j.to_string().as_bytes(), body);
105+
assert_eq!(fixtures::json_data_binary(), body);
122106
}
123107
}

0 commit comments

Comments
 (0)