Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RequestId
- REQUEST_ID = 'request-id'.freeze
- def initialize(app)
- @app = app
- end
- def call(env)
- req = ActionDispatch::Request.new(env)
- if defined?(ApiClient::Base)
- ApiClient::Base.with_default_headers(REQUEST_ID => req.headers[REQUEST_ID] || '-') do
- @app.call(env)
- end
- else
- @app.call(env)
- end
- end
- end
- describe RequestId do
- describe '#call' do
- let(:app) { mock('App') }
- let(:middleware) { RequestId.new(app) }
- subject { middleware.call(Rails.env) }
- context 'when ApiClient defined?' do
- before { middleware.stubs(:defined?).with(ApiClient::Base).returns(true) }
- it 'set that stuff' do
- ApiClient::Base.expects(:with_default_headers).with('request-id' => '-')
- subject
- end
- it 'calls the function call' do
- app.expects(:call).once
- subject
- end
- end
- context 'when ApiClient not defined?' do
- before { middleware.stubs(:defined?).with(ApiClient::Base).returns(false) }
- it 'don't set stuff' do
- ApiClient::Base.expects(:with_default_headers).with('request-id' => '-').never
- subject
- end
- it 'calls the function call' do
- app.expects(:call).once
- subject
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment