Merge pull request #1415 from albertsun/feature/request-headers-in-web-request

Pass request headers to receive_web_request
This commit is contained in:
Andrew Cantino 2016-04-15 20:55:57 -07:00
commit ee504ba067
3 changed files with 55 additions and 6 deletions

View file

@ -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(request)
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(request)
end
}
render :text => "ok"

View file

@ -100,6 +100,10 @@ class Agent < ActiveRecord::Base
["not implemented", 404]
end
# alternate method signature for receive_web_request
# def receive_web_request(request=ActionDispatch::Request.new( ... ))
# end
# Implement me in your subclass to decide if your Agent is working.
def working?
raise "Implement me in your subclass"
@ -149,7 +153,8 @@ class Agent < ActiveRecord::Base
end
end
def trigger_web_request(params, method, format)
def trigger_web_request(request)
params = request.params.except(:action, :controller, :agent_id, :user_id, :format)
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
@ -157,7 +162,12 @@ class Agent < ActiveRecord::Base
save!
end
else
receive_web_request(params, method, format).tap do
if method(:receive_web_request).arity == 1
handled_request = receive_web_request(request)
else
handled_request = receive_web_request(params, request.method_symbol.to_s, request.format.to_s)
end
handled_request.tap do
self.last_web_request_at = Time.now
save!
end

View file

@ -736,12 +736,44 @@ describe Agent do
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")
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html'
})
@agent.trigger_web_request(request)
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 with just request" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@agent.user = users(:bob)
@agent.save!
def @agent.receive_web_request(request)
memory['last_request'] = [request.params, request.method_symbol.to_s, request.format, {'HTTP_X_CUSTOM_HEADER' => request.headers['HTTP_X_CUSTOM_HEADER']}]
['Ok!', 200]
end
end
it "calls the .trigger_web_request with headers, and they get passed to .receive_web_request" do
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html',
'HTTP_X_CUSTOM_HEADER' => "foo"
})
@agent.trigger_web_request(request)
expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html", {'HTTP_X_CUSTOM_HEADER' => "foo"} ])
expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
end
end
context "when .receive_webhook is defined" do
before do
@agent = Agents::WebRequestReceiver.new(:name => "something")
@ -755,8 +787,14 @@ describe Agent do
end
it "outputs a deprecation warning and calls .receive_webhook with the params" do
request = ActionDispatch::Request.new({
'action_dispatch.request.request_parameters' => { :some_param => "some_value" },
'REQUEST_METHOD' => "POST",
'HTTP_ACCEPT' => 'text/html'
})
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(request)
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