Skip to content

Commit

Permalink
Merge pull request #708 from BramBonne/test-syntax-violations
Browse files Browse the repository at this point in the history
Add tests for SyntaxViolation callback types
  • Loading branch information
valenting committed May 4, 2021
2 parents c6f60fe + 744b763 commit 991e438
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,38 @@ fn test_syntax_violation_callback_lifetimes() {
assert_eq!(violation.take(), Some(Backslash));
}

#[test]
fn test_syntax_violation_callback_types() {
use url::SyntaxViolation::*;

let data = [
("http://mozilla.org/\\foo", Backslash, "backslash"),
(" http://mozilla.org", C0SpaceIgnored, "leading or trailing control or space character are ignored in URLs"),
("http://user:pass@mozilla.org", EmbeddedCredentials, "embedding authentication information (username or password) in an URL is not recommended"),
("http:///mozilla.org", ExpectedDoubleSlash, "expected //"),
("file:/foo.txt", ExpectedFileDoubleSlash, "expected // after file:"),
("file://mozilla.org/c:/file.txt", FileWithHostAndWindowsDrive, "file: with host and Windows drive letter"),
("http://mozilla.org/^", NonUrlCodePoint, "non-URL code point"),
("http://mozilla.org/#\00", NullInFragment, "NULL characters are ignored in URL fragment identifiers"),
("http://mozilla.org/%1", PercentDecode, "expected 2 hex digits after %"),
("http://mozilla.org\t/foo", TabOrNewlineIgnored, "tabs or newlines are ignored in URLs"),
("http://user@:pass@mozilla.org", UnencodedAtSign, "unencoded @ sign in username or password")
];

for test_case in &data {
let violation = Cell::new(None);
Url::options()
.syntax_violation_callback(Some(&|v| violation.set(Some(v))))
.parse(test_case.0)
.unwrap();

let v = violation.take();
assert_eq!(v, Some(test_case.1));
assert_eq!(v.unwrap().description(), test_case.2);
assert_eq!(v.unwrap().to_string(), test_case.2);
}
}

#[test]
fn test_options_reuse() {
use url::SyntaxViolation::*;
Expand Down

0 comments on commit 991e438

Please sign in to comment.