Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostAgent: allow sending arbitrary string data #1402

Merged
merged 1 commit into from
Apr 4, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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` contains a [MIME](https://en.wikipedia.org/wiki/Media_type) type, and `payload` is a string, its interpolated value will be sent as a string in the HTTP request's body and the request's `Content-Type` HTTP header will be set to `content_type`. When `payload` is a string `no_merge` has to be set to `true`.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head too? Or do we not support those?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not support those at the moment.

errors.add(:base, "if provided, payload must be a hash")
end

if options['payload'].present? && %w[post put patch].include?(method)
if !options['payload'].is_a?(Hash) && options['content_type'] !~ MIME_RE
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