2
RSpec.describe "Home", type: :request do

    describe "GET /index" do
      it "is a success" do
        get root_path, {}, {'HTTPS' => 'on'}
        expect(response).to have_http_status(200)
      end
    end
end

Rspec version - 3.8 Test Type - Request spec

How do I set https protocol globally on all requests specs?

Shani
  • 2,433
  • 2
  • 19
  • 23

1 Answers1

3

It looks like you might be using Rails, in which case you could do one of the following:

  1. To set "https" for all Rspec examples, create a file, such as spec/support/url_options.rb, that contains the following:
RSpec.configure do |config|
  Rails.application.routes.default_url_options[:protocol] = 'https'
end
  1. To only target request specs, you could use a hook, such as:
RSpec.configure do |config|
  config.before(:all, type: :request) do
    Rails.application.routes.default_url_options[:protocol] = 'https'
  end
end
  1. For a site-wide configuration, you could instead add the following to config/environments/test.rb:
Rails.application.configure do
  default_url_options[:protocol] = 'https'
end

This should work fine for request specs, but there might be additional complications with feature specs. In that case, see https://github.com/rspec/rspec-rails/issues/1275.


If you're not using Rails, you could still follow the broad pattern above, but substitute your framework's equivalent method or configuration setting. If all else fails, you could override the Rspec methods get(), post(), etc. and in your overridden methods simply call super() with { protocol: 'https' } or { 'HTTPS' => 'on' } merged into the arguments.

Also, note that 'HTTPS' => 'on' may have been deprecated in favor of protocol: 'https'.


Similar posts with additional possibilities:

Test an HTTPS (SSL) request in RSpec Rails

Simple way to test an HTTPS (SSL) request with RSpec

Force HTTPS/SSL for all controller/request specs in rspec