rspec_api_documentation icon indicating copy to clipboard operation
rspec_api_documentation copied to clipboard

Ability to customize env for requests

Open hogaur opened this issue 9 years ago • 5 comments

User should be able to set env variables per request with do_request method call.

hogaur avatar Feb 17 '16 06:02 hogaur

Do you mean customize the rack env hash? If so I'm not a fan. RAD is an integration level tool, so playing around with the env isn't something I want add.

oestrich avatar Feb 17 '16 15:02 oestrich

@oestrich how would one set HTTP_AUTHORIZATION env if i wanted to test routes that require basic auth? Here's an example of what i'm trying to do:

resource 'Users' do
  #header 'HTTP_AUTHORIZATION', ActionController::HttpAuthentication::Basic.encode_credentials('[email protected]', "password")

  before do
    @user = FactoryGirl.create :user, email: '[email protected]', password: 'password'
    request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, "password")
  end

  get "/admin/api/v1/users" do
    example "listing users" do
      do_request
      status.should == 200
      response_body.should == {users: [{users: @user}]}.to_json
    end
  end
end

This returns a 401. However, using the standard Rspec DSL, the tests actually pass.

kareemshahin avatar Feb 08 '17 22:02 kareemshahin

@kareemshahin we have done this in the past, I think you'll want to customize your client via config.client_method

jakehow avatar Feb 08 '17 23:02 jakehow

You want to use the header DSL.

header "Authorization", :basic_auth

let(:basic_auth) do
  ActionController::HttpAuthentication::Basic.encode_credentials(user.email, "password")
end

oestrich avatar Feb 08 '17 23:02 oestrich

thank you @oestrich, silly mistake on my behalf. i was using the wrong header

kareemshahin avatar Feb 09 '17 23:02 kareemshahin