diff --git a/lib/protocol/url/reference.rb b/lib/protocol/url/reference.rb index b8f94b4..b0dbee6 100644 --- a/lib/protocol/url/reference.rb +++ b/lib/protocol/url/reference.rb @@ -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? @@ -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. diff --git a/test/protocol/url/reference.rb b/test/protocol/url/reference.rb index 7ec71af..9370b41 100644 --- a/test/protocol/url/reference.rb +++ b/test/protocol/url/reference.rb @@ -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"