Signed-off-by: Joe Grund <jgrund@whamcloud.io>
This commit is contained in:
parent
257bc69dde
commit
eb1fac2425
2 changed files with 51 additions and 11 deletions
juniper_axum
|
@ -16,9 +16,15 @@ All user visible changes to `juniper_axum` crate will be documented in this file
|
||||||
|
|
||||||
- Building on `wasm32-unknown-unknown` and `wasm32-wasi` targets. ([#1283], [#1282])
|
- Building on `wasm32-unknown-unknown` and `wasm32-wasi` targets. ([#1283], [#1282])
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- `Content-Type` header reading full value instead of just media type. ([#1289], [#1288])
|
||||||
|
|
||||||
[#1272]: /../../pull/1272
|
[#1272]: /../../pull/1272
|
||||||
[#1282]: /../../issues/1282
|
[#1282]: /../../issues/1282
|
||||||
[#1283]: /../../pull/1283
|
[#1283]: /../../pull/1283
|
||||||
|
[#1288]: /../../issues/1288
|
||||||
|
[#1289]: /../../pull/1289
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{FromRequest, FromRequestParts, Query},
|
extract::{FromRequest, FromRequestParts, Query},
|
||||||
http::{HeaderValue, Method, Request, StatusCode},
|
http::{header, HeaderValue, Method, Request, StatusCode},
|
||||||
response::{IntoResponse as _, Response},
|
response::{IntoResponse as _, Response},
|
||||||
Json, RequestExt as _,
|
Json, RequestExt as _,
|
||||||
};
|
};
|
||||||
|
@ -85,7 +85,7 @@ where
|
||||||
async fn from_request(mut req: Request<Body>, state: &State) -> Result<Self, Self::Rejection> {
|
async fn from_request(mut req: Request<Body>, state: &State) -> Result<Self, Self::Rejection> {
|
||||||
let content_type = req
|
let content_type = req
|
||||||
.headers()
|
.headers()
|
||||||
.get("content-type")
|
.get(header::CONTENT_TYPE)
|
||||||
.map(HeaderValue::to_str)
|
.map(HeaderValue::to_str)
|
||||||
.transpose()
|
.transpose()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
|
@ -122,7 +122,7 @@ where
|
||||||
.into_response()
|
.into_response()
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
(&Method::POST, Some("application/json")) => {
|
(&Method::POST, Some(x)) if x.starts_with("application/json") => {
|
||||||
Json::<GraphQLBatchRequest<S>>::from_request(req, state)
|
Json::<GraphQLBatchRequest<S>>::from_request(req, state)
|
||||||
.await
|
.await
|
||||||
.map(|req| Self(req.0))
|
.map(|req| Self(req.0))
|
||||||
|
@ -130,14 +130,16 @@ where
|
||||||
(StatusCode::BAD_REQUEST, format!("Invalid JSON body: {e}")).into_response()
|
(StatusCode::BAD_REQUEST, format!("Invalid JSON body: {e}")).into_response()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
(&Method::POST, Some("application/graphql")) => String::from_request(req, state)
|
(&Method::POST, Some(x)) if x.starts_with("application/graphql") => {
|
||||||
.await
|
String::from_request(req, state)
|
||||||
.map(|body| {
|
.await
|
||||||
Self(GraphQLBatchRequest::Single(GraphQLRequest::new(
|
.map(|body| {
|
||||||
body, None, None,
|
Self(GraphQLBatchRequest::Single(GraphQLRequest::new(
|
||||||
)))
|
body, None, None,
|
||||||
})
|
)))
|
||||||
.map_err(|_| (StatusCode::BAD_REQUEST, "Not valid UTF-8 body").into_response()),
|
})
|
||||||
|
.map_err(|_| (StatusCode::BAD_REQUEST, "Not valid UTF-8 body").into_response())
|
||||||
|
}
|
||||||
(&Method::POST, _) => Err((
|
(&Method::POST, _) => Err((
|
||||||
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
||||||
"`Content-Type` header is expected to be either `application/json` or \
|
"`Content-Type` header is expected to be either `application/json` or \
|
||||||
|
@ -246,6 +248,22 @@ mod juniper_request_tests {
|
||||||
assert_eq!(do_from_request(req).await, expected);
|
assert_eq!(do_from_request(req).await, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn from_json_post_request_with_charset() {
|
||||||
|
let req = Request::post("/")
|
||||||
|
.header("content-type", "application/json; charset=utf-8")
|
||||||
|
.body(Body::from(r#"{"query": "{ add(a: 2, b: 3) }"}"#))
|
||||||
|
.unwrap_or_else(|e| panic!("cannot build `Request`: {e}"));
|
||||||
|
|
||||||
|
let expected = JuniperRequest(GraphQLBatchRequest::Single(GraphQLRequest::new(
|
||||||
|
"{ add(a: 2, b: 3) }".to_string(),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)));
|
||||||
|
|
||||||
|
assert_eq!(do_from_request(req).await, expected);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn from_graphql_post_request() {
|
async fn from_graphql_post_request() {
|
||||||
let req = Request::post("/")
|
let req = Request::post("/")
|
||||||
|
@ -262,6 +280,22 @@ mod juniper_request_tests {
|
||||||
assert_eq!(do_from_request(req).await, expected);
|
assert_eq!(do_from_request(req).await, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn from_graphql_post_request_with_charset() {
|
||||||
|
let req = Request::post("/")
|
||||||
|
.header("content-type", "application/graphql; charset=utf-8")
|
||||||
|
.body(Body::from(r#"{ add(a: 2, b: 3) }"#))
|
||||||
|
.unwrap_or_else(|e| panic!("cannot build `Request`: {e}"));
|
||||||
|
|
||||||
|
let expected = JuniperRequest(GraphQLBatchRequest::Single(GraphQLRequest::new(
|
||||||
|
"{ add(a: 2, b: 3) }".to_string(),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)));
|
||||||
|
|
||||||
|
assert_eq!(do_from_request(req).await, expected);
|
||||||
|
}
|
||||||
|
|
||||||
/// Performs [`JuniperRequest::from_request()`].
|
/// Performs [`JuniperRequest::from_request()`].
|
||||||
async fn do_from_request(req: Request<Body>) -> JuniperRequest {
|
async fn do_from_request(req: Request<Body>) -> JuniperRequest {
|
||||||
match JuniperRequest::from_request(req, &()).await {
|
match JuniperRequest::from_request(req, &()).await {
|
||||||
|
|
Loading…
Add table
Reference in a new issue