Skip to content

Commit 8a2dde9

Browse files
committed
update Rust templates to be a more accurate echo
1 parent 431d24e commit 8a2dde9

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

templates/rust/cloudevents/src/handler.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@ pub async fn handle(
1111
) -> Result<Event, actix_web::Error> {
1212
info!("event: {}", event);
1313

14-
let input = match event.data() {
15-
Some(Data::Binary(v)) => from_slice(v)?,
16-
Some(Data::String(v)) => from_str(v)?,
17-
Some(Data::Json(v)) => v.to_owned(),
18-
None => json!({ "name": config.name }),
14+
// Extract message from event data, or use default
15+
let message = match event.data() {
16+
Some(Data::Binary(v)) => {
17+
let input: serde_json::Value = from_slice(v)?;
18+
input.get("message")
19+
.and_then(|v| v.as_str())
20+
.unwrap_or(&config.name)
21+
.to_string()
22+
},
23+
Some(Data::String(v)) => {
24+
let input: serde_json::Value = from_str(v)?;
25+
input.get("message")
26+
.and_then(|v| v.as_str())
27+
.unwrap_or(&config.name)
28+
.to_string()
29+
},
30+
Some(Data::Json(v)) => {
31+
v.get("message")
32+
.and_then(|v| v.as_str())
33+
.unwrap_or(&config.name)
34+
.to_string()
35+
},
36+
None => config.name.clone(),
1937
};
2038

2139
EventBuilderV10::from(event)
2240
.source("func://handler")
23-
.ty("func.example")
24-
.data("application/json", json!({ "hello": input["name"] }))
41+
.ty("func.echo")
42+
.data("application/json", json!({ "message": message }))
2543
.build()
2644
.map_err(ErrorInternalServerError)
2745
}
@@ -37,11 +55,11 @@ mod tests {
3755
#[actix_rt::test]
3856
async fn valid_input() {
3957
let mut input = Event::default();
40-
input.set_data("application/json", json!({"name": "bootsy"}));
58+
input.set_data("application/json", json!({"message": "test-echo"}));
4159
let resp = handle(input, config()).await;
4260
assert!(resp.is_ok());
4361
match resp.unwrap().data() {
44-
Some(Data::Json(output)) => assert_eq!("bootsy", output["hello"]),
62+
Some(Data::Json(output)) => assert_eq!("test-echo", output["message"]),
4563
_ => panic!(),
4664
}
4765
}
@@ -51,7 +69,7 @@ mod tests {
5169
let resp = handle(Event::default(), config()).await;
5270
assert!(resp.is_ok());
5371
match resp.unwrap().data() {
54-
Some(Data::Json(output)) => assert_eq!("world", output["hello"]),
72+
Some(Data::Json(output)) => assert_eq!("world", output["message"]),
5573
_ => panic!(),
5674
}
5775
}

templates/rust/http/src/handler.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ use log::info;
66
pub async fn index(req: HttpRequest, config: Data<HandlerConfig>) -> HttpResponse {
77
info!("{:#?}", req);
88
if req.method() == Method::GET {
9-
HttpResponse::Ok().body(format!("Hello {}!\n", config.name))
9+
// Echo back the query string if present, otherwise return greeting
10+
let query = req.query_string();
11+
if !query.is_empty() {
12+
HttpResponse::Ok().body(query.to_string())
13+
} else {
14+
HttpResponse::Ok().body(format!("Hello {}!\n", config.name))
15+
}
1016
} else {
1117
HttpResponse::Ok().body(format!("Thanks {}!\n", config.name))
1218
}
@@ -32,6 +38,19 @@ mod tests {
3238
);
3339
}
3440

41+
#[actix_rt::test]
42+
async fn get_with_query() {
43+
let req = TestRequest::get()
44+
.uri("/?test=param&message=hello")
45+
.to_http_request();
46+
let resp = index(req, config()).await;
47+
assert_eq!(resp.status(), http::StatusCode::OK);
48+
assert_eq!(
49+
&Bytes::from("test=param&message=hello"),
50+
to_bytes(resp.into_body()).await.unwrap().as_ref()
51+
);
52+
}
53+
3554
#[actix_rt::test]
3655
async fn post() {
3756
let req = TestRequest::post().to_http_request();

0 commit comments

Comments
 (0)