RSpec Request - How to set http authorization header for all requests

后端 未结 4 2174
清歌不尽
清歌不尽 2021-02-07 06:01

I\'m using rspec request to test a JSON API that requires an api-key in the header of each request.

I know I can do this:

get \"/v1/users/janedoe.json\"         


        
4条回答
  •  忘了有多久
    2021-02-07 06:37

    This is another way to do it if you are doing a post.

    @authentication_params = { 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Token.encode_credentials(Temp::Application.config.api_key) }
    
    expect { post "/api/interactions", @interaction_params, @authentication_params }.to change(Interaction, :count).by(1)
    

    Note interaction_params is just a json object I am passing in.

提交回复
热议问题