Validate that CORS is enabled in Rails 6 with RSpec

Viewed 396

I have an endpoint which needs to permit requests from outside the domain.

In my 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

However, I need to write a spec in RSpec (I assume a feature spec) to ensure that requests from outside the domain are permitted for this endpoint.

My spec for the endpoint looks generically like this:

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

How do I simulate this request coming in from another domain?

1 Answers
Related