• 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 #![cfg(all(feature = "http1_1", feature = "ylong_base"))]
15 
16 use ylong_http::headers::{Header, HeaderName, HeaderValue, Headers};
17 
18 /// SDV test cases for `Headers`.
19 ///
20 /// # Brief
21 /// 1. Creates a HeaderName and a HeaderValue.
22 /// 2. check the HeaderName and HeaderValue.
23 /// 3. Creates a Header.
24 /// 4. check the error.
25 #[test]
sdv_client_send_request_repeatedly()26 fn sdv_client_send_request_repeatedly() {
27     let name = HeaderName::from_bytes(b"John-Doe").unwrap();
28     let value = HeaderValue::from_bytes(b"Foo").unwrap();
29     let header = Header::from_raw_parts(name, value);
30     let cloned_header = header.clone();
31     assert_eq!(header, cloned_header);
32     assert_eq!(format!("{:?}", header), "Header { name: HeaderName { name: \"john-doe\" }, value: HeaderValue { inner: [[70, 111, 111]], is_sensitive: false } }");
33 
34     assert_eq!(
35         format!("{:?}", HeaderValue::from_bytes(b"Foo\r\n").err().unwrap()),
36         "HttpError { kind: InvalidInput }"
37     );
38     let mut value = HeaderValue::from_bytes(b"Foo").unwrap();
39     assert_eq!(
40         format!("{:?}", value.append_bytes(b"Foo\r\n").err().unwrap()),
41         "HttpError { kind: InvalidInput }"
42     );
43 
44     let mut headers = Headers::new();
45     headers.insert("key", "value").unwrap();
46     assert_eq!(format!("{}", headers), "key: value\n");
47     let mut_value = headers.get_mut("key");
48     let mut value = HeaderValue::from_bytes(b"value").unwrap();
49     assert_eq!(mut_value, Some(&mut value));
50     assert_eq!(headers.remove("key"), Some(value));
51 }
52