具有机架保护的Rspec测试API

时间:2014-03-16 16:15:52

标签: api session rspec rack grape

我正在尝试使用rspec集成(请求)测试来测试我的API。

我在浏览器的0.0.0.0:3000/api/regions转到我的api端点并返回我的数据,我得到一个会话ID,看起来一切正常。

我在API中使用机架保护:

module Api
  class Root < Grape::API
     use Rack::Protection, instrumenter: ActiveSupport::Notifications
     use Rack::Protection::AuthenticityToken    
     prefix  'api'

     helpers do
        def warden
          request.env['warden']
        end
     end

     version 'v1', using: :header, vendor: 'vendor', strict: false do
        mount Resources::V1::Search => '/'
        mount Resources::V1::Regions => '/'
     end    

   end
end

api资源:

module Resources
  module V1
    class Regions < Grape::API

      resource :regions do
        get do
          # Some code...
        end
      end
    end
  end
end

spec_helper.rb

[...]
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  config.include Rack::Test::Methods, type: :request
  config.include Module.new { def app; Api::Root; end }, type: :request

  config.include Warden::Test::Helpers

  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

这是一个测试:

describe Resources::V1::Regions do
  describe 'GET /regions' do
    it 'returns some data' do
      get '/api/regions'
      [... expectations - doesn't get here...]
    end
  end
end

这是错误:

 RuntimeError:
       you need to set up a session middleware *before* Rack::Protection::RemoteToken
     # ./spec/requests/api/region_spec.rb:6:in `block (3 levels) in <top (required)>'

来自这里:https://github.com/rkh/rack-protection/blob/master/lib/rack/protection/base.rb#L85

所以我需要添加机架会话中间件,对吧?

我将use Rack::Session::Cookie, secret: '...'添加到我的api中,这让我请求。[&#39; warden&#39;]为nil(另一个问题是另一个问题)。

但是,现在,当我使用浏览器加载端点时,我得到了:

undefined method `each' for #<ActionDispatch::Request::Session:0x7f7bf9e521e0 not yet loaded>

在这里提出:https://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb#L158

我怀疑我不需要use Rack::Session::Cookie,因为服务器加载时其他东西正在做,但我需要添加一些东西让测试工作。不知道那是什么东西。

如果您需要任何其他信息,请与我们联系。

版本:

  • grape(0.6.1)
  • rails(4.0.2)
  • rack-protection(1.5.2)
  • rspec(2.14.1)

1 个答案:

答案 0 :(得分:0)

我通过添加&#39; rack.session&#39;来解决这个问题。哈希到我请求的第三个参数,即get '/api/regions', {}, {'rack.session' => {}}

但我找到了更好的方法:https://github.com/heavysixer/grape-on-rack/pull/1 它增加了会话并同时解决了监狱长问题。

RSpec.configure do |config|
[...]
  rack_app = Module.new do 
    def app
      @app ||= Rack::Builder.new do
        use Rack::Session::Cookie
        Warden::Manager.serialize_into_session { |user| user.id }
        Warden::Manager.serialize_from_session { |id| User.get(id) }
        use Warden::Manager do |manager|
          manager.default_strategies :password, :basic
          # manager.failure_app = Acme::BadAuthentication
        end
        run Api::Root
      end
    end
  end

  config.include rack_app, type: :request
  end

标记为答案,除非有人有更好的事情。