Skip to content

Commit e16e966

Browse files
committed
Add poem-example
Signed-off-by: Sunli <[email protected]>
1 parent 8928803 commit e16e966

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

.github/workflows/rust_tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,12 @@ jobs:
137137
command: build
138138
toolchain: ${{ matrix.toolchain }}
139139
args: --target ${{ matrix.target }} --manifest-path ./example-projects/axum-example/Cargo.toml
140+
141+
- uses: actions-rs/cargo@v1
142+
name: "Build poem-example"
143+
if: matrix.target == 'x86_64-unknown-linux-gnu' && matrix.toolchain == 'stable'
144+
with:
145+
command: build
146+
toolchain: ${{ matrix.toolchain }}
147+
args: --target ${{ matrix.target }} --manifest-path ./example-projects/poem-example/Cargo.toml
148+

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ http = { version = "0.2", optional = true }
4747
hyper = { version = "^0.14", optional = true }
4848
axum-lib = { version = "^0.2", optional = true, package = "axum" }
4949
http-body = { version = "^0.4", optional = true }
50-
poem-lib = { version = "1.0.21", optional = true, package = "poem" }
50+
poem-lib = { version = "1.0.23", optional = true, package = "poem" }
5151

5252
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
5353
hostname = "^0.3"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "poem-example"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
cloudevents-sdk = { path = "../..", features = ["poem"] }
8+
tokio = { version = "1.13", features = ["macros", "rt-multi-thread"] }
9+
tracing = "0.1"
10+
poem = { version = "1.0.23" }
11+
tracing-subscriber = "0.2"
12+
13+
[dev-dependencies]
14+
chrono = { version = "0.4", features = ["serde"] }
15+
serde_json = "1.0"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
To run the server:
2+
3+
```console
4+
cargo run
5+
```
6+
7+
To test a GET:
8+
9+
```console
10+
curl http://localhost:8080
11+
```
12+
13+
To test a POST:
14+
15+
```console
16+
curl -d '{"hello": "world"}' \
17+
-H'content-type: application/json' \
18+
-H'ce-specversion: 1.0' \
19+
-H'ce-id: 1' \
20+
-H'ce-source: http://cloudevents.io' \
21+
-H'ce-type: dev.knative.example' \
22+
http://localhost:8080
23+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use cloudevents::Event;
2+
use poem::listener::TcpListener;
3+
use poem::middleware::Tracing;
4+
use poem::{get, handler, Endpoint, EndpointExt, Response, Route, Server};
5+
6+
#[handler]
7+
async fn index_get() -> &'static str {
8+
"hello from cloudevents server"
9+
}
10+
11+
#[handler]
12+
async fn index_post(event: Event) -> Event {
13+
tracing::debug!("received cloudevent {}", &event);
14+
event
15+
}
16+
17+
fn echo_app() -> impl Endpoint<Output = Response> {
18+
Route::new()
19+
.at("/", get(index_get).post(index_post))
20+
.with(Tracing)
21+
}
22+
23+
#[tokio::main]
24+
async fn main() -> Result<(), std::io::Error> {
25+
if std::env::var("RUST_LOG").is_err() {
26+
std::env::set_var("RUST_LOG", "poem=debug")
27+
}
28+
tracing_subscriber::fmt::init();
29+
30+
let server = Server::new(TcpListener::bind("127.0.0.1:8080")).await?;
31+
server.run(echo_app()).await
32+
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
use chrono::Utc;
38+
use poem::http::Method;
39+
use poem::{Body, Request};
40+
use serde_json::json;
41+
42+
#[tokio::test]
43+
async fn poem_test() {
44+
if std::env::var("RUST_LOG").is_err() {
45+
std::env::set_var("RUST_LOG", "poem_example=debug")
46+
}
47+
tracing_subscriber::fmt::init();
48+
49+
let app = echo_app();
50+
let time = Utc::now();
51+
let j = json!({"hello": "world"});
52+
let request = Request::builder()
53+
.method(Method::POST)
54+
.header("ce-specversion", "1.0")
55+
.header("ce-id", "0001")
56+
.header("ce-type", "example.test")
57+
.header("ce-source", "http://localhost/")
58+
.header("ce-someint", "10")
59+
.header("ce-time", time.to_rfc3339())
60+
.header("content-type", "application/json")
61+
.body(Body::from_json(&j).unwrap());
62+
63+
let resp: Response = app.call(request).await;
64+
assert_eq!(
65+
resp.headers()
66+
.get("ce-specversion")
67+
.unwrap()
68+
.to_str()
69+
.unwrap(),
70+
"1.0"
71+
);
72+
assert_eq!(
73+
resp.headers().get("ce-id").unwrap().to_str().unwrap(),
74+
"0001"
75+
);
76+
assert_eq!(
77+
resp.headers().get("ce-type").unwrap().to_str().unwrap(),
78+
"example.test"
79+
);
80+
assert_eq!(
81+
resp.headers().get("ce-source").unwrap().to_str().unwrap(),
82+
"http://localhost/"
83+
);
84+
assert_eq!(
85+
resp.headers()
86+
.get("content-type")
87+
.unwrap()
88+
.to_str()
89+
.unwrap(),
90+
"application/json"
91+
);
92+
assert_eq!(
93+
resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
94+
"10"
95+
);
96+
97+
assert_eq!(
98+
j.to_string().as_bytes(),
99+
resp.into_body().into_vec().await.unwrap()
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)