1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::fmt::{Formatter, Result};
use hyper::error::{self, Error};
use hyper::header::{HeaderFormat, Header};
const SECURELOCATION_HEADER_NAME: &'static str = "SECURELOCATION.UPNP.ORG";
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct SecureLocation(pub String);
impl Header for SecureLocation {
fn header_name() -> &'static str {
SECURELOCATION_HEADER_NAME
}
fn parse_header(raw: &[Vec<u8>]) -> error::Result<Self> {
if raw.len() != 1 || raw[0].is_empty() {
return Err(Error::Header);
}
let owned_bytes = raw[0].clone();
match String::from_utf8(owned_bytes) {
Ok(n) => Ok(SecureLocation(n)),
Err(_) => Err(Error::Header),
}
}
}
impl HeaderFormat for SecureLocation {
fn fmt_header(&self, fmt: &mut Formatter) -> Result {
try!(fmt.write_str(&self.0));
Ok(())
}
}
#[cfg(test)]
mod tests {
use hyper::header::Header;
use super::SecureLocation;
#[test]
fn positive_securelocation() {
let securelocation_header_value = &[b"https://192.168.1.1/"[..].to_vec()];
SecureLocation::parse_header(securelocation_header_value).unwrap();
}
#[test]
fn positive_invalid_url() {
let securelocation_header_value = &[b"just some text"[..].to_vec()];
SecureLocation::parse_header(securelocation_header_value).unwrap();
}
#[test]
#[should_panic]
fn negative_empty() {
let securelocation_header_value = &[b""[..].to_vec()];
SecureLocation::parse_header(securelocation_header_value).unwrap();
}
#[test]
#[should_panic]
fn negative_invalid_utf8() {
let securelocation_header_value = &[b"https://192.168.1.1/\x80"[..].to_vec()];
SecureLocation::parse_header(securelocation_header_value).unwrap();
}
}