• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! Tests nested `MimeMulti`, for its building and asynchronous encoding.
15 
16 use ylong_http::body::{async_impl, MimeMulti, MimeMultiEncoder, MimePart};
17 
18 #[tokio::main]
main()19 async fn main() {
20     let body_text1 = "\
21     This could have been part of the previous part, but \
22     illustrates explicit versus implicit typing of body parts.\r\n"
23         .as_bytes();
24     let body_text2 = "\
25     ... base64-encoded 8000 Hz single-channel \
26     mu-law-format audio data goes here ...\r\n"
27         .as_bytes();
28     let body_text3 = "... base64-encoded image data goes here ...\r\n".as_bytes();
29 
30     let body_text4 = "\
31 This is <bold><italic>enriched.</italic></bold>
32 <smaller>as defined in RFC 1896</smaller>
33 
34 Isn't it
35 <bigger><bigger>cool?</bigger></bigger>
36 "
37     .as_bytes();
38 
39     let body_text5 = "\
40 From: (mailbox in US-ASCII)
41 To: (address in US-ASCII)
42 Subject: (subject in US-ASCII)
43 Content-Type: Text/plain; charset=ISO-8859-1
44 Content-Transfer-Encoding: Quoted-printable
45 
46 ... Additional text in ISO-8859-1 goes here ...
47 "
48     .as_bytes();
49 
50     let multi = MimeMulti::builder()
51         .set_content_type(b"multipart/mixed", b"unique-boundary-1".to_vec())
52         .add_part(
53             MimePart::builder()
54                 .body_from_bytes("... Some text appears here ...\r\n".as_bytes())
55                 .build()
56                 .unwrap(),
57         )
58         .add_part(
59             MimePart::builder()
60                 .header("Content-type", "text/plain; charset=US-ASCII")
61                 .body_from_bytes(body_text1)
62                 .build()
63                 .unwrap(),
64         )
65         .add_multi(
66             MimeMulti::builder()
67                 .set_content_type(b"multipart/parallel", b"unique-boundary-2".to_vec())
68                 .add_part(
69                     MimePart::builder()
70                         .header("Content-type", "audio/basic")
71                         .header("Content-Transfer-Encoding", "base64")
72                         .body_from_bytes(body_text2)
73                         .build()
74                         .unwrap(),
75                 )
76                 .add_part(
77                     MimePart::builder()
78                         .header("Content-type", "image/jpeg")
79                         .header("Content-Transfer-Encoding", "base64")
80                         .body_from_bytes(body_text3)
81                         .build()
82                         .unwrap(),
83                 )
84                 .build()
85                 .unwrap(),
86         )
87         .add_part(
88             MimePart::builder()
89                 .header("Content-type", "text/enriched")
90                 .body_from_async_reader(body_text4)
91                 .build()
92                 .unwrap(),
93         )
94         .add_part(
95             MimePart::builder()
96                 .header("Content-type", "message/rfc822")
97                 .body_from_async_reader(body_text5)
98                 .build()
99                 .unwrap(),
100         )
101         .build()
102         .unwrap();
103 
104     let mut multi_encoder = MimeMultiEncoder::from_multi(multi);
105     let mut buf = vec![0u8; 50];
106     let mut v_size = vec![];
107     let mut v_str = vec![];
108 
109     loop {
110         let len = async_impl::Body::data(&mut multi_encoder, &mut buf)
111             .await
112             .unwrap();
113         if len == 0 {
114             break;
115         }
116         v_size.push(len);
117         v_str.extend_from_slice(&buf[..len]);
118     }
119     assert_eq!(
120         v_size,
121         vec![
122             50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
123             35
124         ]
125     );
126     // Headers is a HashMap, so that sequence of iter is different.
127     println!("{}", core::str::from_utf8(&v_str).unwrap());
128 }
129