@@ -671,4 +671,135 @@ mod extract_tests {
671671 assert_handler ( option_cx_req) ;
672672 assert_handler ( result_cx_req) ;
673673 }
674+
675+ #[ cfg( any( feature = "form" , feature = "json" ) ) ]
676+ fn simple_req ( content_type : & ' static str , body : & ' static str ) -> crate :: request:: Request {
677+ let mut req = crate :: request:: Request :: new ( Body :: from ( body) ) ;
678+ req. headers_mut ( ) . insert (
679+ http:: header:: CONTENT_TYPE ,
680+ http:: header:: HeaderValue :: from_static ( content_type) ,
681+ ) ;
682+ req
683+ }
684+
685+ #[ cfg( feature = "form" ) ]
686+ #[ tokio:: test]
687+ async fn extract_form ( ) {
688+ use crate :: server:: test_helpers;
689+
690+ #[ derive( Debug , PartialEq , Eq , serde:: Deserialize ) ]
691+ struct TestForm {
692+ key1 : String ,
693+ key2 : String ,
694+ key3 : String ,
695+ }
696+
697+ const VALID_FORM : & str = "key1=value1&key2=value2&key3=value3" ;
698+ const INVALID_FORM : & str = "if (key && value) { print(key, value) }" ;
699+
700+ let test_form = serde_urlencoded:: from_str ( VALID_FORM ) . unwrap ( ) ;
701+
702+ // simple content-type
703+ {
704+ let req = simple_req ( "application/x-www-form-urlencoded" , VALID_FORM ) ;
705+ let ( parts, body) = req. into_parts ( ) ;
706+ assert_eq ! (
707+ super :: Form :: <TestForm >:: from_request( & mut test_helpers:: empty_cx( ) , parts, body, )
708+ . await
709+ . unwrap( )
710+ . 0 ,
711+ test_form,
712+ ) ;
713+ }
714+ // content-type with charset
715+ {
716+ let req = simple_req (
717+ "application/x-www-form-urlencoded; charset=utf-8" ,
718+ VALID_FORM ,
719+ ) ;
720+ let ( parts, body) = req. into_parts ( ) ;
721+ assert_eq ! (
722+ super :: Form :: <TestForm >:: from_request( & mut test_helpers:: empty_cx( ) , parts, body, )
723+ . await
724+ . unwrap( )
725+ . 0 ,
726+ test_form,
727+ ) ;
728+ }
729+ // wrong content type
730+ {
731+ let req = simple_req ( "text/javascript" , VALID_FORM ) ;
732+ let ( parts, body) = req. into_parts ( ) ;
733+ super :: Form :: < TestForm > :: from_request ( & mut test_helpers:: empty_cx ( ) , parts, body)
734+ . await
735+ . unwrap_err ( ) ;
736+ }
737+ // invalid form
738+ {
739+ let req = simple_req ( "application/x-www-form-urlencoded" , INVALID_FORM ) ;
740+ let ( parts, body) = req. into_parts ( ) ;
741+ super :: Form :: < TestForm > :: from_request ( & mut test_helpers:: empty_cx ( ) , parts, body)
742+ . await
743+ . unwrap_err ( ) ;
744+ }
745+ }
746+
747+ #[ cfg( feature = "json" ) ]
748+ #[ tokio:: test]
749+ async fn extract_json ( ) {
750+ use crate :: server:: test_helpers;
751+
752+ #[ derive( Debug , PartialEq , Eq , serde:: Deserialize ) ]
753+ struct TestJson {
754+ key1 : String ,
755+ key2 : String ,
756+ key3 : String ,
757+ }
758+
759+ const VALID_JSON : & str = r#"{"key1":"value1","key2":"value2", "key3": "value3"}"# ;
760+ const INVALID_JSON : & str = "if (key && value) { print(key, value) }" ;
761+
762+ let test_json = crate :: utils:: json:: deserialize ( VALID_JSON . as_bytes ( ) ) . unwrap ( ) ;
763+
764+ // simple content-type
765+ {
766+ let req = simple_req ( "application/json" , VALID_JSON ) ;
767+ let ( parts, body) = req. into_parts ( ) ;
768+ assert_eq ! (
769+ super :: Json :: <TestJson >:: from_request( & mut test_helpers:: empty_cx( ) , parts, body, )
770+ . await
771+ . unwrap( )
772+ . 0 ,
773+ test_json,
774+ ) ;
775+ }
776+ // content-type with charset
777+ {
778+ let req = simple_req ( "application/json; charset=utf-8" , VALID_JSON ) ;
779+ let ( parts, body) = req. into_parts ( ) ;
780+ assert_eq ! (
781+ super :: Json :: <TestJson >:: from_request( & mut test_helpers:: empty_cx( ) , parts, body, )
782+ . await
783+ . unwrap( )
784+ . 0 ,
785+ test_json,
786+ ) ;
787+ }
788+ // wrong content type
789+ {
790+ let req = simple_req ( "text/javascript" , VALID_JSON ) ;
791+ let ( parts, body) = req. into_parts ( ) ;
792+ super :: Json :: < TestJson > :: from_request ( & mut test_helpers:: empty_cx ( ) , parts, body)
793+ . await
794+ . unwrap_err ( ) ;
795+ }
796+ // invalid form
797+ {
798+ let req = simple_req ( "application/json" , INVALID_JSON ) ;
799+ let ( parts, body) = req. into_parts ( ) ;
800+ super :: Json :: < TestJson > :: from_request ( & mut test_helpers:: empty_cx ( ) , parts, body)
801+ . await
802+ . unwrap_err ( ) ;
803+ }
804+ }
674805}
0 commit comments