Merge pull request #6659 from FedericoCeratto/Uri-string

Make Uri rendering more lenient. Closes #6652
This commit is contained in:
Dominik Picheta 2017-11-16 21:45:03 +00:00 • committed by GitHub
commit 5883d727a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -308,11 +308,16 @@ proc `$`*(u: Uri): string =
result.add(":") result.add(":")
result.add(u.password) result.add(u.password)
result.add("@") result.add("@")
result.add(u.hostname) if u.hostname.endswith('/'):
result.add(u.hostname[0..^2])
else:
result.add(u.hostname)
if u.port.len > 0: if u.port.len > 0:
result.add(":") result.add(":")
result.add(u.port) result.add(u.port)
if u.path.len > 0: if u.path.len > 0:
if u.hostname.len > 0 and u.path[0] != '/':
result.add('/')
result.add(u.path) result.add(u.path)
if u.query.len > 0: if u.query.len > 0:
result.add("?") result.add("?")
@ -483,6 +488,34 @@ when isMainModule:
let foo = parseUri("http://localhost:9515") / "status" let foo = parseUri("http://localhost:9515") / "status"
doAssert $foo == "http://localhost:9515/status" doAssert $foo == "http://localhost:9515/status"
# bug #6649 #6652
block:
var foo = parseUri("http://example.com")
foo.hostname = "example.com"
foo.path = "baz"
doAssert $foo == "http://example.com/baz"
foo.hostname = "example.com/"
foo.path = "baz"
doAssert $foo == "http://example.com/baz"
foo.hostname = "example.com"
foo.path = "/baz"
doAssert $foo == "http://example.com/baz"
foo.hostname = "example.com/"
foo.path = "/baz"
doAssert $foo == "http://example.com/baz"
foo.hostname = "example.com/"
foo.port = "8000"
foo.path = "baz"
doAssert $foo == "http://example.com:8000/baz"
foo = parseUri("file:/dir/file")
foo.path = "relative"
doAssert $foo == "file:relative"
# isAbsolute tests # isAbsolute tests
block: block:
doAssert "www.google.com".parseUri().isAbsolute() == false doAssert "www.google.com".parseUri().isAbsolute() == false
@ -524,4 +557,4 @@ when isMainModule:
doAssert "https://example.com/about/staff.html?".parseUri().isAbsolute == true doAssert "https://example.com/about/staff.html?".parseUri().isAbsolute == true
doAssert "https://example.com/about/staff.html?parameters".parseUri().isAbsolute == true doAssert "https://example.com/about/staff.html?parameters".parseUri().isAbsolute == true
echo("All good!") echo("All good!")