Skip to content

Commit 06012a9

Browse files
authored
Fix missing / for non-special URLs (#603)
Fixes #579
1 parent 0a18f93 commit 06012a9

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/parser.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,10 @@ impl<'a> Parser<'a> {
11701170
// The query and path states will be handled by the caller.
11711171
return input;
11721172
}
1173+
1174+
if maybe_c != None && maybe_c != Some('/') {
1175+
self.serialization.push('/');
1176+
}
11731177
// Otherwise, if c is not the EOF code point:
11741178
self.parse_path(scheme_type, has_host, path_start, input)
11751179
}
@@ -1293,6 +1297,7 @@ impl<'a> Parser<'a> {
12931297
self.serialization.push('/');
12941298
self.serialization.push_str(&path.trim_start_matches("/"));
12951299
}
1300+
12961301
input
12971302
}
12981303

tests/unit.rs

+36
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,39 @@ fn test_url_from_file_path() {
619619
let path = u.to_file_path().unwrap();
620620
assert_eq!("/c:/", path.to_str().unwrap());
621621
}
622+
623+
#[test]
624+
fn test_non_special_path() {
625+
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
626+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
627+
db_url.set_path("diesel_foo");
628+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/diesel_foo");
629+
assert_eq!(db_url.path(), "/diesel_foo");
630+
}
631+
632+
#[test]
633+
fn test_non_special_path2() {
634+
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
635+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
636+
db_url.set_path("");
637+
assert_eq!(db_url.path(), "");
638+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost");
639+
db_url.set_path("foo");
640+
assert_eq!(db_url.path(), "/foo");
641+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo");
642+
db_url.set_path("/bar");
643+
assert_eq!(db_url.path(), "/bar");
644+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/bar");
645+
}
646+
647+
#[test]
648+
fn test_non_special_path3() {
649+
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap();
650+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
651+
db_url.set_path("/");
652+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/");
653+
assert_eq!(db_url.path(), "/");
654+
db_url.set_path("/foo");
655+
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo");
656+
assert_eq!(db_url.path(), "/foo");
657+
}

0 commit comments

Comments
 (0)