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が実行されるようになる。