在Rails Minitest中请求之前设置标头

时间:2018-06-09 05:47:33

标签: ruby-on-rails ruby ruby-on-rails-5 minitest

在Ruby on Rails Minitest中,有没有办法在ActionDispatch::IntegrationTest测试中调用请求之前设置标题?

目前,before / setup内部不是解决方案。

# Default way
test 'foo' do
  get '/home', headers: { foo: "bar" }
end

# How I wish
test 'foo' do
  @request.headers['foo'] = "bar"

  get '/home'
end

# I already tried and it did not work...
test 'foo' do
  request.headers['foo'] = 'bar'
  @request.headers['foo'] = 'bar'
  request.env['foo'] = 'bar'
  @request.env['foo'] = 'bar'
  request.env['HTTP_FOO'] = 'bar'
  @request.env['HTTP_FOO'] = 'bar'

  get '/home'
end
  • Ruby版本:ruby 2.5.1p57
  • Rails版本:5.2.0
  • Minitest版本:5.1

1 个答案:

答案 0 :(得分:0)

默认方式是好的,但根据标题自定义标题的命名,你应该以" X开始 - "前缀。

test 'foo' do
  get '/home', headers: { "X-Foo": "bar" }
end