LoginSignup
7
7

More than 5 years have passed since last update.

force_sslを指定したControllerのSpecを書く

Posted at

Controllerにてforce_sslを指定することで、ブラウザからのアクセスを強制的にSSLにすることができる。

class HogeController < ApplicationController
  force_ssl

  def index
  end
end

force_sslを指定したControllerのRSpecを記述する場合、テストを通すためにはひと工夫必要。

describe HogeController do
  before do 
    request.env['HTTPS'] = 'on'
  end

  describe 'GET index' do
    it 'should xxxxx' do
      get :index
      # 略
    end
  end
end

request.env['HTTPS'] = 'on'が各Exampleの前に実行されるようにすることで、HTTPSからのアクセスとして各Exampleが実行されるようになる。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7