Custom Headers In Rails Tests
Join the DZone community and get the full member experience.Join For Free
From time to time you may need to set headers in your functional tests that aren't supported by the @request object. I had to set up my request with Basic authentication. Add an extension to the TestRequest class in the test/test_helper.rb file:
class ActionController::TestRequest def set_header(name, value) @env[name] = value end endThen set values as required in your functional test.
def test_index @request.set_header "HTTP_AUTHORIZATION", "Basic " + Base64.encode64('testuser:testpass') get :index assert_response :success assert_template "index" end