fix dev-dependencies, bump axum version
Signed-off-by: xiaoqilin <xiaoqilin@bytedance.com>
This commit is contained in:
parent
4a86973f22
commit
887af22f60
|
@ -53,7 +53,7 @@ bytes = { version = "^1.0", optional = true }
|
|||
futures = { version = "^0.3", optional = true }
|
||||
http = { version = "0.2", optional = true }
|
||||
hyper = { version = "^0.14", optional = true }
|
||||
axum-lib = { version = "^0.5", optional = true , package="axum"}
|
||||
axum-lib = { version = "^0.6", optional = true, package="axum"}
|
||||
http-body = { version = "^0.4", optional = true }
|
||||
poem-lib = { version = "=1.2.34", optional = true, package = "poem" }
|
||||
nats-lib = { version = "0.21.0", optional = true, package = "nats" }
|
||||
|
@ -66,7 +66,7 @@ web-sys = { version = "^0.3", features = ["Window", "Location"] }
|
|||
|
||||
[dev-dependencies]
|
||||
rstest = "0.6"
|
||||
claim = "0.3.1"
|
||||
claims = "0.7.1"
|
||||
version-sync = "0.9.2"
|
||||
serde_yaml = "0.8"
|
||||
rmp-serde = "1"
|
||||
|
|
|
@ -6,7 +6,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
cloudevents-sdk = { path = "../..", features = ["axum"] }
|
||||
axum = "^0.5"
|
||||
axum = "^0.6"
|
||||
http = "^0.2"
|
||||
tokio = { version = "^1", features = ["full"] }
|
||||
tracing = "^0.1"
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
use axum_lib as axum;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use axum::extract::{FromRequest, RequestParts};
|
||||
use axum::extract::FromRequest;
|
||||
use axum::http::Request;
|
||||
use http::request::Parts;
|
||||
use http::StatusCode;
|
||||
use http_body::Body;
|
||||
use hyper::body;
|
||||
|
@ -12,26 +14,23 @@ use crate::event::Event;
|
|||
type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
#[async_trait]
|
||||
impl<B> FromRequest<B> for Event
|
||||
impl<S, B> FromRequest<S, B> for Event
|
||||
where
|
||||
B: Body + Send,
|
||||
B: Body + Send + 'static,
|
||||
B::Data: Send,
|
||||
B::Error: Into<BoxError>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
let req_body = req
|
||||
.take_body()
|
||||
.ok_or(0)
|
||||
.map_err(|_| (StatusCode::BAD_REQUEST, "unexpected empty body".to_string()))?;
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
let (Parts { headers, .. }, req_body) = req.into_parts();
|
||||
let buf = body::to_bytes(req_body)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, format!("{}", e.into())))?
|
||||
.to_vec();
|
||||
let headers = req.headers();
|
||||
|
||||
to_event(headers, buf).map_err(|e| (StatusCode::BAD_REQUEST, format!("{}", e)))
|
||||
to_event(&headers, buf).map_err(|e| (StatusCode::BAD_REQUEST, format!("{}", e)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,39 +48,35 @@ mod tests {
|
|||
async fn axum_test_request() {
|
||||
let expected = fixtures::v10::minimal_string_extension();
|
||||
|
||||
let mut request = RequestParts::new(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "1.0")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "test_event.test_application")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-someint", "10")
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
);
|
||||
let mut request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "1.0")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "test_event.test_application")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-someint", "10")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
|
||||
let result = Event::from_request(&mut request).await.unwrap();
|
||||
let result = Event::from_request(request, &()).await.unwrap();
|
||||
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn axum_test_bad_request() {
|
||||
let mut request = RequestParts::new(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "BAD SPECIFICATION")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "example.test")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-someint", "10")
|
||||
.header("ce-time", fixtures::time().to_rfc3339())
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
);
|
||||
let mut request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "BAD SPECIFICATION")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "example.test")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-someint", "10")
|
||||
.header("ce-time", fixtures::time().to_rfc3339())
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
|
||||
let result = Event::from_request(&mut request).await;
|
||||
let result = Event::from_request(request, &()).await;
|
||||
assert!(result.is_err());
|
||||
let rejection = result.unwrap_err();
|
||||
|
||||
|
@ -93,24 +88,22 @@ mod tests {
|
|||
async fn axum_test_request_with_full_data() {
|
||||
let expected = fixtures::v10::full_binary_json_data_string_extension();
|
||||
|
||||
let mut request = RequestParts::new(
|
||||
Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "1.0")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "test_event.test_application")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-subject", "cloudevents-sdk")
|
||||
.header("content-type", "application/json")
|
||||
.header("ce-string_ex", "val")
|
||||
.header("ce-int_ex", "10")
|
||||
.header("ce-bool_ex", "true")
|
||||
.header("ce-time", &fixtures::time().to_rfc3339())
|
||||
.body(Body::from(fixtures::json_data_binary()))
|
||||
.unwrap(),
|
||||
);
|
||||
let mut request = Request::builder()
|
||||
.method(http::Method::POST)
|
||||
.header("ce-specversion", "1.0")
|
||||
.header("ce-id", "0001")
|
||||
.header("ce-type", "test_event.test_application")
|
||||
.header("ce-source", "http://localhost/")
|
||||
.header("ce-subject", "cloudevents-sdk")
|
||||
.header("content-type", "application/json")
|
||||
.header("ce-string_ex", "val")
|
||||
.header("ce-int_ex", "10")
|
||||
.header("ce-bool_ex", "true")
|
||||
.header("ce-time", &fixtures::time().to_rfc3339())
|
||||
.body(Body::from(fixtures::json_data_binary()))
|
||||
.unwrap();
|
||||
|
||||
let result = Event::from_request(&mut request).await.unwrap();
|
||||
let result = Event::from_request(request, &()).await.unwrap();
|
||||
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ mod tests {
|
|||
use crate::EventBuilder;
|
||||
use crate::EventBuilderV03;
|
||||
use crate::EventBuilderV10;
|
||||
use claim::*;
|
||||
use claims::*;
|
||||
use rstest::rstest;
|
||||
use serde_json::{json, Value};
|
||||
use serde_yaml;
|
||||
|
|
Loading…
Reference in New Issue