Rails 8 docker を使用している場合に rspec > requests spec で 403 エラーが発生した場合の確認点について調査したものを備忘録として残しておく
環境
- ruby: 3.4.2
- rails: 8.0.2
- mode: API mode
エラー内容
Rails 6 以降で追加された DNS リバインディングに関するエラーが発生している
※エラー内容と根本原因は異なるため注意
<!-- 一部抜粋 -->
<main role="main" id="container">
<h2>To allow requests to these hosts, make sure they are valid hostnames (containing only numbers, letters, dashes and dots), then add the following to your environment configuration:</h2>
<pre>
config.hosts << "www.example.com"
</pre>
<p>For more details view: <a href="https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization">the Host Authorization guide</a></p>
</main>
確認点
Docker で使用している環境変数を確認する
RAILS_ENV=development
が設定されていないこと
spec/rails_helper.rb
を確認すると環境変数を設定してしまっている状態だと test 環境とならないようになっている
そのため docker で環境を構築する際、環境変数として RAILS_ENV=development
と設定していると test 環境として実行されないためエラーになる
ENV['RAILS_ENV'] ||= 'test'