Allow Redirect Requests (#1881)

* Allow Redirect Requests

* specs

* type cast
This commit is contained in:
Thiago Talma 2017-01-28 19:58:41 -02:00 committed by Andrew Cantino
parent 80debd63bb
commit 51b9bbca17
3 changed files with 22 additions and 7 deletions

View file

@ -26,12 +26,16 @@ class WebRequestsController < ApplicationController
if agent
content, status, content_type = agent.trigger_web_request(request)
if content.is_a?(String)
render plain: content, :status => status || 200, :content_type => content_type || 'text/plain'
status = status || 200
if status.to_s.in?(["301", "302"])
redirect_to content, status: status
elsif content.is_a?(String)
render plain: content, :status => status, :content_type => content_type || 'text/plain'
elsif content.is_a?(Hash)
render :json => content, :status => status || 200
render :json => content, :status => status
else
head(status || 200)
head(status)
end
else
render plain: "agent not found", :status => 404

View file

@ -26,7 +26,7 @@ module Agents
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'.
* `code` - The response code to the request. Defaults to '201'. If the code is '301' or '302' the request will automatically be redirected to the url defined in "response".
* `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
@ -54,7 +54,7 @@ module Agents
# 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
@ -103,6 +103,10 @@ module Agents
if options['code'].present? && options['code'].to_s !~ /\A\s*(\d+|\{.*)\s*\z/
errors.add(:base, "Must specify a code for request responses")
end
if options['code'].to_s.in?(['301', '302']) && !options['response'].present?
errors.add(:base, "Must specify a url for request redirect")
end
end
def payload_for(params)

View file

@ -10,7 +10,7 @@ describe WebRequestsController do
memory[:web_request_values] = params
memory[:web_request_format] = format
memory[:web_request_method] = method
["success", 200, memory['content_type']]
["success", (options[:status] || 200).to_i, memory['content_type']]
else
["failure", 404]
end
@ -85,6 +85,13 @@ describe WebRequestsController do
expect(response.headers['Content-Type']).to eq('application/json; charset=utf-8')
end
it 'should redirect correctly' do
@agent.options['status'] = 302
@agent.save
post :handle_request, params: {:user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret"}, format: :json
expect(response).to redirect_to('success')
end
it "should fail on incorrect users" do
post :handle_request, params: {:user_id => users(:jane).to_param, :agent_id => @agent.id, :secret => "my_secret", :no => "go"}
expect(response).to be_missing