RailsのコントローラのSSL・非SSLのときの振る舞いをテストするには request.env['HTTPS']
に 'on'
か 'off'
を代入すればいい。
describe SomeController do
describe 'GET new' do
context 'access using SSL' do
before { request.env['HTTPS'] = 'on' }
it do
get :new # SSLでアクセスする
end
end
context 'access not using SSL' do
before { request.env['HTTPS'] = 'off' }
# この中は非SSL
end
end
end
毎回 before
ブロックを書くのは面倒くさいので shared_context
を使うと捗る。以下の様なコードを用意する。
shared_context 'SSL', ssl: true do
before { request.env['HTTPS'] = 'on' }
end
shared_context 'not SSL', ssl: false do
before { request.env['HTTPS'] = 'off' }
end
すると以下のように書ける。
describe SomeController do
describe 'GET new' do
context 'access using SSL', ssl: true do
it do
get :new # SSLでアクセスする
end
end
context 'access not using SSL', ssl: false do
# この中は非SSL
end
end
end