mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-31 11:02:08 +01:00
simplify simple decoder tests
This commit is contained in:
parent
75517cce82
commit
40eab1f091
@ -844,121 +844,98 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_default_1_0() {
|
fn test_conn_default_1_0() {
|
||||||
let mut buf = BytesMut::from("GET /test HTTP/1.0\r\n\r\n");
|
let req = parse_ready!(&mut BytesMut::from("GET /test HTTP/1.0\r\n\r\n"));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_default_1_1() {
|
fn test_conn_default_1_1() {
|
||||||
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
|
let req = parse_ready!(&mut BytesMut::from("GET /test HTTP/1.1\r\n\r\n"));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_close() {
|
fn test_conn_close() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
connection: close\r\n\r\n",
|
connection: close\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
||||||
|
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
connection: Close\r\n\r\n",
|
connection: Close\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_close_1_0() {
|
fn test_conn_close_1_0() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.0\r\n\
|
"GET /test HTTP/1.0\r\n\
|
||||||
connection: close\r\n\r\n",
|
connection: close\r\n\r\n",
|
||||||
);
|
));
|
||||||
|
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_keep_alive_1_0() {
|
fn test_conn_keep_alive_1_0() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.0\r\n\
|
"GET /test HTTP/1.0\r\n\
|
||||||
connection: keep-alive\r\n\r\n",
|
connection: keep-alive\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
|
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.0\r\n\
|
"GET /test HTTP/1.0\r\n\
|
||||||
connection: Keep-Alive\r\n\r\n",
|
connection: Keep-Alive\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_keep_alive_1_1() {
|
fn test_conn_keep_alive_1_1() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
connection: keep-alive\r\n\r\n",
|
connection: keep-alive\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_other_1_0() {
|
fn test_conn_other_1_0() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.0\r\n\
|
"GET /test HTTP/1.0\r\n\
|
||||||
connection: other\r\n\r\n",
|
connection: other\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_other_1_1() {
|
fn test_conn_other_1_1() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
connection: other\r\n\r\n",
|
connection: other\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_upgrade() {
|
fn test_conn_upgrade() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
upgrade: websockets\r\n\
|
upgrade: websockets\r\n\
|
||||||
connection: upgrade\r\n\r\n",
|
connection: upgrade\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert!(req.upgrade());
|
assert!(req.upgrade());
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
||||||
|
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
upgrade: Websockets\r\n\
|
upgrade: Websockets\r\n\
|
||||||
connection: Upgrade\r\n\r\n",
|
connection: Upgrade\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert!(req.upgrade());
|
assert!(req.upgrade());
|
||||||
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
||||||
@ -966,59 +943,54 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_conn_upgrade_connect_method() {
|
fn test_conn_upgrade_connect_method() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"CONNECT /test HTTP/1.1\r\n\
|
"CONNECT /test HTTP/1.1\r\n\
|
||||||
content-type: text/plain\r\n\r\n",
|
content-type: text/plain\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert!(req.upgrade());
|
assert!(req.upgrade());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_headers_content_length_err_1() {
|
fn test_headers_bad_content_length() {
|
||||||
let mut buf = BytesMut::from(
|
// string CL
|
||||||
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
content-length: line\r\n\r\n",
|
content-length: line\r\n\r\n",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf)
|
// negative CL
|
||||||
}
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_headers_content_length_err_2() {
|
|
||||||
let mut buf = BytesMut::from(
|
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
content-length: -1\r\n\r\n",
|
content-length: -1\r\n\r\n",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
// octal CL
|
||||||
|
// expect_parse_err!(&mut BytesMut::from(
|
||||||
|
// "GET /test HTTP/1.1\r\n\
|
||||||
|
// content-length: 0123\r\n\r\n",
|
||||||
|
// ));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_header() {
|
fn test_invalid_header() {
|
||||||
let mut buf = BytesMut::from(
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
test line\r\n\r\n",
|
test line\r\n\r\n",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_name() {
|
fn test_invalid_name() {
|
||||||
let mut buf = BytesMut::from(
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
test[]: line\r\n\r\n",
|
test[]: line\r\n\r\n",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_bad_status_line() {
|
fn test_http_request_bad_status_line() {
|
||||||
let mut buf = BytesMut::from("getpath \r\n\r\n");
|
expect_parse_err!(&mut BytesMut::from("getpath \r\n\r\n"));
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1058,11 +1030,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_parser_utf8() {
|
fn test_http_request_parser_utf8() {
|
||||||
let mut buf = BytesMut::from(
|
let req = parse_ready!(&mut BytesMut::from(
|
||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
x-test: тест\r\n\r\n",
|
x-test: тест\r\n\r\n",
|
||||||
);
|
));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
req.headers().get("x-test").unwrap().as_bytes(),
|
req.headers().get("x-test").unwrap().as_bytes(),
|
||||||
@ -1072,24 +1043,18 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_parser_two_slashes() {
|
fn test_http_request_parser_two_slashes() {
|
||||||
let mut buf = BytesMut::from("GET //path HTTP/1.1\r\n\r\n");
|
let req = parse_ready!(&mut BytesMut::from("GET //path HTTP/1.1\r\n\r\n"));
|
||||||
let req = parse_ready!(&mut buf);
|
|
||||||
|
|
||||||
assert_eq!(req.path(), "//path");
|
assert_eq!(req.path(), "//path");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_parser_bad_method() {
|
fn test_http_request_parser_bad_method() {
|
||||||
let mut buf = BytesMut::from("!12%()+=~$ /get HTTP/1.1\r\n\r\n");
|
expect_parse_err!(&mut BytesMut::from("!12%()+=~$ /get HTTP/1.1\r\n\r\n"));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_http_request_parser_bad_version() {
|
fn test_http_request_parser_bad_version() {
|
||||||
let mut buf = BytesMut::from("GET //get HT/11\r\n\r\n");
|
expect_parse_err!(&mut BytesMut::from("GET //get HT/11\r\n\r\n"));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1106,47 +1071,41 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hrs_multiple_content_length() {
|
fn hrs_multiple_content_length() {
|
||||||
let mut buf = BytesMut::from(
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"GET / HTTP/1.1\r\n\
|
"GET / HTTP/1.1\r\n\
|
||||||
Host: example.com\r\n\
|
Host: example.com\r\n\
|
||||||
Content-Length: 4\r\n\
|
Content-Length: 4\r\n\
|
||||||
Content-Length: 2\r\n\
|
Content-Length: 2\r\n\
|
||||||
\r\n\
|
\r\n\
|
||||||
abcd",
|
abcd",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
|
|
||||||
let mut buf = BytesMut::from(
|
|
||||||
"GET / HTTP/1.1\r\n\
|
"GET / HTTP/1.1\r\n\
|
||||||
Host: example.com\r\n\
|
Host: example.com\r\n\
|
||||||
Content-Length: 0\r\n\
|
Content-Length: 0\r\n\
|
||||||
Content-Length: 2\r\n\
|
Content-Length: 2\r\n\
|
||||||
\r\n\
|
\r\n\
|
||||||
ab",
|
ab",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hrs_content_length_plus() {
|
fn hrs_content_length_plus() {
|
||||||
let mut buf = BytesMut::from(
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"GET / HTTP/1.1\r\n\
|
"GET / HTTP/1.1\r\n\
|
||||||
Host: example.com\r\n\
|
Host: example.com\r\n\
|
||||||
Content-Length: +3\r\n\
|
Content-Length: +3\r\n\
|
||||||
\r\n\
|
\r\n\
|
||||||
000",
|
000",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hrs_te_http10() {
|
fn hrs_te_http10() {
|
||||||
// in HTTP/1.0 transfer encoding is ignored and must therefore contain a CL header
|
// in HTTP/1.0 transfer encoding is ignored and must therefore contain a CL header
|
||||||
|
|
||||||
let mut buf = BytesMut::from(
|
expect_parse_err!(&mut BytesMut::from(
|
||||||
"POST / HTTP/1.0\r\n\
|
"POST / HTTP/1.0\r\n\
|
||||||
Host: example.com\r\n\
|
Host: example.com\r\n\
|
||||||
Transfer-Encoding: chunked\r\n\
|
Transfer-Encoding: chunked\r\n\
|
||||||
@ -1155,9 +1114,7 @@ mod tests {
|
|||||||
aaa\r\n\
|
aaa\r\n\
|
||||||
0\r\n\
|
0\r\n\
|
||||||
",
|
",
|
||||||
);
|
));
|
||||||
|
|
||||||
expect_parse_err!(&mut buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user