micro_http: update unit test
Add some test cases for uri test, to cover sub-domain uri and `ip:port`. Refactor `test_uri` and `test_find`. Leverage tupled vector to init test inputs and expected values, and remove superfluous lines. It will be more flexible to modify the test cases. Signed-off-by: keyangxie <keyang.xie@gmail.com> Signed-off-by: YUAN LYU <lyuyuan92@gmail.com>
This commit is contained in:
committed by
Adrian Catangiu
parent
569230220f
commit
7289d64e24
@@ -276,45 +276,34 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_uri() {
|
||||
let uri = Uri::new("http://localhost/home");
|
||||
assert_eq!(uri.get_abs_path(), "/home");
|
||||
|
||||
let uri = Uri::new("/home");
|
||||
assert_eq!(uri.get_abs_path(), "/home");
|
||||
|
||||
let uri = Uri::new("home");
|
||||
assert_eq!(uri.get_abs_path(), "");
|
||||
|
||||
let uri = Uri::new("http://");
|
||||
assert_eq!(uri.get_abs_path(), "");
|
||||
|
||||
let uri = Uri::new("http://192.168.0.0");
|
||||
assert_eq!(uri.get_abs_path(), "");
|
||||
for tc in &vec![
|
||||
("http://localhost/home", "/home"),
|
||||
("http://localhost:8080/home", "/home"),
|
||||
("http://localhost/home/sub", "/home/sub"),
|
||||
("/home", "/home"),
|
||||
("home", ""),
|
||||
("http://", ""),
|
||||
("http://192.168.0.0", ""),
|
||||
] {
|
||||
assert_eq!(Uri::new(tc.0).get_abs_path(), tc.1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find() {
|
||||
let bytes: &[u8; 13] = b"abcacrgbabsjl";
|
||||
let i = find(&bytes[..], b"ac");
|
||||
assert_eq!(i.unwrap(), 3);
|
||||
|
||||
let i = find(&bytes[..], b"rgb");
|
||||
assert_eq!(i.unwrap(), 5);
|
||||
|
||||
let i = find(&bytes[..], b"ab");
|
||||
assert_eq!(i.unwrap(), 0);
|
||||
|
||||
let i = find(&bytes[..], b"l");
|
||||
assert_eq!(i.unwrap(), 12);
|
||||
|
||||
let i = find(&bytes[..], b"jle");
|
||||
assert!(i.is_none());
|
||||
|
||||
let i = find(&bytes[..], b"asdkjhasjhdjhgsadg");
|
||||
assert!(i.is_none());
|
||||
|
||||
let i = find(&bytes[..], b"abcacrgbabsjl");
|
||||
assert_eq!(i.unwrap(), 0);
|
||||
for tc in &vec![
|
||||
("ac", Some(3)),
|
||||
("rgb", Some(5)),
|
||||
("ab", Some(0)),
|
||||
("l", Some(12)),
|
||||
("abcacrgbabsjl", Some(0)),
|
||||
("jle", None),
|
||||
("asdkjhasjhdjhgsadg", None),
|
||||
] {
|
||||
assert_eq!(find(&bytes[..], tc.0.as_bytes()), tc.1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user