2

Railsアプリには、外国のサービスであるAmazonFPSを介してリダイレクトするフォームがあります。フォームは、情報を収集してからアプリにリダイレクトするAmazonにリダイレクトするアプリ内のアクションにPOSTします。

このワークフローをWebratでテストしています。明らかにAmazonをテストすることはできないので、Amazonへのリダイレクトが行われることを確認してから、Amazonのリダイレクトをシミュレートしてアプリに戻し、テストからAmazonを効果的にモックアウトしたいと思います。

ただし、Webratがフォームを送信するとActionController::Integration::Session#request_via_redirect、を呼び出します。これは、リダイレクトではない応答を取得するまで、すべてのリダイレクトに従います。これには、Amazonへのリダイレクトが含まれます。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 に答える