File tree Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Expand file tree Collapse file tree 2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -467,9 +467,12 @@ impl BufRead for Body {
467
467
/// This is used for various binary formats such as images and videos.
468
468
#[ cfg( feature = "async_std" ) ]
469
469
async fn peek_mime ( file : & mut async_std:: fs:: File ) -> io:: Result < Option < Mime > > {
470
+ // Reading the first 8 bytes should be enough to sniff the mime type.
470
471
let mut buf = [ 0_u8 ; 8 ] ;
471
- file. read_exact ( & mut buf) . await ?;
472
+ file. read ( & mut buf) . await ?;
472
473
let mime = Mime :: sniff ( & buf) . ok ( ) ;
474
+
475
+ // Reset the file cursor back to the start.
473
476
file. seek ( io:: SeekFrom :: Start ( 0 ) ) . await ?;
474
477
Ok ( mime)
475
478
}
Original file line number Diff line number Diff line change 1
1
use async_std:: io;
2
+ use async_std:: fs;
2
3
use http_types:: { mime, Body , Response } ;
3
4
4
5
#[ async_std:: test]
@@ -11,11 +12,16 @@ async fn guess_plain_text_mime() -> io::Result<()> {
11
12
}
12
13
13
14
#[ async_std:: test]
14
- async fn guess_binary_mime ( ) -> io :: Result < ( ) > {
15
+ async fn guess_binary_mime ( ) -> http_types :: Result < ( ) > {
15
16
let body = Body :: from_file ( "tests/fixtures/nori.png" ) . await ?;
16
17
let mut res = Response :: new ( 200 ) ;
17
18
res. set_body ( body) ;
18
19
assert_eq ! ( res. content_type( ) , Some ( mime:: PNG ) ) ;
20
+
21
+ // Assert the file is correctly reset after we've peeked the bytes
22
+ let left = fs:: read ( "tests/fixtures/nori.png" ) . await ?;
23
+ let right = res. body_bytes ( ) . await ?;
24
+ assert_eq ! ( left, right) ;
19
25
Ok ( ( ) )
20
26
}
21
27
You can’t perform that action at this time.
0 commit comments