如何测试重定向到外部URL?

时间:2012-12-14 21:51:36

标签: ruby-on-rails url testing ruby-on-rails-3.2 http-redirect

我正在使用rack-rewrite重定向旧版网址。重定向到新内部路径的旧路径,我可以测试得很好,像这样:

require 'test_helper'

class MiscellaneousTest < ActionDispatch::IntegrationTest

    test 'redirect about.php' do
        get_via_redirect '/about.php?country=US'
        assert_equal '/us/about', path

        get_via_redirect '/about.php?country=CA'
        assert_equal '/ca/about', path

        # default
        get_via_redirect '/about.php'
        assert_equal '/ca/about', path
    end
end

但令我疑惑的是:我如何测试旧路径现在映射到外部URL的案例?

例如,如果检测到以下旧路线:/blog/12345/something-cool-to-say它现在将映射到以下外部网址:http://blog.far.far.away.com/12345/something-cool-to-say通过:

$BLOG_URL = 'http://blog.far.far.away-example.com'
r301 %r{^/blog/([0-9]{5})/(.*)\.php}, "#{BLOG_URL}/$1/$2" # token, slug

1 个答案:

答案 0 :(得分:1)

试试这个:

assert_redirected_to 'http://some.host/with/a/path'