Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/protocol/url/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def parameters?
#
# Afterwards, the `query` attribute will be cleared.
#
# If there is no query string or existing parameters, an empty mutable hash is created.
#
# @returns [Hash] The merged parameters.
def parse_query!(encoding = Encoding)
if @query and !@query.empty?
Expand All @@ -146,7 +148,7 @@ def parse_query!(encoding = Encoding)
@query = nil
end

return @parameters
return @parameters ||= {}
end

# @returns [Boolean] Whether the reference has a query string.
Expand Down
10 changes: 10 additions & 0 deletions test/protocol/url/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@
expect(reference.parameters).to be == {"foo" => "bar"}
end

it "creates mutable parameters when there is no query string" do
reference = subject.parse("/path")

parameters = reference.parse_query!
parameters["foo"] = "bar"

expect(reference.parameters).to be == {"foo" => "bar"}
expect(reference.to_s).to be == "/path?foo=bar"
end

it "updates to_s output after parsing" do
reference = subject.parse("/path?foo=bar&baz=qux")
expect(reference.to_s).to be == "/path?foo=bar&baz=qux"
Expand Down