Fix Content-Type header parsing in 'juniper_actix' (#863, #860)

This commit is contained in:
Erik 2021-01-26 12:07:39 -08:00 committed by GitHub
parent 824cd4081b
commit 8d654a6b42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,8 +42,8 @@ Check the LICENSE file for details.
use actix_web::{ use actix_web::{
error::{ErrorBadRequest, ErrorMethodNotAllowed, ErrorUnsupportedMediaType}, error::{ErrorBadRequest, ErrorMethodNotAllowed, ErrorUnsupportedMediaType},
http::{header::CONTENT_TYPE, Method}, http::Method,
web, Error, FromRequest, HttpRequest, HttpResponse, web, Error, FromRequest, HttpMessage, HttpRequest, HttpResponse,
}; };
use juniper::{ use juniper::{
http::{ http::{
@ -152,16 +152,12 @@ where
CtxT: Sync, CtxT: Sync,
S: ScalarValue + Send + Sync, S: ScalarValue + Send + Sync,
{ {
let content_type_header = req let req = match req.content_type() {
.headers() "application/json" => {
.get(CONTENT_TYPE)
.and_then(|hv| hv.to_str().ok());
let req = match content_type_header {
Some("application/json") => {
let body = String::from_request(&req, &mut payload.into_inner()).await?; let body = String::from_request(&req, &mut payload.into_inner()).await?;
serde_json::from_str::<GraphQLBatchRequest<S>>(&body).map_err(ErrorBadRequest) serde_json::from_str::<GraphQLBatchRequest<S>>(&body).map_err(ErrorBadRequest)
} }
Some("application/graphql") => { "application/graphql" => {
let body = String::from_request(&req, &mut payload.into_inner()).await?; let body = String::from_request(&req, &mut payload.into_inner()).await?;
Ok(GraphQLBatchRequest::Single(GraphQLRequest::new( Ok(GraphQLBatchRequest::Single(GraphQLRequest::new(
body, None, None, body, None, None,
@ -609,7 +605,7 @@ mod tests {
); );
let req = test::TestRequest::post() let req = test::TestRequest::post()
.header("content-type", "application/json") .header("content-type", "application/json; charset=utf-8")
.set_payload( .set_payload(
r##"{ "variables": null, "query": "{ hero(episode: NEW_HOPE) { name } }" }"##, r##"{ "variables": null, "query": "{ hero(episode: NEW_HOPE) { name } }" }"##,
) )