Skip to content

Commit

Permalink
Fix URI#to_s path starting with /
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jul 3, 2018
1 parent 42c9781 commit 165ae8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions spec/std/uri_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ describe "URI" do
it { URI.new("mailto", path: "[email protected]").to_s.should eq("mailto:[email protected]") }
it { URI.new("file", path: "/foo.html").to_s.should eq("file:/foo.html") }
it { URI.new("file", path: "foo.html").to_s.should eq("file:foo.html") }
it { URI.new("file", host: "host", path: "foo.html").to_s.should eq("file://host/foo.html") }
it { URI.new(path: "//foo").to_s.should eq("/.//foo") }
it { URI.new(host: "host", path: "//foo").to_s.should eq("//host//foo") }

it "preserves non-default port" do
URI.new("http", "www.example.com", 1234).to_s.should eq("http://www.example.com:1234")
Expand Down
13 changes: 12 additions & 1 deletion src/uri.cr
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class URI
io << scheme
io << ':'
end
io << "//" if @user || host || port

authority = @user || @host || @port
io << "//" if authority
if user = @user
userinfo(user, io)
io << '@'
Expand All @@ -160,7 +162,16 @@ class URI
if port = @port
io << ':' << port
end

if authority
if !@path.empty? && !@path.starts_with?('/')
io << '/'
end
elsif @path.starts_with?("//")
io << "/."
end
io << @path

if query
io << '?'
io << query
Expand Down

0 comments on commit 165ae8d

Please sign in to comment.