【问题标题】:Validate that CORS is enabled in Rails 6 with RSpec使用 RSpec 验证是否在 Rails 6 中启用了 CORS
【发布时间】:2020-09-13 22:15:14
【问题描述】:

我有一个端点需要允许来自域外的请求。

在我的config/application.rb

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '/api/*',
                 headers: :any,
                 credentials: false,
                 methods: %i[post options]
      end
    end

但是,我需要在 RSpec 中编写一个规范(我假设一个 feature 规范)以确保该端点允许来自域外的请求。

我的端点规范大致如下:

RSpec.describe FooController, type: :request do
  describe 'POST foo#create' do
    it 'should accept an external request' do
      post foo_create_path
      expect(response.status).to eq(200)
    end
  end
end

如何模拟来自另一个域的请求?

【问题讨论】:

    标签: ruby-on-rails rspec cors


    【解决方案1】:

    您可以使用Rack::MockRequest

    如:`Rack::MockRequest.env_for("https://#{host}/a?foo=bar", {}) }``

    你可以找到一个实现here

    【讨论】:

    • 谢谢!我会试试这个。
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2017-05-28
    • 2021-07-14
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多