Guest User

Untitled

a guest
Oct 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. class RequestId
  2. REQUEST_ID = 'request-id'.freeze
  3.  
  4. def initialize(app)
  5. @app = app
  6. end
  7.  
  8. def call(env)
  9. req = ActionDispatch::Request.new(env)
  10. if defined?(ApiClient::Base)
  11. ApiClient::Base.with_default_headers(REQUEST_ID => req.headers[REQUEST_ID] || '-') do
  12. @app.call(env)
  13. end
  14. else
  15. @app.call(env)
  16. end
  17. end
  18. end
  19.  
  20. describe RequestId do
  21. describe '#call' do
  22. let(:app) { mock('App') }
  23. let(:middleware) { RequestId.new(app) }
  24.  
  25. subject { middleware.call(Rails.env) }
  26.  
  27. context 'when ApiClient defined?' do
  28. before { middleware.stubs(:defined?).with(ApiClient::Base).returns(true) }
  29.  
  30. it 'set that stuff' do
  31. ApiClient::Base.expects(:with_default_headers).with('request-id' => '-')
  32. subject
  33. end
  34.  
  35. it 'calls the function call' do
  36. app.expects(:call).once
  37. subject
  38. end
  39. end
  40.  
  41. context 'when ApiClient not defined?' do
  42. before { middleware.stubs(:defined?).with(ApiClient::Base).returns(false) }
  43.  
  44. it 'don't set stuff' do
  45. ApiClient::Base.expects(:with_default_headers).with('request-id' => '-').never
  46. subject
  47. end
  48.  
  49. it 'calls the function call' do
  50. app.expects(:call).once
  51. subject
  52. end
  53. end
  54. end
  55. end
Add Comment
Please, Sign In to add comment