Skip to content

Commit

Permalink
Option to set response code for webhook agent (huginn#1676)
Browse files Browse the repository at this point in the history
* setting for webhook response code

* some adjustments
  • Loading branch information
thiagotalma authored and cantino committed Sep 9, 2016
1 parent c6a17c9 commit c3d5380
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/models/agents/webhook_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class WebhookAgent < Agent
For example, "post,get" will enable POST and GET requests. Defaults
to "post".
* `response` - The response message to the request. Defaults to 'Event Created'.
* `code` - The response code to the request. Defaults to '201'.
* `recaptcha_secret` - Setting this to a reCAPTCHA "secret" key makes your agent verify incoming requests with reCAPTCHA. Don't forget to embed a reCAPTCHA snippet including your "site" key in the originating form(s).
* `recaptcha_send_remote_addr` - Set this to true if your server is properly configured to set REMOTE_ADDR to the IP address of each visitor (instead of that of a proxy server).
MD
Expand Down Expand Up @@ -53,6 +54,9 @@ def receive_web_request(params, method, format)
# check the verbs
verbs = (interpolated['verbs'] || 'post').split(/,/).map { |x| x.strip.downcase }.select { |x| x.present? }
return ["Please use #{verbs.join('/').upcase} requests only", 401] unless verbs.include?(method)

# check the code
code = (interpolated['code'].presence || 201).to_i

# check the reCAPTCHA response if required
if recaptcha_secret = interpolated['recaptcha_secret'].presence
Expand Down Expand Up @@ -84,7 +88,7 @@ def receive_web_request(params, method, format)
create_event(payload: payload)
end

[response_message, 201]
[response_message, code]
end

def working?
Expand All @@ -95,6 +99,10 @@ def validate_options
unless options['secret'].present?
errors.add(:base, "Must specify a secret for 'Authenticating' requests")
end

if options['code'].present? && options['code'].to_s !~ /\A\s*(\d+|\{.*)\s*\z/
errors.add(:base, "Must specify a code for request responses")
end
end

def payload_for(params)
Expand Down
20 changes: 20 additions & 0 deletions spec/models/agents/webhook_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@
expect(out).to eq(['Event Created', 201])
end

it 'should respond with customized response code if configured with `code` option' do
agent.options['code'] = '200'
out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
expect(out).to eq(['Event Created', 200])
end

it 'should respond with `201` if the code option is empty, nil or missing' do
agent.options['code'] = ''
out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
expect(out).to eq(['Event Created', 201])

agent.options['code'] = nil
out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
expect(out).to eq(['Event Created', 201])

agent.options.delete('code')
out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
expect(out).to eq(['Event Created', 201])
end

describe "receiving events" do

context "default settings" do
Expand Down

0 comments on commit c3d5380

Please sign in to comment.