【问题标题】:testing .mobile mime format with capybara / rspec使用 capybara / rspec 测试 .mobile mime 格式
【发布时间】:2012-09-05 03:43:07
【问题描述】:

为了检测和响应移动用户代理,我正在使用

Mime::Type.register_alias "text/html", :mobile

我想知道用 capybara 进行测试的最佳方法是什么。本文建议用Capybara.register_driver :iphone do |app|设置iphone驱动

http://blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/

但我想要一种更灵活的方法,通过 url 扩展设置 mime 类型

localhost/index.mobile

我在哪里可以做到这一点

visit user_path( format: :mobile)

Rails 理解扩展名并在 params 哈希中设置 format,但我如何获取 url 辅助方法将其作为文件扩展名添加到所有 url?

【问题讨论】:

标签: ruby-on-rails url-routing capybara mime-types


【解决方案1】:

我的答案是根据这个 Railscast 将格式保存在会话变量中:http://railscasts.com/episodes/199-mobile-devices。我选择使用 URL 扩展而不是查询字符串参数,因为它看起来更匹配。

这是我在application_controller.rb中的代码:

def mobile_device?
  session.has_key?(:mobile) ? session[:mobile] : request.user_agent =~ /Mobile|webOS/
end
helper_method :mobile_device?

def prepare_for_mobile
  # avoid messing with .json, .xml
  if request.format == 'text/html'
    # only do this when an explicit extension is present
    case File.extname(URI.parse(request.fullpath).path)
    when '.html'
      session[:mobile] = false
    when '.mobile'
      session[:mobile] = true
    end
    # stop using a session param and go back to letting the user_agent decide
    when '.ua'
      session.delete(:mobile)
    end
    request.format = mobile_device? ? :mobile : :html
  end
end

这里是mime_types.rb

Mime::Type.register_alias "text/html", :ua      # let the user agent decide
Mime::Type.register_alias "text/html", :mobile  # mobile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多