Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec:设置请求头_Ruby On Rails_Ruby_Rspec_Rspec Rails - Fatal编程技术网

Ruby on rails RSpec:设置请求头

Ruby on rails RSpec:设置请求头,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,我正试图在我的规范中使用ActionDispatchrequesthelper方法设置重要的标题: RSpec.describe API::V1::FoosController, type: :request do describe 'GET #index' do context 'common case' do request.env.merge!({'HTTP_FOO': 'FOO'}) get api_foos_path, {}, {Accept: Mi

我正试图在我的规范中使用
ActionDispatch
request
helper方法设置重要的标题:

RSpec.describe API::V1::FoosController, type: :request do
  describe 'GET #index' do
    context 'common case' do
      request.env.merge!({'HTTP_FOO': 'FOO'})
      get api_foos_path, {}, {Accept: Mime::JSON}
    end
  end
end
但当涉及到控制器时,此标头(以及通常使用
请求设置的任何标头)
)将消失:

class API::V1::FoosController < ApplicationController
  respond_to :json, :xml

  def index
    request.env.has_key? 'HTTP_FOO' # false
    respond_with serialize_models(Foo.all)
  end
  # ...
end
class API::V1::FoosController
为什么会发生这种情况?如何正确设置? 使用
request.header
@request.header
设置标头也会执行相同的操作


注意:我知道我可以将头设置为
Rack::Test::Methods
helpers的第三个参数,但我不想违反DRY-我只希望Mime格式在那里定义。

使用
controller.request.headers

controller.request.headers['HTTP_FOO'] = 'FOO'
我可以验证这种方法在Rails 4.2.5中是否有效,因为这是直接从真实代码中提取的

我们的测试如下所示:

describe SomeController, type: :controller do
  let(:current_user) { create :user }

  before :each do
    controller.request.headers['Authorization'] = "APIKey #{current_usser.api_key}"
  end
end
before_action :authenticate_request

def authenticate_request
  key = request.headers['Authorization']
  user = User.find_by(api_key: key)
  # raise AuthenticationError unless user, etc etc
end
我们的
ApplicationController
看起来(或多或少)是这样的:

describe SomeController, type: :controller do
  let(:current_user) { create :user }

  before :each do
    controller.request.headers['Authorization'] = "APIKey #{current_usser.api_key}"
  end
end
before_action :authenticate_request

def authenticate_request
  key = request.headers['Authorization']
  user = User.find_by(api_key: key)
  # raise AuthenticationError unless user, etc etc
end
请像这样尝试:

request.env['HTTP_FOO_HEADER'] = 'foo header'

我也使用Rails 4.2.5,但是。。。它不起作用。您使用哪一版本的rack,rspec core?@DreamWalker
gem'rspec rails',“~>3.0'
,rack 1.6.4mine是
rspec-core-3.0.4
rspec-rails-3.0.2
和同一版本的
rack
,您能提供一段这种方法适用的代码吗?@DreamWalker请参阅更新。这可能是您的规范中缺少
type::controller
。您是对的-区别在于规范类型,它确实适用于
controller
规范,但不适用于
request
规范。反过来,控制器规范还有十几条其他警告:(尽管如此,谢谢。不幸的是,以不同的方式添加环境键/值不会改变任何东西。我使用
rspec3.3.0
rails4.2.5.1
测试了此代码段。它可以工作。您使用控制器规格测试过它吗?是的,我使用控制器测试过它。它不适用于请求的代码段,请参阅注释以获取答案。)。