Skip to content

Commit ae68e82

Browse files
committed
Avoid consuming ParseOptions on parse, test reuse
1 parent 3b0ebf8 commit ae68e82

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ impl<'a> ParseOptions<'a> {
259259
}
260260

261261
/// Parse an URL string with the configuration so far.
262-
pub fn parse(self, input: &str) -> Result<Url, ::ParseError> {
262+
pub fn parse(&self, input: &str) -> Result<Url, ::ParseError> {
263263
Parser {
264264
serialization: String::with_capacity(input.len()),
265265
base_url: self.base_url,
266266
query_encoding_override: self.encoding_override,
267-
syntax_violation_callback: self.syntax_violation_callback,
267+
syntax_violation_callback: self.syntax_violation_callback.clone(),
268268
context: Context::UrlParser,
269269
}.parse_url(input)
270270
}

tests/unit.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern crate url;
1313

1414
use std::ascii::AsciiExt;
1515
use std::borrow::Cow;
16-
use std::cell::Cell;
16+
use std::cell::{Cell, RefCell};
1717
use std::net::{Ipv4Addr, Ipv6Addr};
1818
use std::path::{Path, PathBuf};
1919
use url::{Host, HostAndPort, Url, form_urlencoded};
@@ -529,3 +529,21 @@ fn test_syntax_violation_callback_lifetimes() {
529529
assert_eq!(url.path(), "/path");
530530
assert_eq!(violation.take(), Some(Backslash));
531531
}
532+
533+
#[test]
534+
fn test_options_reuse() {
535+
use url::SyntaxViolation::*;
536+
let violations = RefCell::new(Vec::new());
537+
538+
let options = Url::options().
539+
syntax_violation_callback(Some(|v| {
540+
violations.borrow_mut().push(v);
541+
}));
542+
let url = options.parse("http:////mozilla.org").unwrap();
543+
544+
let options = options.base_url(Some(&url));
545+
let url = options.parse("/sub\\path").unwrap();
546+
assert_eq!(url.as_str(), "http://mozilla.org/sub/path");
547+
assert_eq!(*violations.borrow(),
548+
vec!(ExpectedDoubleSlash, Backslash));
549+
}

0 commit comments

Comments
 (0)