Skip to content

Commit

Permalink
PostAgent: allow sending arbitrary string data
Browse files Browse the repository at this point in the history
When `content_type` contains a MIME type and `payload` is not a Hash the string in `payload` will be send as the HTTP
body and `content_type` is send as the `Content-Type` header.

 #1361
  • Loading branch information
dsander committed Apr 2, 2016
1 parent 0755451 commit d500e76
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/models/agents/post_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Agents
class PostAgent < Agent
include WebRequestConcern

MIME_RE = /\A\w+\/.+\z/

can_dry_run!
no_bulk_receive!
default_schedule "never"
Expand All @@ -13,7 +15,13 @@ class PostAgent < Agent
The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`). Change `content_type` to `json` to send JSON instead. Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`).
Change `content_type` to `json` to send JSON instead.
Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
When `content_type` containts a [MIME](https://en.wikipedia.org/wiki/Media_type) type, the interpolated value of `payload` will be send as a string in the HTTP body message and `content_type` will be set as the `Content-Type` HTTP header.
If `emit_events` is set to `true`, the server response will be emitted as an Event and can be fed to a WebsiteAgent for parsing (using its `data_from_event` and `type` options). No data processing
will be attempted by this Agent, so the Event's "body" value will always be raw text.
Expand Down Expand Up @@ -49,7 +57,8 @@ def default_options
'something' => 'the event contained {{ somekey }}'
},
'headers' => {},
'emit_events' => 'false'
'emit_events' => 'false',
'no_merge' => 'false'
}
end

Expand All @@ -66,10 +75,19 @@ def validate_options
errors.add(:base, "post_url and expected_receive_period_in_days are required fields")
end

if options['payload'].present? && !options['payload'].is_a?(Hash)
if options['payload'].present? && %w[get delete].include?(method) && !options['payload'].is_a?(Hash)
errors.add(:base, "if provided, payload must be a hash")
end

if options['payload'].present? && %w[post put patch].include?(method)
if !(options['content_type'] =~ MIME_RE || options['payload'].is_a?(Hash))
errors.add(:base, "if provided, payload must be a hash")
end
if options['content_type'] =~ MIME_RE && options['payload'].is_a?(String) && boolify(options['no_merge']) != true
errors.add(:base, "when the payload is a string, `no_merge` has to be set to `true`")
end
end

if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
errors.add(:base, "if provided, emit_events must be true or false")
end
Expand Down Expand Up @@ -116,13 +134,16 @@ def handle(data, payload = {})
when 'post', 'put', 'patch'
params = nil

case interpolated(payload)['content_type']
case (content_type = interpolated(payload)['content_type'])
when 'json'
headers['Content-Type'] = 'application/json; charset=utf-8'
body = data.to_json
when 'xml'
headers['Content-Type'] = 'text/xml; charset=utf-8'
body = data.to_xml(root: (interpolated(payload)[:xml_root] || 'post'))
when MIME_RE
headers['Content-Type'] = content_type
body = data.to_s
else
body = data
end
Expand Down
31 changes: 31 additions & 0 deletions spec/models/agents/post_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
req.data = ActiveSupport::JSON.decode(request.body)
when 'text/xml'
req.data = Hash.from_xml(request.body)
when Agents::PostAgent::MIME_RE
req.data = request.body
else
raise "unexpected Content-Type: #{content_type}"
end
Expand Down Expand Up @@ -187,6 +189,16 @@
expect(@sent_requests[:get][0].data).to eq(@checker.options['payload'].to_query)
end

it "sends options['payload'] as a string POST request when content-type continas a MIME type" do
@checker.options['payload'] = '<test>hello</test>'
@checker.options['content_type'] = 'application/xml'
expect {
@checker.check
}.to change { @sent_requests[:post].length }.by(1)

expect(@sent_requests[:post][0].data).to eq('<test>hello</test>')
end

describe "emitting events" do
context "when emit_events is not set to true" do
it "does not emit events" do
Expand Down Expand Up @@ -304,6 +316,25 @@
expect(@checker).to be_valid
end

it "should not validate payload as a hash if content_type includes a MIME type and method is not get or delete" do
@checker.options['no_merge'] = 'true'
@checker.options['content_type'] = 'text/xml'
@checker.options['payload'] = "test"
expect(@checker).to be_valid

@checker.options['method'] = 'get'
expect(@checker).not_to be_valid

@checker.options['method'] = 'delete'
expect(@checker).not_to be_valid
end

it "requires `no_merge` to be set to true when content_type contains a MIME type" do
@checker.options['content_type'] = 'text/xml'
@checker.options['payload'] = "test"
expect(@checker).not_to be_valid
end

it "requires headers to be a hash, if present" do
@checker.options['headers'] = [1,2,3]
expect(@checker).not_to be_valid
Expand Down

0 comments on commit d500e76

Please sign in to comment.