Просмотр исходного кода

Truncate the serialization in Url::take_after_path, discard empty segments in PathSegmentsMut::extend.

Fixes #222
Emilio Cobos Álvarez 10 лет назад
Родитель
Сommit
5a77f26407
2 измененных файлов с 25 добавлено и 1 удалено
  1. 5 1
      src/lib.rs
  2. 20 0
      tests/unit.rs

+ 5 - 1
src/lib.rs

@@ -791,7 +791,11 @@ impl Url {
 
     fn take_after_path(&mut self) -> (u32, String) {
         match (self.query_start, self.fragment_start) {
-            (Some(i), _) | (None, Some(i)) => (i, self.slice(i..).to_owned()),
+            (Some(i), _) | (None, Some(i)) => {
+                let after_path = self.slice(i..).to_owned();
+                self.serialization.truncate(i as usize);
+                (i, after_path)
+            },
             (None, None) => (to_u32(self.serialization.len()).unwrap(), String::new())
         }
     }

+ 20 - 0
tests/unit.rs

@@ -268,3 +268,23 @@ fn issue_197() {
     assert_eq!(url, Url::parse("file:///").unwrap());
     url.path_segments_mut().unwrap().pop_if_empty();
 }
+
+#[test]
+/// https://github.com/servo/rust-url/issues/222
+fn append_trailing_slash() {
+    let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
+    url.assert_invariants();
+    url.path_segments_mut().unwrap().push("");
+    url.assert_invariants();
+    assert_eq!(url.to_string(), "http://localhost:6767/foo/bar/?a=b");
+}
+
+#[test]
+/// https://github.com/servo/rust-url/issues/222
+fn append_empty_segment_then_mutate() {
+    let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
+    url.assert_invariants();
+    url.path_segments_mut().unwrap().push("").pop();
+    url.assert_invariants();
+    assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b");
+}