Skip to content

Commit

Permalink
Allow PostAgent headers to interpolate event data
Browse files Browse the repository at this point in the history
  • Loading branch information
cantino committed Jul 25, 2016
1 parent 8aed682 commit 9ca9a15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
17 changes: 9 additions & 8 deletions app/models/agents/post_agent.rb
Expand Up @@ -122,17 +122,19 @@ def validate_options

def receive(incoming_events)
incoming_events.each do |event|
outgoing = interpolated(event)['payload'].presence || {}
if boolify(interpolated['no_merge'])
handle outgoing, event.payload
else
handle outgoing.merge(event.payload), event.payload
interpolate_with(event) do
outgoing = interpolated['payload'].presence || {}
if boolify(interpolated['no_merge'])
handle outgoing, event.payload, headers(interpolated[:headers])
else
handle outgoing.merge(event.payload), event.payload, headers(interpolated[:headers])
end
end
end
end

def check
handle interpolated['payload'].presence || {}
handle interpolated['payload'].presence || {}, headers
end

private
Expand Down Expand Up @@ -160,9 +162,8 @@ def normalize_response_headers(headers)
}
end

def handle(data, payload = {})
def handle(data, payload = {}, headers)
url = interpolated(payload)[:post_url]
headers = headers()

case method
when 'get', 'delete'
Expand Down
23 changes: 22 additions & 1 deletion spec/models/agents/post_agent_spec.rb
Expand Up @@ -33,7 +33,7 @@
stub_request(:any, /:/).to_return { |request|
method = request.method
@requests += 1
@sent_requests[method] << req = OpenStruct.new(uri: request.uri)
@sent_requests[method] << req = OpenStruct.new(uri: request.uri, headers: request.headers)
case method
when :get, :delete
req.data = request.uri.query
Expand Down Expand Up @@ -137,6 +137,18 @@
expect(uri.path).to eq('/a_variable')
expect(uri.query).to eq("existing_param=existing_value")
end

it "interpolates outgoing headers with the event payload" do
@checker.options['headers'] = {
"Foo" => "{{ variable }}"
}
@event.payload = {
'variable' => 'a_variable'
}
@checker.receive([@event])
headers = @sent_requests[:post].first.headers
expect(headers["Foo"]).to eq("a_variable")
end
end

describe "#check" do
Expand Down Expand Up @@ -199,6 +211,15 @@
expect(@sent_requests[:post][0].data).to eq('<test>hello</test>')
end

it "interpolates outgoing headers" do
@checker.options['headers'] = {
"Foo" => "{% credential aws_key %}"
}
@checker.check
headers = @sent_requests[:post].first.headers
expect(headers["Foo"]).to eq("2222222222-jane")
end

describe "emitting events" do
context "when emit_events is not set to true" do
it "does not emit events" do
Expand Down

0 comments on commit 9ca9a15

Please sign in to comment.