Skip to content
Open
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
5 changes: 3 additions & 2 deletions lib/cronitor/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ def ping(params = {})
query.compact!
metrics = query[:metric]
query.delete(:metric)
out = query.map { |k, v| "#{k}=#{v}" }
out += metrics.map { |m| "metric=#{m}" } unless metrics.nil?
out = query.map { |k, v| "#{k}=#{URI.encode_www_form_component(v.to_s)}" }
out += metrics.map { |m| "metric=#{URI.encode_www_form_component(m)}" } unless metrics.nil?
out.join('&')
end
# query_string_normalizer for metrics. instead of metric[]=foo:1 we want metric=foo:1
# values are form-encoded so a message with &, # or + reaches the API unchanged
)

if response.code != 200
Expand Down
27 changes: 27 additions & 0 deletions spec/cronitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,33 @@
series: 'world',
} }

context 'with special characters in the message' do
before do
Cronitor.api_key = FAKE_API_KEY
end

after do
Cronitor.api_key = nil
end

it 'encodes every query value so the message is sent unchanged' do
message = "undefined method 'x' for nil & co #42 a+b=c"
query_string = nil
expect(HTTParty).to receive(:get) do |_url, opts|
query_string = opts[:query_string_normalizer].call(opts[:query])
instance_double(HTTParty::Response, code: 200)
end

monitor.ping(state: 'fail', message: message, host: 'web 1', metrics: { duration: 1.5 })

params = URI.decode_www_form(query_string)
expect(params.assoc('message')).to eq(['message', message])
expect(params.assoc('host')).to eq(['host', 'web 1'])
expect(params.assoc('metric')).to eq(['metric', 'duration:1.5'])
expect(params.map(&:first)).not_to include(' co #42')
end
end

['run', 'complete', 'fail', 'ok'].each do |state|
context "Ping #{state}" do
before(:all) do
Expand Down