1
- use tide:: * ;
1
+ mod test_utils;
2
+
3
+ use tide:: http:: { self , headers, mime, StatusCode } ;
4
+ use tide:: Response ;
2
5
3
6
#[ async_std:: test]
4
7
async 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" ) ;
6
10
assert_eq ! ( resp. status( ) , StatusCode :: NotFound ) ;
7
11
let foo = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
8
12
assert_eq ! ( foo. as_bytes( ) , b"foo" ) ;
@@ -11,44 +15,40 @@ async fn test_status() {
11
15
#[ async_std:: test]
12
16
async fn byte_vec_content_type ( ) {
13
17
use async_std:: io:: Cursor ;
14
- use http_types:: headers:: HeaderName ;
15
- use std:: str:: FromStr ;
18
+ use tide:: Body ;
16
19
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" ) ;
24
26
}
25
27
26
28
#[ async_std:: test]
27
29
async 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" ) ;
30
32
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( ) ) ;
36
34
let foo = resp. take_body ( ) . into_string ( ) . await . unwrap ( ) ;
37
- assert_eq ! ( foo. as_bytes ( ) , b "foo") ;
35
+ assert_eq ! ( foo, "foo" ) ;
38
36
}
39
37
40
38
#[ async_std:: test]
41
39
async fn json_content_type ( ) {
42
- use http_types:: { Method , Url } ;
43
40
use std:: collections:: BTreeMap ;
41
+ use tide:: http:: { Method , Url } ;
42
+ use tide:: Body ;
44
43
45
44
let mut app = tide:: new ( ) ;
46
45
app. at ( "/json_content_type" ) . get ( |_| async move {
47
46
let mut map = BTreeMap :: new ( ) ;
48
47
map. insert ( Some ( "a" ) , 2 ) ;
49
48
map. insert ( Some ( "b" ) , 4 ) ;
50
49
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) ?) ;
52
52
Ok ( resp)
53
53
} ) ;
54
54
let req = http:: Request :: new (
@@ -57,18 +57,20 @@ async fn json_content_type() {
57
57
) ;
58
58
let mut resp: http:: Response = app. respond ( req) . await . unwrap ( ) ;
59
59
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"" ) ;
62
62
63
63
let mut resp = async move {
64
64
let mut map = BTreeMap :: new ( ) ;
65
65
map. insert ( "a" , 2 ) ;
66
66
map. insert ( "b" , 4 ) ;
67
67
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
69
71
}
70
72
. await ;
71
73
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}"## ) ;
74
76
}
0 commit comments