Prechádzať zdrojové kódy

Fix missing / for non-special URLs (#603)

Fixes #579
Valentin Gosu 6 rokov pred
rodič
commit
06012a9241
2 zmenil súbory, kde vykonal 41 pridanie a 0 odobranie
  1. 5 0
      src/parser.rs
  2. 36 0
      tests/unit.rs

+ 5 - 0
src/parser.rs

@@ -1170,6 +1170,10 @@ impl<'a> Parser<'a> {
             // The query and path states will be handled by the caller.
             return input;
         }
+
+        if maybe_c != None && maybe_c != Some('/') {
+            self.serialization.push('/');
+        }
         // Otherwise, if c is not the EOF code point:
         self.parse_path(scheme_type, has_host, path_start, input)
     }
@@ -1293,6 +1297,7 @@ impl<'a> Parser<'a> {
             self.serialization.push('/');
             self.serialization.push_str(&path.trim_start_matches("/"));
         }
+
         input
     }
 

+ 36 - 0
tests/unit.rs

@@ -619,3 +619,39 @@ fn test_url_from_file_path() {
     let path = u.to_file_path().unwrap();
     assert_eq!("/c:/", path.to_str().unwrap());
 }
+
+#[test]
+fn test_non_special_path() {
+    let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
+    db_url.set_path("diesel_foo");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/diesel_foo");
+    assert_eq!(db_url.path(), "/diesel_foo");
+}
+
+#[test]
+fn test_non_special_path2() {
+    let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
+    db_url.set_path("");
+    assert_eq!(db_url.path(), "");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost");
+    db_url.set_path("foo");
+    assert_eq!(db_url.path(), "/foo");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo");
+    db_url.set_path("/bar");
+    assert_eq!(db_url.path(), "/bar");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/bar");
+}
+
+#[test]
+fn test_non_special_path3() {
+    let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
+    db_url.set_path("/");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
+    assert_eq!(db_url.path(), "/");
+    db_url.set_path("/foo");
+    assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo");
+    assert_eq!(db_url.path(), "/foo");
+}