Sending custom headers through RSpec

我的梦境 提交于 2019-12-03 00:05:38

well, maybe too late for people but just to be lined up:

it 'should get profile when authorized' do
  user = FactoryGirl.create :user
  request.headers[EMAIL_TOKEN] = user.email
  request.headers[AUTH_TOKEN] = user.authentication_token
  get :profile
  response.should be success
end

just call request.headers with appropriate settings.

You can define it in get directly.

get :api_call, nil, {'HTTP_FOO'=>'BAR'}

I just verified it works in console.

RSpec request specs changed in Rails 5 so that custom headers and params must now be defined using key-value hash arguments. E.g.:

Before in Rails 4:

it "creates a Widget and redirects to the Widget's page" do
  headers = { "CONTENT_TYPE" => "application/json" }
  post "/widgets", '{ "widget": { "name":"My Widget" } }', headers
  expect(response).to redirect_to(assigns(:widget))
end

Now for Rails 5:

it "creates a Widget and redirects to the Widget's page" do
  headers = { "CONTENT_TYPE" => "application/json" }
  post "/widgets", :params => '{ "widget": { "name":"My Widget" } }', :headers => headers
  expect(response).to redirect_to(assigns(:widget))
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!