Skip to content

Commit

Permalink
Merge pull request #67 from nepalez/master
Browse files Browse the repository at this point in the history
Prevent mutation of notifier arguments
  • Loading branch information
tesssie committed Oct 26, 2017
2 parents aaeefc3 + e1e89e7 commit 2812eb9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/gelf/notifier.rb
Expand Up @@ -263,14 +263,18 @@ def serialize_hash(hash)
Zlib::Deflate.deflate(hash.to_json).bytes
end

def self.stringify_keys(hash)
hash.keys.each do |key|
value, key_s = hash.delete(key), key.to_s
raise ArgumentError.new("Both #{key.inspect} and #{key_s} are present.") if hash.key?(key_s)
value = stringify_keys(value) if value.is_a?(Hash)
hash[key_s] = value
def self.stringify_keys(data)
return data unless data.is_a? Hash

data.each_with_object({}) do |(key, value), obj|
key_s = key.to_s

if (key != key_s) && data.key?(key_s)
raise ArgumentError, "Both #{key.inspect} and #{key_s} are present."
end

obj[key_s] = value
end
hash
end
end
end
10 changes: 10 additions & 0 deletions test/test_notifier.rb
Expand Up @@ -272,6 +272,16 @@ class MyNotifier < GELF::Notifier; end
@notifier.notify!({ 'version' => '1.0', 'short_message' => 'message' })
end

should "not mutate arguments" do
data = { 'version' => '1.0', 'short_message' => 'message', foo: { bar: "BAZ" } }
original_hash = data.hash

@sender.expects(:send_datagrams)
@notifier.notify!(data)

assert_equal(data.hash, original_hash)
end

GELF::Levels.constants.each do |const|
should "call notify with level #{const} from method name" do
@notifier.expects(:notify_with_level).with(GELF.const_get(const), { 'version' => '1.0', 'short_message' => 'message' })
Expand Down

0 comments on commit 2812eb9

Please sign in to comment.