【问题标题】:Rails returns incorrect MIME type for json api request (only in test)Rails 为 json api 请求返回不正确的 MIME 类型(仅在测试中)
【发布时间】:2017-08-14 13:14:11
【问题描述】:

在我的应用程序中,我动态呈现外部小部件请求的 javascript 文件。在开发环境中一切正常,但是当我测试时,出现错误:

Refused to execute script from 'http://localhost:3000/widget_init.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

为什么只出现在测试环境中,如何手动设置MIME type 'text/javascript?

路线:

  get 'widget_init', to: 'widget/root#init'

控制器:

def init
  respond_to do |format|
    format.html

    format.js { render 'init' }
  end
end

UPD:我的测试是否对您有帮助:

scenario 'opens widget if webpage id and token are valid', js: true do
  webpage.url = 'http://localhost:3000/test_widget/with_script'
  webpage.save
  webpage.reload

  visit workspace_webpage_path(webpage)
  find('#open_webpage_button').click
  #here should open a widget
  expect(page).to have_selector('#follower_widget__script')
  expect(page).to have_selector('#follower_widget__root')
end

【问题讨论】:

  • 手动添加 mime 类型没有帮助:format.js { render 'init', mime_type: Mime::Type.lookup("text/javascript") }
  • 你可以展示你的测试吗?
  • 嗨@kunashir!我更新了描述。
  • 主要的谜团是只有在我运行 rspec 测试时才会出现此错误消息。

标签: javascript ruby-on-rails ruby-on-rails-5 mime


【解决方案1】:

我发现问题出在 Chrome 中,它打开了一个包含不正确信息的新标签(例如,虽然我之前在同一个测试中登录过,但用户没有登录)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2018-06-30
    • 2020-09-28
    • 2011-07-10
    • 2014-05-27
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多