Rename constructor to match convention, add doc comment.

This commit is contained in:
Corey Farwell 2016-04-17 11:54:30 -04:00
parent a87fa103b8
commit 52b3226d54
2 changed files with 6 additions and 5 deletions

View file

@ -63,7 +63,8 @@ impl HSTSList {
}
}
pub fn new_from_preload(preload_content: &str) -> Option<HSTSList> {
/// Create an `HSTSList` from the contents of a JSON preload file.
pub fn from_preload(preload_content: &str) -> Option<HSTSList> {
decode(preload_content).ok()
}
@ -114,7 +115,7 @@ impl HSTSList {
pub fn preload_hsts_domains() -> Option<HSTSList> {
read_resource_file("hsts_preload.json").ok().and_then(|bytes| {
from_utf8(&bytes).ok().and_then(|hsts_preload_content| {
HSTSList::new_from_preload(hsts_preload_content)
HSTSList::from_preload(hsts_preload_content)
})
})
}

View file

@ -151,13 +151,13 @@ fn test_push_entry_to_hsts_list_should_add_an_entry() {
#[test]
fn test_parse_hsts_preload_should_return_none_when_json_invalid() {
let mock_preload_content = "derp";
assert!(HSTSList::new_from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
}
#[test]
fn test_parse_hsts_preload_should_return_none_when_json_contains_no_entries_key() {
let mock_preload_content = "{\"nothing\": \"to see here\"}";
assert!(HSTSList::new_from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
assert!(HSTSList::from_preload(mock_preload_content).is_none(), "invalid preload list should not have parsed")
}
#[test]
@ -168,7 +168,7 @@ fn test_parse_hsts_preload_should_decode_host_and_includes_subdomains() {
\"include_subdomains\": false}\
]\
}";
let hsts_list = HSTSList::new_from_preload(mock_preload_content);
let hsts_list = HSTSList::from_preload(mock_preload_content);
let entries = hsts_list.unwrap().entries;
assert_eq!(entries[0].host, "mozilla.org");