1- use tide:: * ;
1+ mod test_utils;
2+
3+ use tide:: http:: { self , headers, mime, StatusCode } ;
4+ use tide:: Response ;
25
36#[ async_std:: test]
47async fn test_status ( ) {
5- let mut resp = Response :: new ( StatusCode :: NotFound ) . body_string ( "foo" . to_string ( ) ) ;
8+ let mut resp = Response :: new ( StatusCode :: NotFound ) ;
9+ resp. set_body ( "foo" ) ;
610 assert_eq ! ( resp. status( ) , StatusCode :: NotFound ) ;
711 let foo = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
812 assert_eq ! ( foo. as_bytes( ) , b"foo" ) ;
@@ -11,44 +15,40 @@ async fn test_status() {
1115#[ async_std:: test]
1216async fn byte_vec_content_type ( ) {
1317 use async_std:: io:: Cursor ;
14- use http_types:: headers:: HeaderName ;
15- use std:: str:: FromStr ;
18+ use tide:: Body ;
1619
17- let mut resp = Response :: new ( StatusCode :: Ok ) . body ( Cursor :: new ( "foo" ) ) ;
18- let header_values = resp
19- . header ( & HeaderName :: from_str ( "Content-Type" ) . unwrap ( ) )
20- . unwrap ( ) ;
21- assert_eq ! ( header_values[ 0 ] , mime:: APPLICATION_OCTET_STREAM . to_string( ) ) ;
22- let foo = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
23- assert_eq ! ( foo. as_bytes( ) , b"foo" ) ;
20+ let mut resp = Response :: new ( StatusCode :: Ok ) ;
21+ resp. set_body ( Body :: from_reader ( Cursor :: new ( "foo" ) , None ) ) ;
22+
23+ assert_eq ! ( resp[ headers:: CONTENT_TYPE ] , mime:: BYTE_STREAM . to_string( ) ) ;
24+ let foo = resp. take_body ( ) . into_bytes ( ) . await . unwrap ( ) ;
25+ assert_eq ! ( foo, b"foo" ) ;
2426}
2527
2628#[ async_std:: test]
2729async fn string_content_type ( ) {
28- use http_types :: headers :: HeaderName ;
29- use std :: str :: FromStr ;
30+ let mut resp = Response :: new ( StatusCode :: Ok ) ;
31+ resp . set_body ( "foo" ) ;
3032
31- let mut resp = Response :: new ( StatusCode :: Ok ) . body_string ( "foo" . to_string ( ) ) ;
32- let header_values = resp
33- . header ( & HeaderName :: from_str ( "Content-Type" ) . unwrap ( ) )
34- . unwrap ( ) ;
35- assert_eq ! ( header_values[ 0 ] , mime:: TEXT_PLAIN_UTF_8 . to_string( ) ) ;
33+ assert_eq ! ( resp[ headers:: CONTENT_TYPE ] , mime:: PLAIN . to_string( ) ) ;
3634 let foo = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
37- assert_eq ! ( foo. as_bytes ( ) , b "foo") ;
35+ assert_eq ! ( foo, "foo" ) ;
3836}
3937
4038#[ async_std:: test]
4139async fn json_content_type ( ) {
42- use http_types:: { Method , Url } ;
4340 use std:: collections:: BTreeMap ;
41+ use tide:: http:: { Method , Url } ;
42+ use tide:: Body ;
4443
4544 let mut app = tide:: new ( ) ;
4645 app. at ( "/json_content_type" ) . get ( |_| async move {
4746 let mut map = BTreeMap :: new ( ) ;
4847 map. insert ( Some ( "a" ) , 2 ) ;
4948 map. insert ( Some ( "b" ) , 4 ) ;
5049 map. insert ( None , 6 ) ;
51- let resp = Response :: new ( StatusCode :: Ok ) . body_json ( & map) ?;
50+ let mut resp = Response :: new ( StatusCode :: Ok ) ;
51+ resp. set_body ( Body :: from_json ( & map) ?) ;
5252 Ok ( resp)
5353 } ) ;
5454 let req = http:: Request :: new (
@@ -57,18 +57,20 @@ async fn json_content_type() {
5757 ) ;
5858 let mut resp: http:: Response = app. respond ( req) . await . unwrap ( ) ;
5959 assert_eq ! ( resp. status( ) , StatusCode :: InternalServerError ) ;
60- let body = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
61- assert_eq ! ( body. as_bytes ( ) , b"" ) ;
60+ let body = resp. take_body ( ) . into_bytes ( ) . await . unwrap ( ) ;
61+ assert_eq ! ( body, b"" ) ;
6262
6363 let mut resp = async move {
6464 let mut map = BTreeMap :: new ( ) ;
6565 map. insert ( "a" , 2 ) ;
6666 map. insert ( "b" , 4 ) ;
6767 map. insert ( "c" , 6 ) ;
68- Response :: new ( StatusCode :: Ok ) . body_json ( & map) . unwrap ( )
68+ let mut r = Response :: new ( StatusCode :: Ok ) ;
69+ r. set_body ( Body :: from_json ( & map) . unwrap ( ) ) ;
70+ r
6971 }
7072 . await ;
7173 assert_eq ! ( resp. status( ) , StatusCode :: Ok ) ;
72- let body = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
73- assert_eq ! ( body. as_bytes ( ) , br##"{"a":2,"b":4,"c":6}"## ) ;
74+ let body = resp. take_body ( ) . into_bytes ( ) . await . unwrap ( ) ;
75+ assert_eq ! ( body, br##"{"a":2,"b":4,"c":6}"## ) ;
7476}
0 commit comments