2

How do I set headers within before each like below?

RSpec.describe "Users API", type: :request do
  before :each do
    host! "example.org"
    // set a header for all requests here ??
  end
end

Possible duplicate: How to set request headers in rspec request spec?

But above didn't answer my question.

Community
  • 1
  • 1
Chanaka
  • 954
  • 1
  • 8
  • 17
  • Possible duplicate of [Set header in RSpec 3 request](https://stackoverflow.com/questions/25815741/set-header-in-rspec-3-request) – slhck Jul 23 '19 at 11:31

1 Answers1

-1

Found one solution. Include the Rack::Test::Methods module. I don't know why request specs doesn't provide this feature.

RSpec.describe "Users API", type: :request do
  include Rack::Test::Methods

  before :each do
    host! "example.org"
    // now we can do
    header 'Authorization', "Bearer #{some_token}"
  end
end
Chanaka
  • 954
  • 1
  • 8
  • 17
  • 1
    including Rack::Test::Methods breaks the whole Request Spec flow, result of get|post|put... method will not be assigned to the response variable anymore. Do not do it in request specs. – januszm Jul 15 '18 at 21:34
  • @januszm You are right. Perhaps best thing is to set the header manually for each request rather than looking for a shortcut method. – Chanaka Jul 21 '18 at 00:58