2

在我的 Rails 应用程序中,我有一个通过外部服务 Amazon FPS 重定向的表单。表单发布到我的应用程序中的一个操作,该操作重定向到亚马逊,亚马逊收集信息然后重定向回我的应用程序。

我正在用 Webrat 测试这个工作流程。显然我无法测试亚马逊,所以我想检查到亚马逊的重定向是否发生,然后模拟亚马逊重定向回我的应用程序,有效地从测试中模拟出亚马逊。

但是,当 Webrat 提交表单时,它会调用ActionController::Integration::Session#request_via_redirect,它会跟随所有重定向,直到它得到一个不是重定向的响应。这包括重定向到亚马逊。Rails 忽略域并从本地应用程序请求路径,但失败。

我正在寻找的是一种阻止 Webrat/Rails 在其他域上请求 URL 并允许我验证重定向的方法。

4

1 回答 1

2

解决方案:我自己的方式。

class ActionController::Integration::Session
  # Intercepts a request to a foreign domain.  Use this to stub
  # a service which the user is bounced through, such as an
  # OpenID provider.  The block should return a new URL to
  # request.  This is the URL which the foreign service would
  # redirect the browser to if we were really using it.
  # 
  # Currently, the return URL can only be requested with a GET.
  # 
  #   stub_request 'foreign.host.com' do |path|
  #     return_from_bounce_url
  #   end
  def stub_request(host, &block)
    @request_stubs ||= {}
    @request_stubs[host] = block
  end

  def process_with_stubs(method, path, parameters = nil, headers = nil)
    @request_stubs ||= {}

    if @request_stubs.key? self.host
      url = @request_stubs[host].call(path)
      process_without_stubs(method, url, parameters, headers)
    else
      process_without_stubs(method, path, parameters, headers)
    end
  end
  alias_method_chain :process, :stubs
end
于 2008-12-19T20:11:37.520 回答