如何在 rspec 测试中设置 HTTP_USER_AGENT

发布于 2024-12-19 13:24:21 字数 902 浏览 0 评论 0原文

可能的重复:
是吗可以在 Rails 集成测试或规范中指定用户代理吗?

我正在使用 rspec 在我的 Rails 应用程序中测试请求。我需要能够在请求之前设置用户代理。

这不起作用:

  describe "GET /articles feed for feedburner" do
it "displays article feed if useragent is feedburner" do
  # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
  @articles=[]
  5.times do
    @articles << Factory(:article, :status=>1, :created_at=>3.days.ago)
  end
  request.env['HTTP_USER_AGENT'] = 'feedburner'
  get "/news.xml" 
  response.should be_success
  response.content_type.should eq("application/xml")
  response.should include("item[title='#{@articles.first.title}']")
end

end

如何正确指定用户代理?

Possible Duplicate:
Is it possible to specify a user agent in a rails integration test or spec?

I'm testing a request in my rails app using rspec. I need to be able to set the user agent before the request.

This is not working:

  describe "GET /articles feed for feedburner" do
it "displays article feed if useragent is feedburner" do
  # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
  @articles=[]
  5.times do
    @articles << Factory(:article, :status=>1, :created_at=>3.days.ago)
  end
  request.env['HTTP_USER_AGENT'] = 'feedburner'
  get "/news.xml" 
  response.should be_success
  response.content_type.should eq("application/xml")
  response.should include("item[title='#{@articles.first.title}']")
end

end

How can I properly specify the user agent?

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

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

发布评论

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

评论(3

美男兮 2024-12-26 13:24:21

尝试在测试中使用它:

request.stub!(:user_agent).and_return('FeedBurner/1.0')

或者对于较新的 RSpec:

allow(request).to receive(:user_agent).and_return("FeedBurner/1.0")

FeedBurner/1.0 替换为您要使用的用户代理。我不知道确切的代码是否有效,但类似的代码应该有效

Try using this in your test:

request.stub!(:user_agent).and_return('FeedBurner/1.0')

or for newer RSpec:

allow(request).to receive(:user_agent).and_return("FeedBurner/1.0")

Replace FeedBurner/1.0 with the user agent you want to use. I don't know if that exact code will work but something like it should.

妄断弥空 2024-12-26 13:24:21

这就是我在集成测试中所做的 - 请注意设置 REMOTE_ADDR 的最后一个哈希(不带 HTTP_)。也就是说,您不必在请求之前设置 HTTP 标头,您可以将其作为请求的一部分进行设置。

# Rails integration tests don't have access to the request object (so we can't mock it), hence this hack
it 'correctly updates the last_login_ip attribute' do
  post login_path, { :email => user.email, :password => user.password }, { 'REMOTE_ADDR' => 'some_address' }
  user.reload
  user.last_login_ip.should == 'some_address'
end

This is what I do in an integration test - notice the last hash that sets REMOTE_ADDR (without HTTP_). That is, you don't have to set HTTP header before the request, you can do so as part of the request.

# Rails integration tests don't have access to the request object (so we can't mock it), hence this hack
it 'correctly updates the last_login_ip attribute' do
  post login_path, { :email => user.email, :password => user.password }, { 'REMOTE_ADDR' => 'some_address' }
  user.reload
  user.last_login_ip.should == 'some_address'
end
梦幻的味道 2024-12-26 13:24:21

在某处定义它(例如 spec_helper.rb):

module DefaultUserAgent

  def post(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

  def get(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

end

然后在需要时包含 DefaultUserAgent 即可。

Define this somewhere (e.g. spec_helper.rb):

module DefaultUserAgent

  def post(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

  def get(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

end

Then just include DefaultUserAgent when you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文