带有子域的水豚 - default_host

发布于 2024-11-18 01:14:19 字数 941 浏览 3 评论 0原文

我有一个使用子域来切换数据库(多租户)的应用程序。我正在尝试使用 Capybara 进行集成测试,它确实非常依赖于子域。

我的理解是,将 Capybara.default_host= 设置为某项将使我的所有请求都来自该主机。事实似乎并非如此。在这篇文章中,作者建议仅使用主机访问显式网址,但是这如果我在各处导航,就会变得有点烦人。我想只设置主机,然后能够按预期使用我的 Rails 路径。不确定我做错了什么,但这是我尝试过的:

# spec_helper.rb
RSpec.configure do |config|
  config.before(:each, :type => :request) do
    Capybara.default_host = 'http://app.mydomain.com'
  end
end

# in some_integration_spec.rb
before do
  puts "Capybara.default_host: #{Capybara.default_host}"
  puts "some_app_url: #{some_app_url}"
end

这会产生输出:

Capybara.default_host: http://app.mydomain.com
some_app_url: http://www.example.com/some_path

我做错了什么? default_host 似乎什么也没做。正如我所说,我不想说 visit(Capybara.default_host + some_app_path) 因为每次都有点烦人。为什么还要存在这个 default_host 选项?

I have an app that uses subdomains to switch databases (multi-tenancy). I'm trying to use Capybara for integration testing, and it really relies a lot on subdomains.

My understanding was that setting Capybara.default_host= to something would make all my requests come from this host. This doesn't seem to be the case. In this post, the author recommends just visiting the explicit url with a host, but this becomes a bit annoying if I'm navigating all over the place. I'd like to just set the host, then be able to use my rails paths as expected. Not sure what I'm doing wrong, but here's what I've tried:

# spec_helper.rb
RSpec.configure do |config|
  config.before(:each, :type => :request) do
    Capybara.default_host = 'http://app.mydomain.com'
  end
end

# in some_integration_spec.rb
before do
  puts "Capybara.default_host: #{Capybara.default_host}"
  puts "some_app_url: #{some_app_url}"
end

This yields the output:

Capybara.default_host: http://app.mydomain.com
some_app_url: http://www.example.com/some_path

What am I doing wrong? default_host appears to do nothing. As I say, I don't want to have to say visit(Capybara.default_host + some_app_path) as that's a bit annoying each time. Why else does this default_host option exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风柔一江水 2024-11-25 01:14:20

我不确定 default_host 的预期用途,但 app_host 可以满足您的需要。我发现我首先需要调用 Rails 会话方法 host! 以便设置将传递给请求对象中的控制器的主机字符串。

然后,您需要设置 Capybara.app_host 来告诉 Capybara 通过网络服务器调用您的应用程序,而不仅仅是在进程中进行调用。如果您不这样做,那么水豚在遇到重定向时就会出错,并在第二个请求中删除主机信息。

我不确定为什么这不会自动处理 Rails request 的事情,但我发现除非我在两个地方显式设置主机,否则我会得到不一致的结果。

def set_host (host)
  host! host
  Capybara.app_host = "http://" + host
end

before(:each) do
  set_host "lvh.me:3000"
end

然后就可以使用相对路径来访问页面了。

更新:

Capybara 2.xrspec-rails 2.12.0 引入了用于运行 Capybara 验收测试的“功能”规范。 rspec-rails 中的新 FeatureExampleGroup 模块与 RequestExampleGroup 不同,并且不再有权访问机架测试主机!方法。现在您想使用 default_url_options 代替:

def set_host (host)
  # host! host
  default_url_options[:host] = host
  Capybara.app_host = "http://" + host
end

I'm not sure of the intended use of default_host, but app_host does what you need. I've found I first need to call the rails session method host! in order to set the host string that will be passed to controllers in the request object.

Then you need to set Capybara.app_host to tell Capybara to call your app via the web server instead of just making the calls in process. If you don't do that then Capybara wigs out when it encounters redirects and drops the host information in the second request.

I'm not sure why this doesn't take care of the Rails request end of things automatically, but I've found that unless I set the host in both places explicitly, then I get inconsistent results.

def set_host (host)
  host! host
  Capybara.app_host = "http://" + host
end

before(:each) do
  set_host "lvh.me:3000"
end

Then you can just use relative paths to access pages.

Update:

Capybara 2.x and rspec-rails 2.12.0 introduced "Feature" specs for running Capybara acceptance tests. The new FeatureExampleGroup module in rspec-rails is different from RequestExampleGroup and no longer has access to the rack-test host! method. Now you want to use default_url_options instead:

def set_host (host)
  # host! host
  default_url_options[:host] = host
  Capybara.app_host = "http://" + host
end
白芷 2024-11-25 01:14:20

当您需要更改 URL 以包含子域时,您可以在步骤定义中指定 app_host。使用像 lvh.me 这样的域名,因为它指向 127.0.0.1

Capybara.app_host = "http://#{subdomain}.lvh.me"

Capybara 假定当您指定 app_host 时,您测试在端口 80 上运行的远程服务器,但在我们的例子中,我们正在测试在 Capybara 指定的随机端口上运行的本地应用程序。要解决此问题,请在您的 env.rb 文件中添加以下行:

Capybara.always_include_port = true

现在,当您访问应用程序的页面时...

visit '/page'

...该 url 将指定子域以及该子域所使用的端口。应用程序正在运行。

仅供参考:这对我使用 Capybara 2.0.2 有用。

When you need to change the URL to include the subdomain, you can specify the app_host in your step definitions. Use a domain like lvh.me since it points to 127.0.0.1:

Capybara.app_host = "http://#{subdomain}.lvh.me"

Capybara assumes that when you're specifying an app_host that you're testing a remote server running on port 80, but in our case, we're testing a local app which is running on a random port specified by Capybara. To fix this, in your env.rb file, add this line:

Capybara.always_include_port = true

Now when you visit a page of your app...

visit '/page'

...the url will specify the subdomain as well as the port that the app is running on.

FYI: This worked for me using Capybara 2.0.2.

孤千羽 2024-11-25 01:14:20

这家伙在这里有正确的答案:

http://zurb.com/forrst/posts/Testing_Subdomains_in_Capybara-g4M

你想做的事

Capybara.current_session.driver.reset!
Capybara.default_host = 'http://app.mydomain.com'

This guy has the right answer here:

http://zurb.com/forrst/posts/Testing_Subdomains_in_Capybara-g4M

You want to do

Capybara.current_session.driver.reset!
Capybara.default_host = 'http://app.mydomain.com'
沉鱼一梦 2024-11-25 01:14:20

自:

  • capybara (2.4.1)
  • capybara-webkit (1.3.0)

    Capybara.server_host = "example.com"
    水豚.server_port = 3050
    水豚.run_server = true
    Capybara.javascript_driver = :webkit #需要 capybara-webkit
    

as of:

  • capybara (2.4.1)
  • capybara-webkit (1.3.0)

    Capybara.server_host = "example.com"
    Capybara.server_port = 3050
    Capybara.run_server = true
    Capybara.javascript_driver = :webkit #requires capybara-webkit
    
与风相奔跑 2024-11-25 01:14:20

这与您的情况不完全相同,但这可能会对某些人有所帮助:

对于我当前的项目,我使用 pow 与许多子域。测试套件还必须在不同的端口上运行。

解决方案取决于您运行的水豚版本。

对于当前的最新版本,我将其放入 custom_env.rb 中:

Capybara.server_host = 'myapp.dev'
Capybara.server_port = 9887
Capybara.run_server = true

# I don't remember what this was for. Another team member wrote this part...
module ActionDispatch
  module Integration #:nodoc:
    class Session
      def host
        [Capybara.server_host, Capybara.server_port].join(':')
      end
    end
  end
end

使用 capybara 1.1.2,我必须进行上述更改,但 server_host 变为 app_host 并修改 lib/capybara /server.rb 在 gem 中,如下所示:

def url(path)
  ..
  if path =~ /^http/
    path
  else
    # Was this (Capybara.app_host || "http://#{host}:#{port}") + path.to_s
    (Capybara.app_host || "http://#{host}") + ":#{port}" + path.to_s
  end
end

This is not exactly the same situation as you but this might help some people:

For my current project, I'm using pow with many subdomains. The test suite also has to run on a different port.

The solution depends on which version of capybara you're running.

For the current latest release I put this in custom_env.rb:

Capybara.server_host = 'myapp.dev'
Capybara.server_port = 9887
Capybara.run_server = true

# I don't remember what this was for. Another team member wrote this part...
module ActionDispatch
  module Integration #:nodoc:
    class Session
      def host
        [Capybara.server_host, Capybara.server_port].join(':')
      end
    end
  end
end

With capybara 1.1.2, I had had to make the above change but server_host becomes app_host AND modify lib/capybara/server.rb in the gem like this:

def url(path)
  ..
  if path =~ /^http/
    path
  else
    # Was this (Capybara.app_host || "http://#{host}:#{port}") + path.to_s
    (Capybara.app_host || "http://#{host}") + ":#{port}" + path.to_s
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文