mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
Pass request headers through to agents that implement receive_web_request
This commit is contained in:
parent
15622e2c7e
commit
58f6307e2c
4 changed files with 34 additions and 9 deletions
|
@ -24,7 +24,8 @@ class WebRequestsController < ApplicationController
|
|||
if user
|
||||
agent = user.agents.find_by_id(params[:agent_id])
|
||||
if agent
|
||||
content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s)
|
||||
content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers)
|
||||
|
||||
if content.is_a?(String)
|
||||
render :text => content, :status => status || 200, :content_type => content_type || 'text/plain'
|
||||
elsif content.is_a?(Hash)
|
||||
|
@ -46,7 +47,7 @@ class WebRequestsController < ApplicationController
|
|||
secret = params[:secret]
|
||||
user.agents.of_type(Agents::UserLocationAgent).each { |agent|
|
||||
if agent.options[:secret] == secret
|
||||
agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s)
|
||||
agent.trigger_web_request(params.except(:action, :controller, :user_id, :format), request.method_symbol.to_s, request.format.to_s, request.headers)
|
||||
end
|
||||
}
|
||||
render :text => "ok"
|
||||
|
|
|
@ -95,7 +95,7 @@ class Agent < ActiveRecord::Base
|
|||
false
|
||||
end
|
||||
|
||||
def receive_web_request(params, method, format)
|
||||
def receive_web_request(params, method, format, headers={})
|
||||
# Implement me in your subclass of Agent.
|
||||
["not implemented", 404]
|
||||
end
|
||||
|
@ -149,18 +149,23 @@ class Agent < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def trigger_web_request(params, method, format)
|
||||
def trigger_web_request(params, method, format, headers)
|
||||
if respond_to?(:receive_webhook)
|
||||
Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request."
|
||||
receive_webhook(params).tap do
|
||||
self.last_web_request_at = Time.now
|
||||
save!
|
||||
end
|
||||
else
|
||||
elsif method(:receive_web_request).arity == 3
|
||||
receive_web_request(params, method, format).tap do
|
||||
self.last_web_request_at = Time.now
|
||||
save!
|
||||
end
|
||||
else
|
||||
receive_web_request(params, method, format, headers).tap do
|
||||
self.last_web_request_at = Time.now
|
||||
save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ describe WebRequestsController do
|
|||
cannot_receive_events!
|
||||
cannot_be_scheduled!
|
||||
|
||||
def receive_web_request(params, method, format)
|
||||
def receive_web_request(params, method, format, headers={})
|
||||
if params.delete(:secret) == options[:secret]
|
||||
memory[:web_request_values] = params
|
||||
memory[:web_request_format] = format
|
||||
|
|
|
@ -724,6 +724,25 @@ describe Agent do
|
|||
end
|
||||
|
||||
context "when .receive_web_request is defined" do
|
||||
before do
|
||||
@agent = Agents::WebRequestReceiver.new(:name => "something")
|
||||
@agent.user = users(:bob)
|
||||
@agent.save!
|
||||
|
||||
def @agent.receive_web_request(params, method, format, headers={})
|
||||
memory['last_request'] = [params, method, format, headers]
|
||||
['Ok!', 200]
|
||||
end
|
||||
end
|
||||
|
||||
it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
|
||||
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {})
|
||||
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {} ])
|
||||
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context "when .receive_web_request is defined without headers" do
|
||||
before do
|
||||
@agent = Agents::WebRequestReceiver.new(:name => "something")
|
||||
@agent.user = users(:bob)
|
||||
|
@ -735,8 +754,8 @@ describe Agent do
|
|||
end
|
||||
end
|
||||
|
||||
it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
|
||||
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
|
||||
it "calls the .trigger_web_request with headers, but they don't get passed to .receive_web_request" do
|
||||
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {})
|
||||
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ])
|
||||
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
|
||||
end
|
||||
|
@ -756,7 +775,7 @@ describe Agent do
|
|||
|
||||
it "outputs a deprecation warning and calls .receive_webhook with the params" do
|
||||
mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
|
||||
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
|
||||
@agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html", {})
|
||||
expect(@agent.reload.memory['last_webhook_request']).to eq({ "some_param" => "some_value" })
|
||||
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue