Over a million developers have joined DZone.

Custom Headers In Rails Tests

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

Then 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"

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}