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:
keyangxie
2020-02-06 11:02:12 +08:00
committed by Adrian Catangiu
parent 569230220f
commit 7289d64e24
+22 -33
View File
@@ -276,45 +276,34 @@ mod tests {
#[test] #[test]
fn test_uri() { fn test_uri() {
let uri = Uri::new("http://localhost/home"); for tc in &vec![
assert_eq!(uri.get_abs_path(), "/home"); ("http://localhost/home", "/home"),
("http://localhost:8080/home", "/home"),
let uri = Uri::new("/home"); ("http://localhost/home/sub", "/home/sub"),
assert_eq!(uri.get_abs_path(), "/home"); ("/home", "/home"),
("home", ""),
let uri = Uri::new("home"); ("http://", ""),
assert_eq!(uri.get_abs_path(), ""); ("http://192.168.0.0", ""),
] {
let uri = Uri::new("http://"); assert_eq!(Uri::new(tc.0).get_abs_path(), tc.1);
assert_eq!(uri.get_abs_path(), ""); }
let uri = Uri::new("http://192.168.0.0");
assert_eq!(uri.get_abs_path(), "");
} }
#[test] #[test]
fn test_find() { fn test_find() {
let bytes: &[u8; 13] = b"abcacrgbabsjl"; let bytes: &[u8; 13] = b"abcacrgbabsjl";
let i = find(&bytes[..], b"ac");
assert_eq!(i.unwrap(), 3);
let i = find(&bytes[..], b"rgb"); for tc in &vec![
assert_eq!(i.unwrap(), 5); ("ac", Some(3)),
("rgb", Some(5)),
let i = find(&bytes[..], b"ab"); ("ab", Some(0)),
assert_eq!(i.unwrap(), 0); ("l", Some(12)),
("abcacrgbabsjl", Some(0)),
let i = find(&bytes[..], b"l"); ("jle", None),
assert_eq!(i.unwrap(), 12); ("asdkjhasjhdjhgsadg", None),
] {
let i = find(&bytes[..], b"jle"); assert_eq!(find(&bytes[..], tc.0.as_bytes()), tc.1);
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);
} }
#[test] #[test]