@@ -61,18 +61,12 @@ impl Drive for CloudDrive {
6161 ) )
6262 }
6363
64- async fn get ( & self , filename : & str ) -> io:: Result < String > {
64+ async fn get ( & self , filename : & str ) -> io:: Result < Vec < u8 > > {
6565 let request = GetFileRequest :: default ( ) . with_get_content ( ) ;
6666 let response =
6767 self . service . borrow_mut ( ) . get_file ( & self . username , filename, & request) . await ?;
6868 match response. decoded_content ( ) ? {
69- Some ( content) => match String :: from_utf8 ( content) {
70- Ok ( s) => Ok ( s) ,
71- Err ( e) => Err ( io:: Error :: new (
72- io:: ErrorKind :: InvalidData ,
73- format ! ( "Requested file is not valid UTF-8: {}" , e) ,
74- ) ) ,
75- } ,
69+ Some ( content) => Ok ( content) ,
7670 None => Err ( io:: Error :: new (
7771 io:: ErrorKind :: InvalidData ,
7872 "Server response is missing the file content" . to_string ( ) ,
@@ -93,8 +87,8 @@ impl Drive for CloudDrive {
9387 }
9488 }
9589
96- async fn put ( & mut self , filename : & str , content : & str ) -> io:: Result < ( ) > {
97- let request = PatchFileRequest :: default ( ) . with_content ( content. as_bytes ( ) ) ;
90+ async fn put ( & mut self , filename : & str , content : & [ u8 ] ) -> io:: Result < ( ) > {
91+ let request = PatchFileRequest :: default ( ) . with_content ( content) ;
9892 self . service . borrow_mut ( ) . patch_file ( & self . username , filename, & request) . await
9993 }
10094
@@ -214,7 +208,7 @@ mod tests {
214208 } ;
215209 service. borrow_mut ( ) . add_mock_get_file ( "the-user" , "the-filename" , request, Ok ( response) ) ;
216210 let result = drive. get ( "the-filename" ) . await . unwrap ( ) ;
217- assert_eq ! ( "some content" , result) ;
211+ assert_eq ! ( "some content" . as_bytes ( ) , result) ;
218212
219213 service. take ( ) . verify_all_used ( ) ;
220214 }
@@ -235,21 +229,24 @@ mod tests {
235229 service. take ( ) . verify_all_used ( ) ;
236230 }
237231
232+ #[ allow( invalid_from_utf8) ]
238233 #[ tokio:: test]
239234 async fn test_clouddrive_get_invalid_utf8 ( ) {
235+ const BAD_UTF8 : & [ u8 ] = & [ 0x00 , 0xc3 , 0x28 ] ;
236+ assert ! ( str :: from_utf8( BAD_UTF8 ) . is_err( ) ) ;
237+
240238 let service = Rc :: from ( RefCell :: from ( MockService :: default ( ) ) ) ;
241239 service. borrow_mut ( ) . do_login ( ) . await ;
242240 let drive = CloudDrive :: new ( service. clone ( ) , "the-user" ) ;
243241
244242 let request = GetFileRequest :: default ( ) . with_get_content ( ) ;
245243 let response = GetFileResponse {
246- content : Some ( BASE64_STANDARD . encode ( [ 0x00 , 0xc3 , 0x28 ] ) ) ,
244+ content : Some ( BASE64_STANDARD . encode ( BAD_UTF8 ) ) ,
247245 ..Default :: default ( )
248246 } ;
249247 service. borrow_mut ( ) . add_mock_get_file ( "the-user" , "the-filename" , request, Ok ( response) ) ;
250- let err = drive. get ( "the-filename" ) . await . unwrap_err ( ) ;
251- assert_eq ! ( io:: ErrorKind :: InvalidData , err. kind( ) ) ;
252- assert ! ( format!( "{}" , err) . contains( "not valid UTF-8" ) ) ;
248+ let result = drive. get ( "the-filename" ) . await . unwrap ( ) ;
249+ assert_eq ! ( BAD_UTF8 , result) ;
253250
254251 service. take ( ) . verify_all_used ( ) ;
255252 }
@@ -296,7 +293,7 @@ mod tests {
296293
297294 let request = PatchFileRequest :: default ( ) . with_content ( "some content" ) ;
298295 service. borrow_mut ( ) . add_mock_patch_file ( "the-user" , "the-filename" , request, Ok ( ( ) ) ) ;
299- drive. put ( "the-filename" , "some content" ) . await . unwrap ( ) ;
296+ drive. put ( "the-filename" , b "some content") . await . unwrap ( ) ;
300297
301298 service. take ( ) . verify_all_used ( ) ;
302299 }
@@ -309,11 +306,11 @@ mod tests {
309306
310307 let request = PatchFileRequest :: default ( ) . with_content ( "some content" ) ;
311308 service. borrow_mut ( ) . add_mock_patch_file ( "the-user" , "the-filename" , request, Ok ( ( ) ) ) ;
312- drive. put ( "the-filename" , "some content" ) . await . unwrap ( ) ;
309+ drive. put ( "the-filename" , b "some content") . await . unwrap ( ) ;
313310
314311 let request = PatchFileRequest :: default ( ) . with_content ( "some other content" ) ;
315312 service. borrow_mut ( ) . add_mock_patch_file ( "the-user" , "the-filename" , request, Ok ( ( ) ) ) ;
316- drive. put ( "the-filename" , "some other content" ) . await . unwrap ( ) ;
313+ drive. put ( "the-filename" , b "some other content") . await . unwrap ( ) ;
317314
318315 service. take ( ) . verify_all_used ( ) ;
319316 }
0 commit comments