in src/common/headers.rs [582:700]
fn test_parse_header_line() {
let mut header = Headers::default();
// Invalid header syntax.
assert_eq!(
header.parse_header_line(b"Expect"),
Err(RequestError::HeaderError(HttpHeaderError::InvalidFormat(
"Expect".to_string()
)))
);
// Invalid content length.
assert_eq!(
header.parse_header_line(b"Content-Length: five"),
Err(RequestError::HeaderError(HttpHeaderError::InvalidValue(
"Content-Length".to_string(),
" five".to_string()
)))
);
// Invalid transfer encoding.
assert_eq!(
header.parse_header_line(b"Transfer-Encoding: gzip"),
Err(RequestError::HeaderError(
HttpHeaderError::UnsupportedValue(
"Transfer-Encoding".to_string(),
" gzip".to_string()
)
))
);
// Invalid expect.
assert_eq!(
header
.parse_header_line(b"Expect: 102-processing")
.unwrap_err(),
RequestError::HeaderError(HttpHeaderError::UnsupportedValue(
"Expect".to_string(),
" 102-processing".to_string()
))
);
// Unsupported media type.
assert_eq!(
header
.parse_header_line(b"Content-Type: application/json-patch")
.unwrap_err(),
RequestError::HeaderError(HttpHeaderError::UnsupportedValue(
"Content-Type".to_string(),
" application/json-patch".to_string()
))
);
// Invalid input format.
let input: [u8; 10] = [130, 140, 150, 130, 140, 150, 130, 140, 150, 160];
assert_eq!(
header.parse_header_line(&input[..]).unwrap_err(),
RequestError::HeaderError(HttpHeaderError::InvalidUtf8String(
String::from_utf8(input.to_vec()).unwrap_err().utf8_error()
))
);
// Test valid transfer encoding.
assert!(header
.parse_header_line(b"Transfer-Encoding: chunked")
.is_ok());
assert!(header.chunked());
// Test valid expect.
assert!(header.parse_header_line(b"Expect: 100-continue").is_ok());
assert!(header.expect());
// Test valid media type.
assert!(header
.parse_header_line(b"Content-Type: application/json")
.is_ok());
// Test valid accept media type.
assert!(header
.parse_header_line(b"Accept: application/json")
.is_ok());
assert_eq!(header.accept, MediaType::ApplicationJson);
assert!(header.parse_header_line(b"Accept: text/plain").is_ok());
assert_eq!(header.accept, MediaType::PlainText);
// Test invalid accept media type.
assert!(header
.parse_header_line(b"Accept: application/json-patch")
.is_err());
// Invalid content length.
assert_eq!(
header.parse_header_line(b"Content-Length: -1"),
Err(RequestError::HeaderError(HttpHeaderError::InvalidValue(
"Content-Length".to_string(),
" -1".to_string()
)))
);
assert!(header
.parse_header_line(b"Accept-Encoding: deflate")
.is_ok());
assert_eq!(
header.parse_header_line(b"Accept-Encoding: compress, identity;q=0"),
Err(RequestError::HeaderError(HttpHeaderError::InvalidValue(
"Accept-Encoding".to_string(),
" identity;q=0".to_string()
)))
);
// Test custom header.
assert_eq!(header.custom_entries().len(), 0);
assert!(header.parse_header_line(b"Custom-Header: foo").is_ok());
assert_eq!(
header.custom_entries().get("Custom-Header").unwrap(),
&"foo".to_string()
);
assert_eq!(header.custom_entries().len(), 1);
}