@@ -61,18 +61,12 @@ impl Drive for CloudDrive {
61
61
) )
62
62
}
63
63
64
- async fn get ( & self , filename : & str ) -> io:: Result < String > {
64
+ async fn get ( & self , filename : & str ) -> io:: Result < Vec < u8 > > {
65
65
let request = GetFileRequest :: default ( ) . with_get_content ( ) ;
66
66
let response =
67
67
self . service . borrow_mut ( ) . get_file ( & self . username , filename, & request) . await ?;
68
68
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) ,
76
70
None => Err ( io:: Error :: new (
77
71
io:: ErrorKind :: InvalidData ,
78
72
"Server response is missing the file content" . to_string ( ) ,
@@ -93,8 +87,8 @@ impl Drive for CloudDrive {
93
87
}
94
88
}
95
89
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) ;
98
92
self . service . borrow_mut ( ) . patch_file ( & self . username , filename, & request) . await
99
93
}
100
94
@@ -214,7 +208,7 @@ mod tests {
214
208
} ;
215
209
service. borrow_mut ( ) . add_mock_get_file ( "the-user" , "the-filename" , request, Ok ( response) ) ;
216
210
let result = drive. get ( "the-filename" ) . await . unwrap ( ) ;
217
- assert_eq ! ( "some content" , result) ;
211
+ assert_eq ! ( "some content" . as_bytes ( ) , result) ;
218
212
219
213
service. take ( ) . verify_all_used ( ) ;
220
214
}
@@ -235,21 +229,24 @@ mod tests {
235
229
service. take ( ) . verify_all_used ( ) ;
236
230
}
237
231
232
+ #[ allow( invalid_from_utf8) ]
238
233
#[ tokio:: test]
239
234
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
+
240
238
let service = Rc :: from ( RefCell :: from ( MockService :: default ( ) ) ) ;
241
239
service. borrow_mut ( ) . do_login ( ) . await ;
242
240
let drive = CloudDrive :: new ( service. clone ( ) , "the-user" ) ;
243
241
244
242
let request = GetFileRequest :: default ( ) . with_get_content ( ) ;
245
243
let response = GetFileResponse {
246
- content : Some ( BASE64_STANDARD . encode ( [ 0x00 , 0xc3 , 0x28 ] ) ) ,
244
+ content : Some ( BASE64_STANDARD . encode ( BAD_UTF8 ) ) ,
247
245
..Default :: default ( )
248
246
} ;
249
247
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) ;
253
250
254
251
service. take ( ) . verify_all_used ( ) ;
255
252
}
@@ -296,7 +293,7 @@ mod tests {
296
293
297
294
let request = PatchFileRequest :: default ( ) . with_content ( "some content" ) ;
298
295
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 ( ) ;
300
297
301
298
service. take ( ) . verify_all_used ( ) ;
302
299
}
@@ -309,11 +306,11 @@ mod tests {
309
306
310
307
let request = PatchFileRequest :: default ( ) . with_content ( "some content" ) ;
311
308
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 ( ) ;
313
310
314
311
let request = PatchFileRequest :: default ( ) . with_content ( "some other content" ) ;
315
312
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 ( ) ;
317
314
318
315
service. take ( ) . verify_all_used ( ) ;
319
316
}
0 commit comments