参考: rack middlewareのテストをrspecで書いてみた - Qiita
上記ページを参考に書いてみた。
テスト対象がMyMiddleware
とした場合、以下のように書ける。
require "rails_helper"
RSpec.describe MyMiddleware do
include Rack::Test::Methods
# テスト用アプリケーション
class TestApplication
def call(_)
[200, { "Content-Type" => "text/html;charset=utf-8" }, ["test body"]]
end
end
let(:test_app) { TestApplication.new }
let(:app) { MyMiddleware.new(test_app) }
describe "GET /" do
it "200 を返すこと" do
get "/"
expect(last_response.status).to eq 200
end
end
end
Tips
リクエストのドメイン名を指定
get のパラメタにドメイン名を含むURL文字列を渡すと request.host
にドメインが設定される。
get "http://hoge.example.com/"
# => request.host == "hoge.example.com"